public static void RemoveAt <T>(this StorageList list, BigInteger index) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } var indexKey = ElementKey(list.BaseKey, index); size = size - 1; if (size > index) { // TODO <T> would not really be necessary here, this swap could be improved by using byte[] var last = list.Get <T>(size); list.Replace(index, last); } var key = ElementKey(list.BaseKey, size); list.Context.Delete(key); list.Context.Put(CountKey(list.BaseKey), size); }
public static void Add <T>(this StorageList list, T element) { var count = list.Count(); list.Context.Put(CountKey(list.BaseKey), count + 1); list.Replace(count, element); }
public static void Remove <T>(this StorageList list, T obj) { var index = list.IndexOf(obj); if (index >= 0) { list.RemoveAt <T>(index); } }
public static BigInteger Add <T>(this StorageList list, T element) { var index = list.Count(); list.Context.Put(CountKey(list.BaseKey), index + 1); list.Replace(index, element); return(index); }
public static BigInteger AddRaw(this StorageList list, byte[] bytes) { var index = list.Count(); list.Context.Put(CountKey(list.BaseKey), index + 1); list.ReplaceRaw(index, bytes); return(index); }
public static T[] All <T>(this StorageList list) { var size = list.Count(); var items = new T[(int)size]; for (int i = 0; i < size; i++) { items[i] = list.Get <T>(i); } return(items); }
public static void ReplaceRaw(this StorageList list, BigInteger index, byte[] bytes) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } var key = ElementKey(list.BaseKey, index); list.Context.Put(key, bytes); }
public static byte[] GetRaw(this StorageList list, BigInteger index) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } var key = ElementKey(list.BaseKey, index); var bytes = list.Context.Get(key); return(bytes); }
public static T Get <T>(this StorageList list, BigInteger index) { var bytes = GetRaw(list, index); if (typeof(IStorageCollection).IsAssignableFrom(typeof(T))) { var args = new object[] { bytes, list.Context }; var obj = (T)Activator.CreateInstance(typeof(T), args); return(obj); } else { return(Serialization.Unserialize <T>(bytes)); } }
public static void Replace <T>(this StorageList list, BigInteger index, T element) { byte[] bytes; if (typeof(IStorageCollection).IsAssignableFrom(typeof(T))) { var collection = (IStorageCollection)element; //bytes = MergeKey(map.BaseKey, key); bytes = collection.BaseKey; } else { bytes = Serialization.Serialize(element); } ReplaceRaw(list, index, bytes); }
public static BigInteger IndexOf <T>(this StorageList list, T obj) { BigInteger index = 0; var size = list.Count(); while (index < size) { var val = list.Get <T>(index); if (val.Equals(obj)) { return(index); } index++; } return(-1); }
public static T Get <T>(this StorageList list, BigInteger index) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } var key = ElementKey(list.BaseKey, index); var bytes = list.Context.Get(key); if (typeof(IStorageCollection).IsAssignableFrom(typeof(T))) { var args = new object[] { bytes, list.Context }; var obj = (T)Activator.CreateInstance(typeof(T), args); return(obj); } else { return(Serialization.Unserialize <T>(bytes)); } }
public static void RemoveAt(this StorageList list, BigInteger index) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } size = size - 1; if (size > index) { var last = list.GetRaw(size); list.ReplaceRaw(index, last); } var key = ElementKey(list.BaseKey, size); list.Context.Delete(key); list.Context.Put(CountKey(list.BaseKey), size); }
public static T[] Range <T>(this StorageList list, BigInteger minIndex, BigInteger maxIndex) { if (minIndex > maxIndex) { throw new StorageException("outside of range"); } int total = 1 + (int)(maxIndex - minIndex); var result = new T[total]; int offset = 0; BigInteger index = minIndex; while (offset < total) { result[offset] = list.Get <T>(index); offset = offset + 1; index++; } return(result); }
public static void Replace <T>(this StorageList list, BigInteger index, T element) { var size = list.Count(); if (index < 0 || index >= size) { throw new StorageException("outside of range"); } var key = ElementKey(list.BaseKey, index); byte[] bytes; if (typeof(IStorageCollection).IsAssignableFrom(typeof(T))) { var collection = (IStorageCollection)element; //bytes = MergeKey(map.BaseKey, key); bytes = collection.BaseKey; } else { bytes = Serialization.Serialize(element); } list.Context.Put(key, bytes); }
public static BigInteger Count(this StorageList list) { var result = list.Context.Get(CountKey(list.BaseKey)).AsBigInteger(); return(result); }
public static bool Contains <T>(this StorageList list, T obj) { return(list.IndexOf(obj) >= 0); }
// TODO should this delete all entries instead of just adjusting the count()? public static void Clear(this StorageList list) { BigInteger count = 0; list.Context.Put(CountKey(list.BaseKey), count); }