/******************************************************** * Dictionary.h * * * * XFX Generic Dictionary class definition file * * Copyright © XFX Team. All Rights Reserved * ********************************************************/ #ifndef _SYSTEM_COLLECTIONS_GENERIC_DICTIONARY_ #define _SYSTEM_COLLECTIONS_GENERIC_DICTIONARY_ #include #include "EqualityComparer.h" #include "Interfaces.h" #include "KeyValuePair.h" namespace System { namespace Collections { namespace Generic { // Represents a collection of keys and values. template class Dictionary : public IDictionary, public ICollection >, virtual Object { private: template struct Entry { int hashCode; int next; UKey key; UValue value; }; private: int buckets[]; static const char* ComparerName; IEqualityComparer* comparer; int count; Entry entries[]; int freeCount; int freeList; static const char* HashSizeName; static const char* KeyValuePairsName; int version; static const char* VersionName; void Add(KeyValuePair keyValuePair); bool Contains(KeyValuePair keyValuePair); void CopyTo(KeyValuePair array[], int index); int FindEntry(TKey key); bool Remove(KeyValuePair keyValuePair); void Initialize(int capacity); void Insert(TKey key, TValue value, bool add); void Resize(); public: // Represents the collection of keys in a Dictionary<,>. template class KeyCollection : public ICollection, public IEnumerable { private: Dictionary* _dictionary; public: int Count(); KeyCollection(Dictionary dictionary); KeyCollection(const KeyCollection &obj); void Add(UKey item); void Clear(); bool Contains(UKey item); void CopyTo(UKey array[], int index); bool Remove(UKey item); }; // Represents the collection of values in a Dictionary<,>. template class ValueCollection : public ICollection, public IEnumerable { private: Dictionary *_dictionary; public: int Count(); ValueCollection(Dictionary dictionary); ValueCollection(const ValueCollection &obj); void Add(UValue item); void Clear(); bool Contains(UValue item); void CopyTo(UValue array[], int index); bool Remove(UValue item); }; public: IEqualityComparer* Comparer() { return comparer; } int Count(); KeyCollection Keys(); ValueCollection Values(); TValue operator[](TKey key); Dictionary(); Dictionary(IDictionary* dictionary) { /*foreach (KeyValuePair pair in dictionary) { Add(pair.Key, pair.Value); }*/ } Dictionary(int capacity); ~Dictionary(); void Add(TKey key, TValue value) { Insert(key, value, true); } void Clear(); bool ContainsKey(TKey key); bool ContainsValue(TValue value); bool Remove(TKey key); bool TryGetValue(TKey key, out TValue value); }; ////////////////////////////////////////////////////////////////////// template template int Dictionary::KeyCollection::Count() { return _dictionary->Count(); } template template Dictionary::KeyCollection::KeyCollection(Dictionary dictionary) { _dictionary = &dictionary; } template template Dictionary::KeyCollection::KeyCollection(const KeyCollection &obj) { _dictionary = obj._dictionary; } template template void Dictionary::KeyCollection::Add(UKey item) { //throw NotSupportedException("Adding keys directly to the Dictionary::Keycollection is not supported."); return; } template template void Dictionary::KeyCollection::Clear() { //throw NotSupportedException("Directly clearing the Dictionary::KeyCollection is not supported."); return; } template template bool Dictionary::KeyCollection::Contains(UKey item) { return _dictionary.ContainsKey(item); } template template void Dictionary::KeyCollection::CopyTo(UKey array[], int index) { if(array == null) { //throw ArgumentNullException("array"); return; } if((index < 0) ||(index > Array::Length(array))) { //throw ArgumentOutOfRangeException("index", "Non-negative array index required."); return; } if((Array::Length(array) - index) < _dictionary.Count()) { //throw ArgumentException("Array plus offset too small."); return; } int count = _dictionary.Count(); Entry entries[] = _dictionary.entries; for(int i = 0; i < count; i++) { if(entries[i].hashCode >= 0) { array[index++] = entries[i].key; } } } template template bool Dictionary::KeyCollection::Remove(UKey item) { //throw NotSupportedException("Removing keys directly from the Dictionary::KeyCollection is not supported."); return false; } template Dictionary::~Dictionary() { delete[] buckets; delete[] entries; } template void Dictionary::Clear() { if(count > 0) { for(int i = 0; i < Array::Length(buckets); i++) { buckets[i] = -1; } Array::Clear(entries, 0, count); freeList = -1; count = 0; freeCount = 0; version++; } } template bool Dictionary::Remove(TKey key) { if(buckets) { int num = comparer->GetHashCode(key) & 0x7fffffff; int index = num % Array::Length(buckets); int num3 = -1; for(int i = buckets[index]; i >= 0; i = entries[i].next) { if((entries[i].hashCode == num) && comparer->Equals(entries[i].key, key)) { if(num3 < 0) { buckets[index] = entries[i].next; } else { entries[num3].next = entries[i].next; } entries[i].hashCode = -1; entries[i].next = freeList; entries[i].key = TKey(); entries[i].value = TValue(); freeList = i; freeCount++; version++; return true; } num3 = i; } } return false; } template bool Dictionary::TryGetValue(TKey key, out TValue value) { int index = FindEntry(key); if(index >= 0) { value = entries[index].value; return true; } value = TValue(); return false; } } } } #endif //_SYSTEM_COLLECTIONS_GENERIC_DICTIONARY_