/// <summary> /// Initialize a list with given metadata, first element and rest of list. /// </summary> /// <param name="meta">The metadata to attach.</param> /// <param name="first">The first element in the list.</param> /// <param name="rest">The rest of the list.</param> /// <param name="count">The number of elements in the list.</param> PersistentList(IPersistentMap meta, Object first, IPersistentList rest, int count) : base(meta) { this._first = first; this._rest = rest; this._count = count; }
public Employer(Storage db, string name) { Name = name; CreatedAt = DateTime.Now; _listings = db.CreateScalableList <Listing>(); }
public static string Serialize(IPersistentList list) { IList data = list.GetRawData(); StringBuilder sb = new StringBuilder(); sb.Append(" ["); bool first = true; foreach (object o in data) { string value = Serialize(o); sb.AppendFormat("{0}", value); if (first) { first = false; } else { sb.AppendLine(","); sb.Append("\t\t"); } } sb.Append("]"); return(sb.ToString()); }
public static string Serialize(IPersistentList list) { IList data = list.GetRawData(); StringBuilder sb = new StringBuilder(); sb.Append(" ["); bool first = true; foreach (object o in data) { string value = Serialize(o); sb.AppendFormat("{0}",value); if (first) { first = false; } else { sb.AppendLine(","); sb.Append("\t\t"); } } sb.Append("]"); return sb.ToString(); }
internal PersistentList(object first, IPersistentList rest, int count, IPersistentMap metadata) : base(metadata) { this.first = first; this.rest = rest; this.count = count; }
public void RemoveOutOfRangeTest() { const int count = 10; IPersistentList <int> list = PersistentList <int> .Of(new int[count]); Assert.Throws <IndexOutOfRangeException>(() => list.RemoveAt(-1)); Assert.Throws <IndexOutOfRangeException>(() => list.RemoveAt(count)); }
public void SetItemOutOfRangeTest() { const int count = 10; IPersistentList <int> list = PersistentList <int> .Of(new int[count]); Assert.Throws <IndexOutOfRangeException>(() => list.Set(-1, -1)); Assert.Throws <IndexOutOfRangeException>(() => list.Set(count, -1)); }
public static IPersistentList create(IList init) { IPersistentList ret = EMPTY; for (int i = init.Count - 1; i >= 0; --i) { ret = (IPersistentList)ret.cons(init[i]); } return(ret); }
private void CheckEmptyListBehaviour(IPersistentList <int> empty) { Assert.Equal(0, empty.Count); foreach (IEnumerator enumerator in new IEnumerator[] { empty.GetEnumerator(), ((IEnumerable)empty).GetEnumerator() }) { Assert.False(enumerator.MoveNext()); } Assert.Throws <IndexOutOfRangeException>(() => empty[0]); Assert.Throws <IndexOutOfRangeException>(() => empty.RemoveAt(0)); }
private static void InjectInterceptableList(IPersistentObject target, PropertyInfo property, Type propertyType) { Type subType = propertyType.GetGenericArguments()[0]; Type genericType = typeof(PersistentList <>).MakeGenericType(subType); Object instance = Activator.CreateInstance(genericType); property.SetValue(target, instance, null); IPersistentList persistentList = (IPersistentList)instance; persistentList.Owner = target; }
public void AddOutOfRangeTest() { const int count = 100; IPersistentList <int> list = PersistentList <int> .Empty; foreach (int i in Enumerable.Range(1, count)) { list = list.Add(i); } Assert.Throws <IndexOutOfRangeException>(() => list.Add(-1, 1)); Assert.Throws <IndexOutOfRangeException>(() => list.Add(count + 1, 1)); }
private IPersistentList ReadList(TextReader source) { ConsumeChar(source); ReadWhiteSpace(source); IPersistentList listBuilder = PersistentList.EMPTY; while (PeekChar(source) != ')') { listBuilder = (IPersistentList)listBuilder.cons(Read(source)); ReadWhiteSpace(source); } ConsumeChar(source); return(listBuilder); }
public void ListCtorConstructsListOfSeveralElements() { object[] items = new object[] { 1, "abc", 2, "def" }; IPersistentList p = PersistentList.create(items); Expect(p.count(), EqualTo(4)); ISeq s = p.seq(); Expect(s.first(), EqualTo(1)); Expect(s.next().first(), EqualTo("abc")); Expect(s.next().next().first(), EqualTo(2)); Expect(s.next().next().next().first(), EqualTo("def")); Expect(s.next().next().next().next(), Null); }
public void AddFirstTest() { const int count = 100; IPersistentList <int> list = PersistentList <int> .Empty; foreach (int i in Enumerable.Range(1, count)) { list = list.Add(0, count - i + 1); Assert.Equal(i, list.Count); int index = 0; foreach (int j in list) { Assert.Equal(count - i + index + 1, j); index++; } } }
/// <summary> /// The creator method. /// </summary> /// <param name="args">A sequence of elements.</param> /// <returns>A new list.</returns> protected override object doInvoke(object args) { if (args is IArraySeq ias) { object[] argsarray = (object[])ias.ToArray(); IPersistentList ret = EMPTY; for (int i = argsarray.Length - 1; i >= ias.index(); i--) { ret = (IPersistentList)ret.cons(argsarray[i]); } return(ret); } List <object> list = new List <object>(); for (ISeq s = RT.seq(args); s != null; s = s.next()) { list.Add(s.first()); } return(create(list)); }
static public object invokeStatic(ISeq args) { if (args is IArraySeq ias) { object[] argsarray = (object[])ias.ToArray(); IPersistentList ret = EMPTY; for (int i = argsarray.Length - 1; i >= 0; i--) { ret = (IPersistentList)ret.cons(argsarray[i]); } return(ret); } List <object> list = new List <object>(); for (ISeq s = RT.seq(args); s != null; s = s.next()) { list.Add(s.first()); } return(create(list)); }
internal ScalableList(Storage storage, int initialSize) : base(storage) { #if USE_GENERICS if (initialSize <= BtreeThreshold) { small = storage.CreateLink <T>(initialSize); } else { large = storage.CreateList <T>(); } #else if (initialSize <= BtreeThreshold) { small = storage.CreateLink(initialSize); } else { large = storage.CreateList(); } #endif }
public void Insert(int i, object o) #endif { if (small != null) { if (small.Count == BtreeThreshold) { #if USE_GENERICS large = Storage.CreateList <T>(); foreach (T obj in small) { large.Add(obj); } #else large = Storage.CreateList(); foreach (object obj in small) { large.Add(obj); } #endif large.Insert(i, o); Modify(); small = null; } else { #if USE_GENERICS small.Insert(i, o); #else small.Insert(i, o); #endif } } else { large.Insert(i, o); } }
private static void CheckListEquality <T>(IList <T> expected, IPersistentList <T> given) { Assert.Equal(expected.Count, given.Count); // check using indexer for (int i = 0; i < expected.Count; i++) { Assert.Equal(expected[i], given[i]); } // same check using enumerators foreach (IEnumerator enumerator in new IEnumerator[] { given.GetEnumerator(), ((IEnumerable)given).GetEnumerator() }) { IEnumerator expectedEnumerator = expected.GetEnumerator(); while (expectedEnumerator.MoveNext()) { Assert.True(enumerator.MoveNext()); Assert.Equal(expectedEnumerator.Current, enumerator.Current); } Assert.False(enumerator.MoveNext()); } }
public void SetItemTest() { const int count = 200; IPersistentList <int> list = PersistentList <int> .Empty; for (int i = 0; i < count; i++) { list = list.Add(i); } IPersistentList <int> list2 = list; for (int i = 0; i < count; i++) { list = list.Set(i, j => j + 1, out int oldValue); Assert.Equal(i, oldValue); list2 = list2.Set(i, i + 1, out oldValue); Assert.Equal(i, oldValue); } CheckListEquality(Enumerable.Range(1, count).ToArray(), list); CheckListEquality(Enumerable.Range(1, count).ToArray(), list2); }
private void AddRemoveAtTestImpl(InsertRemoveScenario scenario) { IPersistentList <int> list = PersistentList <int> .Empty; List <int> expected = new List <int>(); int i = 0; foreach (int position in scenario.InsertionOrder) { list = list.Add(position, i); expected.Insert(position, i); CheckListEquality(expected, list); i++; } foreach (int position in scenario.RemovalOrder) { list = list.RemoveAt(position, out int removedValue); Assert.Equal(expected[position], removedValue); expected.RemoveAt(position); CheckListEquality(expected, list); } }
public void EmptyListTest(IPersistentList <int> empty) { CheckEmptyListBehaviour(empty); Assert.NotSame(empty, empty.Add(1)); }
static public void Main(string[] args) { int i; Storage db = StorageFactory.Instance.CreateStorage(); db.Open("testlist2.dbs", pagePoolSize); #if USE_GENERICS IPersistentList <Record> root = (IPersistentList <Record>)db.Root; if (root == null) { root = db.CreateList <Record>(); db.Root = root; } #else IPersistentList root = (IPersistentList)db.Root; if (root == null) { root = db.CreateList(); db.Root = root; } #endif DateTime start = DateTime.Now; for (i = 0; i < nRecords / 2; i++) { Record rec = new Record(); rec.i = i * 2; root.Add(rec); } for (i = 1; i < nRecords; i += 2) { Record rec = new Record(); rec.i = i; root.Insert(i, rec); } db.Commit(); Console.WriteLine("Elapsed time for inserting " + nRecords + " records: " + (DateTime.Now - start)); start = DateTime.Now; for (i = 0; i < nRecords; i++) { #if USE_GENERICS Record rec = root[i]; #else Record rec = (Record)root[i]; #endif Debug.Assert(rec.i == i); } Console.WriteLine("Elapsed time for performing " + nRecords + " gets: " + (DateTime.Now - start)); start = DateTime.Now; i = 0; foreach (Record rec in root) { Debug.Assert(rec.i == i); i += 1; } Debug.Assert(i == nRecords); Console.WriteLine("Elapsed time for iteration through " + nRecords + " records: " + (DateTime.Now - start)); start = DateTime.Now; for (i = 0; i < nRecords; i++) { #if USE_GENERICS Record rec = root[0]; #else Record rec = (Record)root[0]; #endif Debug.Assert(rec.i == i); root.RemoveAt(0); rec.Deallocate(); } Debug.Assert(!root.GetEnumerator().MoveNext()); Console.WriteLine("Elapsed time for deleting " + nRecords + " records: " + (DateTime.Now - start)); db.Close(); }
public IPersistentObject Get(string id) { if (uow.Objects.ContainsKey(id)) { WeakReference reff = (WeakReference)uow.Objects[id]; IPersistentObject instance = (IPersistentObject)reff.Target; if (reff.IsAlive) { return(instance); //return object if possible } else { uow.Objects.Remove(id); //clear dead ref } } //try to load object string filePath = dbPath + @"\" + id + ".txt"; if (System.IO.File.Exists(filePath)) { //try to load and deserialize object System.IO.StreamReader sr = new System.IO.StreamReader(filePath); string serializedData = sr.ReadToEnd(); sr.Close(); SerializedObject so = JSONSerializer.DeSerialize(serializedData); IPersistentObject managed = (IPersistentObject)engine.CreateProxy(so.Type); managed.Context = this; managed.Id = id; RegisterObject(managed); managed.Initializing = true; foreach (var entry in so.Data) { string property = (string)entry.Key; PropertyInfo pi = so.Type.GetProperty(property); if (entry.Value is PersistentId) { managed.SetReference(property, entry.Value); managed.SetUnloaded(property, true); } else { pi.SetValue(managed, entry.Value, null); managed.SetUnloaded(property, false); //setup list owner if (entry.Value is IPersistentList) { IPersistentList list = entry.Value as IPersistentList; list.Owner = managed; } } } managed.Initializing = false; return(managed); } else { return(null); //file didnt exist } }
/// <summary> /// Initializes a list of one member. /// </summary> /// <param name="first">The one member.</param> public PersistentList(object first) { this._first = first; this._rest = null; this._count = 1; }
public bool Equiv(IPersistentList <T> list) { return(Equals(list)); }