// Helper method used to add an item to the array. If the // key already exists, the existing item is replaced. If the // array is full, the array size is increased. protected void AddToArray(string key, object item) { if (KeyExists(key)) { // Scroll through the item array, and replace the // existing item associated with the key with the // new item. for (int n = 0; n < _count; ++n) { KeyItemPair pair = (KeyItemPair)_items[n]; if (key == pair.key) { _items[n] = new KeyItemPair(key, item); } } } else { if (_count == _items.Length) { IncreaseCapacity(); } _items[_count] = new KeyItemPair(key, item); _count++; } }
// Given a key in the array, returns the associated object, or // returns null if the key isn't found. protected object KeyToObject(string key) { for (int n = 0; n < _count; ++n) { KeyItemPair pair = (KeyItemPair)_items[n]; if (key == pair.key) { return(pair.item); } } return(null); }
// Returns true if a specific key exists in the array; // otherwise, return false. protected bool KeyExists(string key) { for (int n = 0; n < _count; ++n) { KeyItemPair pair = (KeyItemPair)_items[n]; if (key == pair.key) { return(true); } } return(false); }
// Given a key in the array, returns the associated object, or // returns null if the key isn't found. protected int KeyToIndex(string key) { for (int n = 0; n < _count; ++n) { KeyItemPair pair = (KeyItemPair)_items[n]; if (key == pair.key) { return(n); } } return(-1); }
/*Find the value of key of the Dictionary*/ public Item Find(int key) { int pos = GetIndex(key); KeyItemPair pareja = Find(buckets[pos], key); if (pareja == null) { return(default(Item)); } else { return(pareja.item); } }
/*Add key with the value to the Dictionary*/ public void Add(int key, Item value) { if (size >= buckets.Length) { const int FACTOR_CRECIMIENTO = 2; ResizeAndReindex(buckets.Length * FACTOR_CRECIMIENTO); } int pos = GetIndex(key); // Console.WriteLine("Posicion de key {0} es {1}", key, pos); KeyItemPair pareja = new KeyItemPair(key, value); if (Find(buckets[pos], key) != null) { throw new DuplicaKey(); } buckets[pos].AddLast(pareja); size++; }
// Helper method used to add an item to the array. If the // key already exists, the existing item is replaced. If the // array is full, the array size is increased. protected void AddToArray(string key, object item) { if(KeyExists(key)) { // Scroll through the item array, and replace the // existing item associated with the key with the // new item. for(int n = 0; n < _count; ++n) { KeyItemPair pair = (KeyItemPair)_items[n]; if(key == pair.key) _items[n] = new KeyItemPair(key, item); } } else { if(_count == _items.Length) { IncreaseCapacity(); } _items[_count] = new KeyItemPair(key, item); _count++; } }