summaryrefslogtreecommitdiff
path: root/Runtime/Utilities/sorted_vector.h
blob: 5f0576f1c0ee1c9b985903489022044ee1f6a9eb (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef SORTED_VECTOR_H
#define SORTED_VECTOR_H
#include <vector>
#include <algorithm>

/// container optimization anschauen (compressed pair for valuecompare)


template<class T, class Compare, class Allocator>
class sorted_vector : private Compare
{
	public:
	
	typedef				std::vector<T, Allocator>			container;
	typedef typename	container::iterator					iterator;
	typedef typename	container::const_iterator			const_iterator;
	typedef typename	container::reverse_iterator			reverse_iterator;
	typedef typename	container::const_reverse_iterator	const_reverse_iterator;
	typedef typename	container::value_type				value_type;
	typedef typename	container::size_type				size_type;
	typedef typename	container::difference_type			difference_type;
	typedef 			Compare								value_compare;
	typedef typename	Allocator::reference				reference;
	typedef typename	Allocator::const_reference			const_reference;
	typedef				Allocator							allocator_type;

	value_compare const& get_compare () const { return *static_cast<value_compare const*> (this); }

		
	sorted_vector (const Compare& comp, const Allocator& a)
		: c (a), value_compare (comp)	 {}
	
	
	bool      			empty() const		{ return c.empty(); }
	size_type			size()  const		{ return c.size(); }
	const value_type&	front() const 		{ return c.front(); }
	const value_type&	back() const		{ return c.front(); }
		
	const_iterator begin ()const			{ return c.begin (); }
	const_iterator end ()const				{ return c.end (); }
	iterator begin ()						{ return c.begin (); }
	iterator end ()							{ return c.end (); }
	const_reverse_iterator rbegin ()const	{ return c.rbegin (); }
	const_reverse_iterator rend ()const		{ return c.rend (); }
	reverse_iterator rbegin ()				{ return c.rbegin (); }
	reverse_iterator rend ()				{ return c.rend (); }
	
	value_compare value_comp() const		{ return get_compare ();}
/*
	template <class InputIterator>
	void insert_one (InputIterator first, InputIterator last)
	{
		c.reserve (size () + std::distance (first, last));
		for (;first != last;first++)
			insert_one (*i);
	}
	
	template<typename KeyT, typename MappedT>
	MappedT& find_or_insert (const KeyT& k)
	{
		iterator i = lower_bound (k);
		if (i == end () || get_compare () (k, *i))
			return c.insert (i, std::make_pair<KeyT, MappedT> (k, MappedT ()))->second;
		else
			return i->second;
	}
*/
	
	template<typename KeyT, typename MappedT>
	void find_or_insert (MappedT*& mappedT, const KeyT& k)
	{
		iterator i = lower_bound (k);
		if (i == end () || get_compare () (k, *i))
			#if _MSC_VER >= 1700	// Microsoft Visual Studio 2012 workaround
			mappedT =  &c.insert (i, std::make_pair<KeyT, MappedT> (static_cast<KeyT&&>(const_cast<KeyT&>(k)), MappedT ()))->second;
			#else
			mappedT =  &c.insert (i, std::make_pair<KeyT, MappedT> (k, MappedT ()))->second;
			#endif
		else
			mappedT = &i->second;
	}

	std::pair<iterator, bool> insert_one (const value_type& x)
	{
		iterator i = lower_bound (x);
		// is not included in container
		if (i == end () || get_compare () (x, *i))
			return std::make_pair (c.insert (i, x), true);
		else
			return std::make_pair (i, false);
	
	}

	void verify_duplicates_and_sorted () const
	{
		#if DEBUGMODE
		// Check that there are no duplicates in the set
		if (empty())
			return;

		const_iterator previous = c.begin();
		const_iterator i = c.begin();
		i++;
		for (; i != c.end();++i)
		{
			Assert(get_compare ()(*previous, *i));
			previous = i;
		}
		#endif
	}
	
	
	template<class CompareType>
	size_type erase_one (const CompareType& x)
	{
		iterator i = lower_bound (x);
		if (i == end () || get_compare () (x, *i))
			return 0;
		else
		{	
			c.erase (i);
			return 1;
		}
	}
	
	void      			erase(iterator position)				{ c.erase (position);}
	void      			erase(iterator first, iterator last)	{ c.erase (first, last); }
	void 				swap(sorted_vector& x)					{ c.swap (x.c); }
	
	void				clear ()								{ c.clear (); }
	
	template<class T2>
	size_type			count_one (const T2& x)const			
	{	
		const_iterator i = lower_bound (x);
		if (i == end () || get_compare () (x, *i))
			return 0;
		else
			return 1;
	}							

	template<class T2>
	iterator				find (const T2& x)						
	{
		iterator i = lower_bound (x);
		if (i == end () || get_compare () (x, *i))
			return end ();
		else
			return i;
	}

	template<class T2>
	const_iterator				find (const T2& x) const						
	{
		const_iterator i = lower_bound (x);
		if (i == end () || get_compare () (x, *i))
			return end ();
		else
			return i;
	}
		
	template<class T2>
	iterator lower_bound (const T2& x)
	{
		return std::lower_bound (c.begin (), c.end (), x, get_compare ());
	}

	template<class T2>
	const_iterator lower_bound (const T2& x) const
	{
		return std::lower_bound (c.begin (), c.end (), x, get_compare ());
	}

	template<class T2>
	iterator upper_bound (const T2& x)
	{
		return std::lower_bound (c.begin (), c.end (), x, get_compare ());
	}

	template<class T2>
	const_iterator upper_bound (const T2& x) const
	{
		return std::lower_bound (c.begin (), c.end (), x, get_compare ());
	}

	template<class T2>
	std::pair<iterator, iterator> equal_range (const T2& x)
	{
		return std::equal_range (c.begin (), c.end (), x, get_compare ());
	}	

	template<class T2>
	std::pair<const_iterator, const_iterator> equal_range (const T2& x) const
	{
		return std::equal_range (c.begin (), c.end (), x, get_compare ());
	}	
	
	void reserve (size_type n)	{ c.reserve (n); }

	value_type& operator [] (int n) { return c[n]; }
	const value_type& operator [] (int n) const { return c[n]; }

	public:

	container		c;
};

template<class T, class Compare, class Allocator>
class unsorted_vector : private Compare
{
	public:
	
	typedef         	std::vector<T, Allocator>			container;
	typedef typename	container::iterator					iterator;
	typedef typename	container::const_iterator			const_iterator;
	typedef typename	container::reverse_iterator			reverse_iterator;
	typedef typename	container::const_reverse_iterator	const_reverse_iterator;

	typedef typename	container::value_type				value_type;
	typedef typename	container::size_type				size_type;
	typedef typename	container::difference_type			difference_type;
	typedef 			Compare								value_compare;					
	typedef typename	Allocator::reference				reference;
	typedef typename	Allocator::const_reference			const_reference;
	typedef				Allocator							allocator_type;

	value_compare const& get_compare () const { return *static_cast<value_compare const*> (this); }

	unsorted_vector (const Compare& comp, const Allocator& a)
		: c (a), value_compare (comp)  {}
	
	
	bool      			empty() const		{ return c.empty(); }
	size_type			size()  const		{ return c.size(); }
	const value_type&	front() const 		{ return c.front(); }
	const value_type&	back() const		{ return c.front(); }
		
	const_iterator begin ()const			{ return c.begin (); }
	const_iterator end ()const				{ return c.end (); }
	iterator begin ()						{ return c.begin (); }
	iterator end ()							{ return c.end (); }
	const_reverse_iterator rbegin ()const	{ return c.rbegin (); }
	const_reverse_iterator rend ()const		{ return c.rend (); }
	reverse_iterator rbegin ()				{ return c.rbegin (); }
	reverse_iterator rend ()				{ return c.rend (); }
	
	value_compare value_comp() const		{ return get_compare ();}
/*
	template <class InputIterator>
	void insert_one (InputIterator first, InputIterator last)
	{
		c.reserve (size () + std::distance (first, last));
		for (;first != last;first++)
			insert_one (*i);
	}
*/
	template<typename KeyT, typename MappedT>
	void find_or_insert (MappedT*& mappedT, const KeyT& k)
	{
		iterator i = find (k);
		if (i == end ())
		{
			c.push_back (std::make_pair<KeyT, MappedT> (k, MappedT ()));
			mappedT = &(c.end () - 1)->second;
		}
		else
			mappedT = &i->second;
	}
	/*
	template<class Key, class Value>
	Value& find_or_insert (const Key& k)
	{
		iterator i = find (k);
		if (i == end ())
		{
			c.push_back (std::make_pair<Key, Value> (k, Value ()));
			return (c.end () - 1)->second;
		}
		else
			return i->second;
	}
	*/
	std::pair<iterator, bool> insert_one (const value_type& x)
	{
		iterator i = find (x);
		// is not included in container
		if (i == end ())
		{
			c.push_back (x);
			return std::make_pair (c.end () - 1, true);
		}
		else
			return std::make_pair (i, false);
	}
	
	template<class CompareType>
	size_type erase_one (const CompareType& x)
	{
		iterator i = find (x);
		if (i == end ())
			return 0;
		else
		{	
			erase (i);
			return 1;
		}
	}
	
	void      			erase(iterator position)	{ *position = c.back (); c.pop_back (); }
	void 					swap(unsorted_vector& x)	{ c.swap (x.c); }
	
	void					clear ()							{ c.clear (); }

	template<class T2>
	size_type			count_one (const T2& x)const			
	{	
		const_iterator i = find (x);
		return i != end ();
	}							

	template<class T2>
	iterator				find (const T2& x)
	{
		iterator b = c.begin ();
		iterator e = c.end ();
		for (;b != e;++b)
		{
			if (get_compare () (*b, x))
				return b;
		}
		return e;	
	}

	template<class T2>
	const_iterator			find (const T2& x) const
	{
	{
		const_iterator b = c.begin ();
		const_iterator e = c.end ();
		for (;b != e;++b)
		{
			if (get_compare () (*b, x))
				return b;
		}
		return e;	
	}
	
	}		
	void 					reserve (size_type n)				{ c.reserve (n); }
	value_type& operator [] (int n) { return c[n]; }
	const value_type& operator [] (int n) const { return c[n]; }

	public:

	container		c;
};

#endif