Exemple #1
0
        public bool Contains(K key)
        {
            Guarder.CheckNull(key);
            var found = false;
            var node  = root;

            while (null != node)
            {
                var cmp = Comparer(key, node.Item.Key);
                if (cmp == 0)
                {
                    found = true;
                    break;
                }
                else if (cmp < 0)
                {
                    node = node.Left;
                }
                else // cmp > 0
                {
                    node = node.Right;
                }
            }

            return(found);
        }
        public virtual bool ContainsKey(K key)
        {
            Guarder.CheckNull(key);
            var entry = GetEntry(key);

            return(entry != null);
        }
        public virtual bool Remove(K key)
        {
            Guarder.CheckNull(key);
            var removed = false;
            var index   = HashIndex(key);
            var entry   = Entries[index];

            if (null != entry)
            {
                if (Equator.Equals(entry.Key, key))
                {
                    Entries[index] = entry.Next;
                    removed        = true;
                }
                else
                {
                    while (null != entry.Next)
                    {
                        var item = entry.Next;
                        if (Equator.Equals(item.Key, key))
                        {
                            entry.Next = item.Next;
                            removed    = true;
                            break;
                        }
                        entry = item;
                    }
                }
            }
            if (removed)
            {
                Count--;
            }
            return(removed);
        }
Exemple #4
0
        public KeyValuePair <K, V> Floor(K key)
        {
            Guarder.CheckNull(key);
            ValidateNotEmpty();
            var node  = root;
            var found = false;

            while (null != node)
            {
                var cmp = Comparer(key, node.Item.Key);
                if (cmp >= 0)
                {
                    if (node.Right == null || Comparer(key, Minimum(node.Right).Item.Key) < 0)
                    {
                        found = true;
                        break;
                    }
                    node = node.Right;
                }
                else
                {
                    node = node.Left;
                }
            }

            if (!found)
            {
                throw new ArgumentException(string.Format("No item is the floor of the key: {0}", key));
            }

            return(node.Item);
        }
Exemple #5
0
        public bool Remove(K key)
        {
            Guarder.CheckNull(key);
            var index = HashIndex(key);
            var found = false;

            while (entries[index].Occupied)
            {
                if (!entries[index].Deleted && comparer.Equals(entries[index].Key, key))
                {
                    entries[index].Deleted = true;
                    entries[index].Key     = default(K);
                    entries[index].Value   = default(V);
                    Count--;
                    found = true;
                    break;
                }
                index = (index + 1) & (capacity - 1);
            }
            if (!found)
            {
                index++;
            }

            return(found);
        }
Exemple #6
0
 public V this[K key]
 {
     get
     {
         Guarder.CheckNull(key);
         var found = GetNode(root, key);
         if (null == found)
         {
             throw new KeyNotFoundException(string.Format("The key {0} does not exist in the map.", key));
         }
         return(found.Item.Value);
     }
     set
     {
         Guarder.CheckNull(key);
         var found = GetNode(root, key);
         if (null == found)
         {
             Add(key, value);
         }
         else
         {
             found.Item = new KeyValuePair <K, V>(key, value);
         }
     }
 }
Exemple #7
0
        public bool ContainsKey(K key)
        {
            Guarder.CheckNull(key);
            var index = FindEntry(key);
            var entry = entries[index];

            return(entry.Occupied);
        }
        protected virtual V Get(K key)
        {
            Guarder.CheckNull(key);
            var entry = GetEntry(key);

            entry.Validate(x => x != null, new KeyNotFoundException(string.Format(Messages.KeyDoesNotExistInMap, key)));
            return(entry.Value);
        }
Exemple #9
0
        public static string ToJson <T>(T target, Transformer <T, JValue> transform)
        {
            Guarder.CheckNull(transform, "transform");
            Guarder.CheckNull(target, "target");
            var value = transform(target);

            return(value.ToString());
        }
Exemple #10
0
 public void CopyTo(KeyValuePair <K, V>[] array, int arrayIndex)
 {
     Guarder.CheckNull(array);
     foreach (var item in this)
     {
         array[arrayIndex++] = item;
     }
 }
Exemple #11
0
        public static string ToJson <T>(T target, IObjectConverter <T> converter)
        {
            Guarder.CheckNull(converter, "converter");
            Guarder.CheckNull(target, "target");
            var value = converter.Convert(target);

            return(value.ToString());
        }
Exemple #12
0
        public static T To <T>(string json, IJsonConverter <T> converter)
        {
            Guarder.CheckNull(converter, "converter");
            var engine = new ParseEngine();
            var value  = engine.Parse(json);

            return(converter.Convert(value));
        }
        public bool ContainsKey(K key)
        {
            Guarder.CheckNull(key, "key");
            LockableNode pred, curr;

            Find(key, out pred, out curr);
            return(!IsNotFound(pred, curr) && (comparer.Compare(curr.Key, key) == 0 && !curr.Marked));
        }
Exemple #14
0
        public static T To <T>(string json, Transformer <JValue, T> transform)
        {
            Guarder.CheckNull(transform, "transform");
            var engine = new ParseEngine();
            var value  = engine.Parse(json);

            return(transform(value));
        }
 public ConcurrentListBasedMap(IComparer <K> comparer)
 {
     Guarder.CheckNull(comparer, "comparer");
     this.comparer = comparer;
     head          = new LockableNode();
     tail          = new LockableNode();
     head.Next     = tail;
 }
 protected AbstractHashedMap(int capacity, IEqualityComparer <K> equator)
 {
     capacity.Validate(x => x > 0, new ArgumentException("Capacity must be larger than 0."));
     Guarder.CheckNull(equator);
     Count    = 0;
     Capacity = CalculateCapacity(capacity);
     Equator  = equator;
     Entries  = new HashEntry[this.Capacity];
 }
 public ConcurrentSortedList(IComparer <T> comparer)
 {
     Guarder.CheckNull(comparer, "comparer");
     head          = new Node();
     tail          = new Node();
     head.Next     = new AtomicMarkableReference <Node>(tail);
     tail.Next     = new AtomicMarkableReference <Node>(null);
     this.comparer = comparer;
 }
Exemple #18
0
 public void Add(K key, V value)
 {
     Guarder.CheckNull(key);
     if (Count >= threshold)
     {
         Inflate();
     }
     Enroute(key, value);
 }
        public void Add(T key)
        {
            Guarder.CheckNull(key, "key");
            var spin = new SpinWait();

            while (!Insert(key))
            {
                spin.SpinOnce();
            }
        }
        public virtual void CopyTo(KeyValuePair <K, V>[] array, int arrayIndex)
        {
            Guarder.CheckNull(array);
            var index = 0;

            foreach (var entry in this)
            {
                array[arrayIndex + (index++)] = entry;
            }
        }
Exemple #21
0
        /// <summary>
        /// Copies the items in the set to an array, start from the arrayIndex.
        /// </summary>
        /// <param name="array">The array to which items are copied.</param>
        /// <param name="arrayIndex">The index to start to copy the items.</param>
        public override void CopyTo(T[] array, int arrayIndex)
        {
            Guarder.CheckNull(array);
            var index = arrayIndex;

            foreach (var i in llrbTree)
            {
                array[index++] = i.Key;
            }
        }
Exemple #22
0
        public bool TryGetValue(K key, out V value)
        {
            Guarder.CheckNull(key);
            var index = FindEntry(key);
            var entry = entries[index];

            value = entry.Value;

            return(entry.Occupied);
        }
Exemple #23
0
 public IJsonObjectMapper <T> With(string jsonKey)
 {
     Guarder.CheckNull(jsonKey, "jsonKey");
     if (lastExp == null)
     {
         throw new InvalidOperationException(Messages.NoPropertyToMap);
     }
     Map(jsonKey, lastExp);
     lastExp = null;
     return(this);
 }
        public virtual bool Remove(T item)
        {
            Guarder.CheckNull(item);
            var removed = false;

            if (Map.ContainsKey(item))
            {
                Map.Remove(item);
                removed = true;
            }
            return(removed);
        }
Exemple #25
0
 public HashedMap(int capacity, IEqualityComparer <K> comparer)
 {
     Guarder.CheckNull(comparer);
     if (capacity <= 0)
     {
         throw new ArgumentException("The capacity must be larger than zero.");
     }
     this.comparer = comparer;
     this.capacity = CalculateCapacity(capacity);
     Count         = 0;
     entries       = new Entry[this.capacity];
 }
 public Customized32HashedMap(IDictionary <K, V> items, IHashStrategy hasher, Transformer <K, byte[]> transformer, Equator <K> isEqual)
     : this(items == null ? DefaultCapacity : items.Count, hasher, transformer, isEqual)
 {
     Guarder.CheckNull(hasher, transformer, isEqual);
     if (null != items)
     {
         foreach (var item in items)
         {
             Add(item.Key, item.Value);
         }
     }
 }
 public virtual int this[T item]
 {
     get
     {
         Guarder.CheckNull(item);
         if (!Map.ContainsKey(item))
         {
             throw new ArgumentException("The item does not exist in the bag");
         }
         return(Map[item]);
     }
 }
Exemple #28
0
        public void CopyTo(KeyValuePair <K, V>[] array, int arrayIndex)
        {
            Guarder.CheckNull(array);
            var index = 0;

            foreach (var element in entries)
            {
                if (element.Occupied)
                {
                    array[arrayIndex + (index++)] = new KeyValuePair <K, V>(element.Key, element.Value);
                }
            }
        }
Exemple #29
0
        void ICollection.CopyTo(Array array, int index)
        {
            Guarder.CheckNull(array);
            var theArray = (T[])array;

            Guarder.CheckNull(theArray);
            var cursor = 0;

            for (var i = head; i <= tail; i++)
            {
                theArray[index + (cursor++)] = items[i];
            }
        }
        protected virtual void Set(K key, V v)
        {
            Guarder.CheckNull(key);
            var entry = GetEntry(key);

            if (entry == null)
            {
                Add(key, v);
            }
            else
            {
                entry.Value = v;
            }
        }