2 * Copyright (c) 1999-2005 Mark D. Hill and David A. Wood
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.
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.
37 #include "base/hashmap.hh"
38 #include "mem/gems_common/Vector.hh"
40 template <class KEY_TYPE, class VALUE_TYPE>
45 ~Map() { /* empty */ }
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;
55 VALUE_TYPE& lookup(const KEY_TYPE& key) const;
56 void clear() { m_map.clear(); }
57 void print(ostream& out) const;
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); }
65 // Use default copy constructor and assignment operator
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;
75 template <class KEY_TYPE, class VALUE_TYPE>
76 ostream& operator<<(ostream& out, const Map<KEY_TYPE, VALUE_TYPE>& map);
78 // *********************
80 template <class KEY_TYPE, class VALUE_TYPE>
81 void Map<KEY_TYPE, VALUE_TYPE>::add(const KEY_TYPE& key, const VALUE_TYPE& value)
83 // Update or add a new key/value pair
87 template <class KEY_TYPE, class VALUE_TYPE>
88 bool Map<KEY_TYPE, VALUE_TYPE>::exist(const KEY_TYPE& key) const
90 return (m_map.count(key) != 0);
93 template <class KEY_TYPE, class VALUE_TYPE>
94 VALUE_TYPE& Map<KEY_TYPE, VALUE_TYPE>::lookup(const KEY_TYPE& key) const
100 template <class KEY_TYPE, class VALUE_TYPE>
101 Vector<KEY_TYPE> Map<KEY_TYPE, VALUE_TYPE>::keys() const
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);
111 template <class KEY_TYPE, class VALUE_TYPE>
112 Vector<VALUE_TYPE> Map<KEY_TYPE, VALUE_TYPE>::values() const
114 Vector<VALUE_TYPE> values;
115 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
116 pair<KEY_TYPE, VALUE_TYPE> p;
118 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
120 values.insertAtBottom(p.second);
125 template <class KEY_TYPE, class VALUE_TYPE>
126 void Map<KEY_TYPE, VALUE_TYPE>::deleteKeys()
128 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
129 pair<KEY_TYPE, VALUE_TYPE> p;
131 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
137 template <class KEY_TYPE, class VALUE_TYPE>
138 void Map<KEY_TYPE, VALUE_TYPE>::deleteValues()
140 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
141 pair<KEY_TYPE, VALUE_TYPE> p;
143 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
149 template <class KEY_TYPE, class VALUE_TYPE>
150 void Map<KEY_TYPE, VALUE_TYPE>::print(ostream& out) const
152 typename hash_map<KEY_TYPE, VALUE_TYPE>::const_iterator iter;
153 pair<KEY_TYPE, VALUE_TYPE> p;
156 for (iter = m_map.begin(); iter != m_map.end(); iter++) {
157 // unparse each basic block
159 out << " " << p.first << "=" << p.second;
164 template <class KEY_TYPE, class VALUE_TYPE>
165 ostream& operator<<(ostream& out, const Map<KEY_TYPE, VALUE_TYPE>& map)