File: stringHashMap.h

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 239,928 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (244 lines) | stat: -rw-r--r-- 4,735 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: stringHashMap.h,v 1.25 2004/05/06 21:39:37 oliver Exp $
//

#ifndef BALL_DATATYPE_STRINGHASHMAP_H
#define BALL_DATATYPE_STRINGHASHMAP_H

#ifndef BALL_COMMON_H
#	include <BALL/common.h>
#endif

#ifndef BALL_COMMON_HASH_H
#	include <BALL/COMMON/hash.h>
#endif

#ifndef BALL_CONCEPT_VISITOR_H
#	include <BALL/CONCEPT/visitor.h>
#endif

#ifndef BALL_CONCEPT_PROCESSOR_H
#	include <BALL/CONCEPT/processor.h>
#endif

#ifndef BALL_DATATYPE_HASHMAP_H
#	include <BALL/DATATYPE/hashMap.h>
#endif

#include <algorithm>

namespace BALL 
{


	/** StringHashMap
	    \ingroup  GenericHash
	*/
	template <typename Value>
	class StringHashMap
		:	public HashMap<String, Value>
	{
		public:

		BALL_CREATE(StringHashMap)

		/**	@name Type Definitions
		*/
		//@{

		/**	Iterator type
		*/
		typedef typename HashMap<String, Value>::Iterator Iterator;

		/**	Const iterator type
		*/
		typedef typename HashMap<String, Value>::ConstIterator ConstIterator;	
		
		/**	Value type
		*/
		typedef typename HashMap<String, Value>::ValueType ValueType;	
		//@}

		/**	@name	Constructors and Destructors */
		//@{

		/**	Default constructor.
				Create an empty hash map.
		*/
		StringHashMap()
			: HashMap<String, Value>()
		{
		}

		/** Copy constructor.
				Create a copy of an existing hash map.
				@param	map the hash map to be copied
		*/
		StringHashMap(const StringHashMap& map)
			: HashMap<String, Value>(map)
		{
		}
			
		/** Destructor.
				Destruct the hash map and free all used memory.
		*/
		virtual ~StringHashMap()
		{
		}

		/** Clear the hash map.
				Remove all contents from the hash map.
		*/
		void destroy()
		{
			HashMap<String, Value>::clear();
		}
	
		//@}

		/**	@name	Assignment */
		//@{
			
		/** Assign a hash map from another.
				Create a copy of a hash map.
				@param	hash_map	the map to be copied
		*/
    void set(const StringHashMap& hash_map)
		{
			HashMap<String, Value>::clear();

			ConstIterator it = hash_map.begin();
			for ( ; it != hash_map.end(); ++it)
			{
				insert(*it);
			}
		}

		/// Assign a hash map from another
		const StringHashMap& operator = (const StringHashMap& hash_map)
		{
			set(hash_map);
			return *this;
		}
			
		/// Assigns the content of a hash map to another
    void get(StringHashMap& hash_map) const
		{
			hash_map.set(*this);
		}

		/// Swaps the contents of two hash maps
    void swap(StringHashMap& hash_map)
		{
			std::swap(*this, hash_map);
		}

		//@}

		/**	@name Accessors */
		//@{

		/**	Insert a pair of key and value.
		*/
		std::pair<Iterator, bool> insert(const ValueType& obj)
		{
			return HashMap<String, Value>::insert(obj);
		}
		
		/**	Insert a given value and key.
				@param	value the value to be inserted
				@param	key the value`s key
		*/
		::std::pair<Iterator, bool> insert(const String& key, const Value& value)
		{
			return HashMap<String, Value>::insert(::std::pair<String, Value>(key, value));
		}

			
		/**	Remove the entry <tt>key</tt> from the map.
				@param	key the key of the entry to be removed
				@return	bool <b>true</b> if the key was removed
		*/
		bool remove(const String& key)
		{
			// search the key
			Iterator it = HashMap<String, Value>::find(key);
			if (it == HashMap<String, Value>::end())
			{
				// we didn't find it..
				return false;
			}
			
			// found it: delete it
			HashMap<String, Value>::erase(it);

			return true;
		}

		/** Return the size of the hash map.
		*/
		Size getSize() const
		{
			return HashMap<String, Value>::size();
		}
		
		//@}

		/**	@name	Predicates */
		//@{

		/**	Compare two string hash maps.
		*/
		bool operator == (const StringHashMap<Value>& hash_map) const
		{
			return HashMap<String, Value>::operator == (hash_map);
		}

		/**	Compare two string hash maps.
		*/
		bool operator != (const StringHashMap<Value>& hash_map) const
		{
			return !(HashMap<String, Value>::operator == (hash_map));
		}

		/** Decide whether the hash map contains a given key.
		*/
		bool has(const String& key) const
		{
			return !(HashMap<String, Value>::find(key) == HashMap<String, Value>::end());
		}

		/** Return true if the hash map is empty.
				This method return <b>true</b> if the hash map does not contain any entries.
		*/
		bool isEmpty() const
		{
			return (HashMap<String, Value>::size() == 0);
		}
		//@}

		/**	@name	Miscellaneous */
		//@{

		/**	Visitor host method.
				StringHashMaps may be visited.
				@param	visitor	the visitor
		*/
		void host(Visitor<StringHashMap<Value> >& visitor)
		{
			visitor.visit(*this);  
		}
		//@}

	};

// required for visual studio
#ifdef BALL_COMPILER_MSVC
template class BALL_EXPORT StringHashMap<String>;
#endif

}// namespace BALL

#endif // BALL_DATATYPE_HASHMAP_H