예제 #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;
 }