File: StringMap.h

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (231 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download
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
/*
 *  Phusion Passenger - https://www.phusionpassenger.com/
 *  Copyright (c) 2011-2013 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
#ifndef _PASSENGER_STRING_MAP_H_
#define _PASSENGER_STRING_MAP_H_

#include <string>
#include <map>
#include <utility>

#include <StaticString.h>
#include <Utils/HashMap.h>

namespace Passenger {

using namespace std;


/**
 * An efficient map with string keys. map<string, T> forces one to construct an
 * std::string object when looking up the map. StringMap interns all keys and
 * allows lookups without constructing an std::string key.
 *
 * StringMap requires the following properties on T:
 * - T's default constructor must be cheap, otherwise set() can be a bit slow.
 * - T must support operator=().
 */
template<typename T>
class StringMap {
private:
	struct Entry {
		string key;
		pair<StaticString, T> thePair;
	};
	
	typedef HashMap<StaticString, Entry, StaticString::Hash> InternalMap;
	typedef typename InternalMap::iterator InternalIterator;
	typedef typename InternalMap::const_iterator InternalConstIterator;
	typedef typename InternalMap::value_type ValueType;
	InternalMap store;
	
public:
	class const_iterator {
	private:
		InternalConstIterator it;
		
	public:
		const_iterator() { }
		
		const_iterator(const InternalConstIterator &_it)
			: it(_it)
			{ }
		
		const_iterator &operator=(const const_iterator &value) {
			it = value.it;
			return *this;
		}
		
		const_iterator &operator++() {
			it++;
			return *this;
		}
		
		const_iterator operator++(int) {
			const_iterator copy(*this);
			operator++();
			return copy;
		}
		
		bool operator==(const const_iterator &other) {
			return it == other.it;
		}
		
		bool operator!=(const const_iterator &other) {
			return it != other.it;
		}
		
		const pair<const StaticString, const T> &operator*() {
			return (pair<const StaticString, const T> &) it->second.thePair;
		}
		
		const pair<const StaticString, const T> *operator->() {
			return &(**this);
		}
	};
	
	class iterator {
	private:
		InternalIterator it;
		
	public:
		iterator() { }
		
		iterator(const InternalIterator &_it)
			: it(_it)
			{ }
		
		iterator &operator=(const iterator &value) {
			it = value.it;
			return *this;
		}
		
		iterator &operator++() {
			it++;
			return *this;
		}
		
		iterator operator++(int) {
			iterator copy(*this);
			operator++();
			return copy;
		}
		
		bool operator==(const iterator &other) {
			return it == other.it;
		}
		
		bool operator!=(const iterator &other) {
			return it != other.it;
		}
		
		pair<StaticString, T> &operator*() {
			return it->second.thePair;
		}
		
		pair<StaticString, T> *operator->() {
			return &(**this);
		}
		
		operator const_iterator() const {
			return const_iterator(it);
		}
	};
	
	T get(const StaticString &key) const {
		InternalConstIterator it = store.find(key);
		if (it == store.end()) {
			return T();
		} else {
			return it->second.thePair.second;
		}
	}

	T get(const StaticString &key, const T &defaultValue) const {
		InternalConstIterator it = store.find(key);
		if (it == store.end()) {
			return defaultValue;
		} else {
			return it->second.thePair.second;
		}
	}

	bool has(const StaticString &key) const {
		return store.find(key) != store.end();
	}
	
	bool set(const StaticString &key, const T &value) {
		pair<InternalIterator, bool> result = store.insert(make_pair(key, Entry()));
		if (result.second) {
			// Key has been inserted. Copy it internally and point key
			// to the copy.
			ValueType &node = *result.first;
			StaticString &originalKey = const_cast<StaticString &>(node.first);
			Entry &entry = node.second;
			entry.key = key;
			entry.thePair.first = entry.key;
			entry.thePair.second = value;
			originalKey = entry.key;
			return true;
		} else {
			// Key already exists. Update value.
			Entry &entry = result.first->second;
			entry.thePair.second = value;
			return false;
		}
	}
	
	bool remove(const StaticString &key) {
		return store.erase(key) > 0;
	}
	
	unsigned int size() const {
		return store.size();
	}
	
	bool empty() const {
		return store.empty();
	}
	
	iterator begin() {
		return iterator(store.begin());
	}
	
	const_iterator begin() const {
		return const_iterator(store.begin());
	}
	
	iterator end() {
		return iterator(store.end());
	}
	
	const_iterator end() const {
		return const_iterator(store.end());
	}
};


} // namespace Passenger

#endif /* _PASSENGER_STRING_MAP_H_ */