Beispiel #1
0
        public void Add(ItemMap <TKey, TValue> item)
        {
            var hash = GetHash(item.Key);

            if (Keys.Contains(item.Key))
            {
                return;
            }

            if (Items[hash] == null)
            {
                Keys.Add(item.Key);
                Items[hash] = item;
            }
            else
            {
                var placed = false;
                for (var i = hash; i < size; i++)
                {
                    if (Items[i] == null)
                    {
                        Keys.Add(item.Key);
                        Items[i] = item;
                        placed   = true;
                        break;
                    }

                    if (Items[i].Key.Equals(item.Key))
                    {
                        return;
                    }
                }

                if (!placed)
                {
                    for (var i = 0; i < hash; i++)
                    {
                        if (Items[i] == null)
                        {
                            Keys.Add(item.Key);
                            Items[i] = item;
                            placed   = true;
                            break;
                        }

                        if (Items[i].Key.Equals(item.Key))
                        {
                            return;
                        }
                    }
                }

                if (!placed)
                {
                    throw new Exception("Словарь заполнен");
                }
            }
        }
Beispiel #2
0
        public void Add(ItemMap <TKey, TValue> item)
        {
            var hash = GetHash(item.Key);

            if (Keys.Contains(item.Key))
            {
                return;
            }

            if (Items[hash] == null)
            {
                Keys.Add(item.Key);
                Items[hash] = item;
            }
            else
            {
                List <int> help = new List <int>();
                while (true)
                {
                    Random rand    = new Random();
                    int    NewHash = rand.Next(0, size - 1);
                    if (!help.Contains(NewHash))
                    {
                        help.Add(NewHash);
                    }
                    if (Items[NewHash] == null)
                    {
                        Items[NewHash] = item;
                        Keys.Add(item.Key);
                        break;
                    }

                    if (help.Count >= size - Items.Length)
                    {
                        throw new Exception("Словарь заполнен");
                    }
                }
            }
        }