Exemple #1
0
        public void Delete(string key)
        {
            var hash = h(k(key));

            HashTableItem prevItem = null;
            var           item     = hashTableItems[hash];

            while (item != null)
            {
                if (item.key == key)
                {
                    break;
                }

                prevItem = item;
                item     = item.nextItem;
            }

            if (item != null)
            {
                if (prevItem == null)
                {
                    hashTableItems[hash] = item.nextItem;
                }
                else
                {
                    prevItem.nextItem = item.nextItem;
                }

                if (item.prevItemLinear != null)
                {
                    item.prevItemLinear.nextItemLinear = item.nextItemLinear;
                }
                if (item.nextItemLinear != null)
                {
                    item.nextItemLinear.prevItemLinear = item.prevItemLinear;
                }

                //крайний правый элемент// item == lastAddedItem)
                if (item.nextItemLinear == null && item.prevItemLinear != null)
                {
                    lastAddedItem = item.prevItemLinear;
                    lastAddedItem.nextItemLinear = null;
                }

                //крайний левый элемент
                if (item.prevItemLinear == null && item.nextItemLinear != null)
                {
                    item.nextItemLinear.prevItemLinear = null;
                }

                //единственный элемент
                if (item.prevItemLinear == null && item.nextItemLinear == null)
                {
                    lastAddedItem = null;
                }

                item = null;
            }
        }
Exemple #2
0
        public void Put(string key, string value)
        {
            var newItem = new HashTableItem {
                key = key, value = value
            };
            var hash = h(k(key));

            var found = false;

            if (hashTableItems[hash] == null)
            {
                hashTableItems[hash] = newItem;
            }
            else
            {
                var item = hashTableItems[hash];

                while (item != null)
                {
                    if (item.key == key)
                    {
                        found = true;
                        break;
                    }

                    item = item.nextItem;
                }

                if (!found)
                {
                    newItem.nextItem     = hashTableItems[hash];
                    hashTableItems[hash] = newItem;
                }
                else
                {
                    item.value = value;
                }
            }

            if (!found)
            {
                if (lastAddedItem != null)
                {
                    lastAddedItem.nextItemLinear = newItem;
                    newItem.prevItemLinear       = lastAddedItem;
                    lastAddedItem = newItem;
                }
                else
                {
                    lastAddedItem = newItem;
                }
            }
        }