Ejemplo n.º 1
0
        // Adds an element to the dictionary.
        public virtual bool Add(object key, object value)
        {
            // Creating an object with wanted key and value.
            DicPoint <K, T> dicPointBuffer = new DicPoint <K, T>((K)key, (T)value);

            // Getting index.
            int index = GetIndex(key);

            // If there are no objects with this index.
            if (Table[index] == null || Table[index].value == null)
            {
                DicPoint <K, T> buf = new DicPoint <K, T>((K)key, (T)value);
                Table[index] = buf;
            }
            else
            {
                // The first object in the list of this index.
                DicPoint <K, T> current = Table[index];

                // If there is the same object.
                if (current.Equals(dicPointBuffer))
                {
                    return(false);
                }

                // Finding the end of the list or the same object.
                while (current.next != null)
                {
                    if (current.next.Equals(dicPointBuffer))
                    {
                        return(false);
                    }
                    current = current.next;
                }

                // Adding the element to the table.
                current.next = dicPointBuffer;
            }

            // Adding the key and the value to the arrays and increasing the count.
            AddKey((K)key);
            AddValue((T)value);
            Count++;

            return(true);
        }