1 /* 2 * tArrayMap.h 3 * Avida 4 * 5 * Created by David on 1/11/09. 6 * Copyright 2009-2011 Michigan State University. All rights reserved. 7 * 8 * This file is part of Avida. 9 * 10 * Avida is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 11 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 12 * 13 * Avida is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License along with Avida. 17 * If not, see <http://www.gnu.org/licenses/>. 18 * 19 */ 20 21 #ifndef tArrayMap_h 22 #define tArrayMap_h 23 24 #ifndef tArray_h 25 #include "tArray.h" 26 #endif 27 #ifndef tKVPair_h 28 #include "tKVPair.h" 29 #endif 30 31 32 template<class KeyType, class ValueType> class tArrayMap 33 { 34 private: 35 tArray<tKVPair<KeyType, ValueType> > m_map; 36 37 public: tArrayMap()38 tArrayMap() { ; } tArrayMap(const tArrayMap & am)39 tArrayMap(const tArrayMap& am) : m_map(am.m_map) { ; } 40 GetSize()41 int GetSize() const { return m_map.GetSize(); } 42 Clear()43 void Clear() { m_map.Resize(0); } 44 Set(const KeyType & key,const ValueType & value)45 void Set(const KeyType& key, const ValueType& value) 46 { 47 for (int i = 0; i < m_map.GetSize(); i++) { 48 if (m_map[i].Key() == key) { 49 m_map[i].Value() = value; 50 return; 51 } 52 } 53 m_map.Push(tKVPair<KeyType, ValueType>(key, value)); 54 } 55 Get(const KeyType & key,ValueType & out_value)56 bool Get(const KeyType& key, ValueType& out_value) const 57 { 58 for (int i = 0; i < m_map.GetSize(); i++) { 59 if (m_map[i].Key() == key) { 60 out_value = m_map[i].Value(); 61 return true; 62 } 63 } 64 return false; 65 } 66 GetWithDefault(const KeyType & key,const ValueType & default_value)67 const ValueType& GetWithDefault(const KeyType& key, const ValueType& default_value) 68 { 69 for (int i = 0; i < m_map.GetSize(); i++) { 70 if (m_map[i].Key() == key) return m_map[i].Value(); 71 } 72 m_map.Push(tKVPair<KeyType, ValueType>(key, default_value)); 73 return m_map[m_map.GetSize() - 1].Value(); 74 } 75 ValueFor(const KeyType & key)76 ValueType& ValueFor(const KeyType& key) 77 { 78 for (int i = 0; i < m_map.GetSize(); i++) { 79 if (m_map[i].Key() == key) { 80 return m_map[i].Value(); 81 } 82 } 83 m_map.Push(tKVPair<KeyType, ValueType>(key)); 84 return m_map[m_map.GetSize() - 1].Value(); 85 } 86 ValueFor(const KeyType & key)87 const ValueType& ValueFor(const KeyType& key) const 88 { 89 for (int i = 0; i < m_map.GetSize(); i++) { 90 if (m_map[i].Key() == key) { 91 return m_map[i].Value(); 92 } 93 } 94 m_map.Push(tKVPair<KeyType, ValueType>(key)); 95 return m_map[m_map.GetSize() - 1].Value(); 96 } 97 98 ValueType& operator[](const KeyType& key) { return ValueFor(key); } 99 const ValueType& operator[](const KeyType& key) const { return ValueFor(key); } 100 GetKeys()101 tArray<KeyType> GetKeys() const 102 { 103 tArray<KeyType> keys(m_map.GetSize()); 104 for (int i = 0; i < m_map.GetSize(); i++) keys[i] = m_map[i].Key(); 105 return keys; 106 } 107 Remove(const KeyType & key)108 void Remove(const KeyType& key) 109 { 110 for (int i = 0; i < m_map.GetSize(); i++) { 111 if (m_map[i].Key() == key) { 112 int lastkv = m_map.GetSize() - 1; 113 if (i != lastkv) m_map[i] = m_map[lastkv]; 114 m_map.Resize(lastkv); 115 } 116 } 117 } 118 Remove(const KeyType & key,ValueType & out_value)119 bool Remove(const KeyType& key, ValueType& out_value) 120 { 121 for (int i = 0; i < m_map.GetSize(); i++) { 122 if (m_map[i].Key() == key) { 123 int lastkv = m_map.GetSize() - 1; 124 out_value = m_map[i].Value(); 125 if (i != lastkv) m_map[i] = m_map[lastkv]; 126 m_map.Resize(lastkv); 127 return true; 128 } 129 } 130 return false; 131 } 132 133 134 typedef typename tArray<tKVPair<KeyType, ValueType> >::iterator iterator; 135 typedef typename tArray<tKVPair<KeyType, ValueType> >::const_iterator const_iterator; 136 begin()137 inline iterator begin() { return m_map.begin(); } end()138 inline iterator end() { return m_map.end(); } begin()139 inline const_iterator begin() const { return m_map.begin(); } end()140 inline const_iterator end() const { return m_map.end(); } 141 }; 142 143 #endif 144