ruby: apply some fixes that were overwritten by the recent ruby import.
[gem5.git] / src / mem / gems_common / Map.hh
1 /*
2 * Copyright (c) 1999-2005 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * $Id$
31 *
32 */
33
34 #ifndef MAP_H
35 #define MAP_H
36
37 #include "base/hashmap.hh"
38 #include "mem/gems_common/Vector.hh"
39
40 template <class KEY_TYPE, class VALUE_TYPE>
41 class Map
42 {
43 public:
44 Map() { /* empty */ }
45 ~Map() { /* empty */ }
46
47 void add(const KEY_TYPE& key, const VALUE_TYPE& value);
48 bool exist(const KEY_TYPE& key) const;
49 int size() const { return m_map.size(); }
50 void erase(const KEY_TYPE& key) { assert(exist(key)); m_map.erase(key); }
51 Vector<KEY_TYPE> keys() const;
52 Vector<VALUE_TYPE> values() const;
53 void deleteKeys();
54 void deleteValues();
55 VALUE_TYPE& lookup(const KEY_TYPE& key) const;
56 void clear() { m_map.clear(); }
57 void print(ostream& out) const;
58
59 // Synonyms
60 void remove(const KEY_TYPE& key) { erase(key); }
61 void deallocate(const KEY_TYPE& key) { erase(key); }
62 void allocate(const KEY_TYPE& key) { add(key, VALUE_TYPE()); }
63 void insert(const KEY_TYPE& key, const VALUE_TYPE& value) { add(key, value); }
64
65 // Use default copy constructor and assignment operator
66 private:
67 // Data members
68
69 // m_map is declared mutable because some methods from the STL "map"
70 // class that should be const are not. Thus we define this as
71 // mutable so we can still have conceptually const accessors.
72 mutable m5::hash_map<KEY_TYPE, VALUE_TYPE> m_map;
73 };
74
75 template <class KEY_TYPE, class VALUE_TYPE>
76 ostream& operator<<(ostream& out, const Map<KEY_TYPE, VALUE_TYPE>& map);
77
78 // *********************
79
80 template <class KEY_TYPE, class VALUE_TYPE>
81 void Map<KEY_TYPE, VALUE_TYPE>::add(const KEY_TYPE& key, const VALUE_TYPE& value)
82 {
83 // Update or add a new key/value pair
84 m_map[key] = value;
85 }
86
87 template <class KEY_TYPE, class VALUE_TYPE>
88 bool Map<KEY_TYPE, VALUE_TYPE>::exist(const KEY_TYPE& key) const
89 {
90 return (m_map.count(key) != 0);
91 }
92
93 template <class KEY_TYPE, class VALUE_TYPE>
94 VALUE_TYPE& Map<KEY_TYPE, VALUE_TYPE>::lookup(const KEY_TYPE& key) const
95 {
96 assert(exist(key));
97 return m_map[key];
98 }
99
100 template <class KEY_TYPE, class VALUE_TYPE>
101 Vector<KEY_TYPE> Map<KEY_TYPE, VALUE_TYPE>::keys() const
102 {
103 Vector<KEY_TYPE> keys;
104 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
105 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
106 keys.insertAtBottom((*iter).first);
107 }
108 return keys;
109 }
110
111 template <class KEY_TYPE, class VALUE_TYPE>
112 Vector<VALUE_TYPE> Map<KEY_TYPE, VALUE_TYPE>::values() const
113 {
114 Vector<VALUE_TYPE> values;
115 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
116 pair<KEY_TYPE, VALUE_TYPE> p;
117
118 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
119 p = *iter;
120 values.insertAtBottom(p.second);
121 }
122 return values;
123 }
124
125 template <class KEY_TYPE, class VALUE_TYPE>
126 void Map<KEY_TYPE, VALUE_TYPE>::deleteKeys()
127 {
128 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
129 pair<KEY_TYPE, VALUE_TYPE> p;
130
131 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
132 p = *iter;
133 delete p.first;
134 }
135 }
136
137 template <class KEY_TYPE, class VALUE_TYPE>
138 void Map<KEY_TYPE, VALUE_TYPE>::deleteValues()
139 {
140 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
141 pair<KEY_TYPE, VALUE_TYPE> p;
142
143 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
144 p = *iter;
145 delete p.second;
146 }
147 }
148
149 template <class KEY_TYPE, class VALUE_TYPE>
150 void Map<KEY_TYPE, VALUE_TYPE>::print(ostream& out) const
151 {
152 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
153 pair<KEY_TYPE, VALUE_TYPE> p;
154
155 out << "[";
156 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
157 // unparse each basic block
158 p = *iter;
159 out << " " << p.first << "=" << p.second;
160 }
161 out << " ]";
162 }
163
164 template <class KEY_TYPE, class VALUE_TYPE>
165 ostream& operator<<(ostream& out, const Map<KEY_TYPE, VALUE_TYPE>& map)
166 {
167 map.print(out);
168 return out;
169 }
170
171 #endif //MAP_H