Пример #1
0
        /// <summary>
        /// Adds a new value to the dictionary
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        public void Add(string key, object value)
        {
            int hash = key.GetHashCode();

            DictionaryItem last = null;
            DictionaryItem node;

            for (node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    throw new ArgumentException("Duplicated key");
                }
                last = node;
            }

            DictionaryItem newNode = new DictionaryItem(hash, key, value);

            _count++;
            if (last == null)
            {
                _head = newNode;
            }
            else
            {
                last.next = newNode;
            }
        }
Пример #2
0
        /// <summary>
        /// Sets the value for the given key. The key is created if missing.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Set(string key, object value)
        {
            int hash = key.GetHashCode();

            DictionaryItem last = null;
            DictionaryItem node;

            for (node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    node.value = value;
                    return;
                }
                last = node;
            }

            DictionaryItem newNode = new DictionaryItem(hash, key, value);

            if (last == null)
            {
                _head = newNode;
            }
            else
            {
                last.next = newNode;
            }
            _count++;
        }
Пример #3
0
        internal void SetDefault(string key, bool value)
        {
            int hash = key.GetHashCode();

            DictionaryItem last = null, node;

            for (node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    return;
                }
                last = node;
            }

            DictionaryItem newNode = new DictionaryItem(hash, key, value);

            if (last == null)
            {
                _head = newNode;
            }
            else
            {
                last.next = newNode;
            }
            _count++;
        }
Пример #4
0
        /// <summary>
        /// Writes the values in an Excel sheet.
        /// </summary>
        /// <param name="target">Excel address, worksheet, range or null to create a new sheet.</param>
        /// <param name="clearFirst">Optional - If true, clears the cells first</param>
        public void ToExcel(object target = null, bool clearFirst = false)
        {
            IRange rg = ExcelExt.GetRange(target);

            if (clearFirst)
            {
                rg[rg.SpecialCells(XlCellType.xlCellTypeLastCell).Row, 1].Clear();
            }

            if (_count == 0)
            {
                return;
            }

            var values = new object[_count, 2];

            int i = 0;

            for (DictionaryItem node = _head; node != null; node = node.next, i++)
            {
                values[i, 0] = node.key;
                values[i, 1] = node.value;
            }

            rg[_count, 2].Value2 = values;
        }
Пример #5
0
        /// <summary>
        /// Returns true if the key is present, false otherwise.
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            int hash = key.GetHashCode();

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #6
0
        internal T GetValue <T>(string key, T defaultValue)
        {
            int hash = key.GetHashCode();

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    return((T)node.value);
                }
            }
            return(defaultValue);
        }
Пример #7
0
        /// <summary>
        /// Returns the value associated with the specified key or the default value if the key doesn't exist.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public object Get(string key, object defaultValue)
        {
            int hash = key.GetHashCode();

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    return(node.value);
                }
            }
            return(defaultValue);
        }
Пример #8
0
            /// <summary>
            /// Moves to the next item
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                if (_current == null)
                {
                    _current = _head;
                    return(_current != null);
                }
                if (_current.next == null)
                {
                    return(false);
                }

                _current = _current.next;
                return(true);
            }
Пример #9
0
        internal bool TryGetValue <T>(string key, out T value)
        {
            int hash = key.GetHashCode();

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    value = (T)node.value;
                    return(true);
                }
            }
            value = default(T);
            return(false);
        }
Пример #10
0
 /// <summary>
 /// Returns the value for the key
 /// </summary>
 /// <param name="key">Key</param>
 /// <returns></returns>
 public object this[string key] {
     get {
         int hash = key.GetHashCode();
         for (DictionaryItem node = _head; node != null; node = node.next)
         {
             if (hash == node.hash && key.Equals(node.key))
             {
                 return(node.value);
             }
         }
         throw new Errors.KeyNotFoundError(key);
     }
     set {
         this.Set(key, value);
     }
 }
Пример #11
0
        /// <summary>
        /// Creates a new Dictionary object
        /// </summary>
        /// <param name="d">System dictionary</param>
        public Dictionary(System.Collections.Generic.IDictionary <string, object> d)
        {
            DictionaryItem last = null;

            foreach (System.Collections.Generic.KeyValuePair <string, object> item in d)
            {
                DictionaryItem newNode = new DictionaryItem(item.Key, item.Value);
                if (last == null)
                {
                    _head = last = newNode;
                }
                else
                {
                    last.next = newNode;
                    last      = newNode;
                }
            }
            _count = d.Count;
        }
Пример #12
0
        /// <summary>
        /// Removes the value for the given key.
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>True if succeed, false otherwise</returns>
        public bool Remove(string key)
        {
            int            hash = key.GetHashCode();
            DictionaryItem last = null;

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    if (last == null)
                    {
                        _head = node.next;
                    }
                    else
                    {
                        last.next = node.next;
                    }
                    return(true);
                }
                last = node;
            }
            return(false);
        }
Пример #13
0
        /// <summary>
        /// Moves an item to a target dictionary.
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="target">Target dictionary</param>
        /// <returns>True if the key was found, false otherwise</returns>
        internal bool TryMove(string key, Dictionary target)
        {
            int            hash = key.GetHashCode();
            DictionaryItem last = null;

            for (DictionaryItem node = _head; node != null; node = node.next)
            {
                if (hash == node.hash && key.Equals(node.key))
                {
                    if (last == null)
                    {
                        _head = node.next;
                    }
                    else
                    {
                        last.next = node.next;
                    }
                    target.Add(key, node.value);
                    return(true);
                }
                last = node;
            }
            return(false);
        }
Пример #14
0
 /// <summary>
 /// Sets the enumerator to its initial position
 /// </summary>
 public void Reset()
 {
     _current = null;
 }
Пример #15
0
 public DictionaryItemDebugView(DictionaryItem item) {
     _item = item;
 }
Пример #16
0
 internal DictionaryItemEnumerator(DictionaryItem dict) {
     _dict = dict;
     _index = 0;
     _current = null;
 }
Пример #17
0
 internal DictionaryEnumerator(DictionaryItem node)
 {
     _head    = node;
     _current = null;
 }
Пример #18
0
 internal DictionaryItemEnumerator(DictionaryItem dict)
 {
     _dict    = dict;
     _index   = 0;
     _current = null;
 }
Пример #19
0
 /// <summary>
 /// Clears the dictionary
 /// </summary>
 public void Clear()
 {
     _head  = null;
     _count = 0;
 }
Пример #20
0
 public DictionaryItemDebugView(DictionaryItem item)
 {
     _item = item;
 }