示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        public static void Remove <T>(this StorageList list, T obj)
        {
            var index = list.IndexOf(obj);

            if (index >= 0)
            {
                list.RemoveAt <T>(index);
            }
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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));
            }
        }
示例#10
0
        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);
        }
示例#11
0
        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);
        }
示例#12
0
        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));
            }
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
0
        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);
        }
示例#16
0
        public static BigInteger Count(this StorageList list)
        {
            var result = list.Context.Get(CountKey(list.BaseKey)).AsBigInteger();

            return(result);
        }
示例#17
0
 public static bool Contains <T>(this StorageList list, T obj)
 {
     return(list.IndexOf(obj) >= 0);
 }
示例#18
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);
        }