aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/include/Poco/Array.h
blob: 76e5faed8916cfbdbb627aac829a3f92647b5948 (plain) (blame)
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
//
// Array.h
//
// Library: Foundation
// Package: Core
// Module:  Array
//
// Definition of the Array class
//
// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//
// ------------------------------------------------------------------------------
// (C) Copyright Nicolai M. Josuttis 2001.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// ------------------------------------------------------------------------------


#ifndef Foundation_Array_INCLUDED
#define Foundation_Array_INCLUDED

#include "Poco/Exception.h"
#include "Poco/Bugcheck.h"
#include <algorithm>

namespace Poco {

template<class T, std::size_t N>
class Array 
	/// STL container like C-style array replacement class. 
	/// 
	/// This implementation is based on the idea of Nicolai Josuttis.
	/// His original implementation can be found at http://www.josuttis.com/cppcode/array.html . 
{

public:

	typedef T				value_type;
	typedef T*				iterator;
	typedef const T*		const_iterator;
	typedef T&				reference;
	typedef const T&		const_reference;
	typedef std::size_t		size_type;
	typedef std::ptrdiff_t  difference_type;

	iterator begin()
	{
		return elems;
	}

	const_iterator begin() const
	{
		return elems;
	}

	iterator end()
	{ 
		return elems+N;
	}

	const_iterator end() const
	{
		return elems+N;
	}

	typedef std::reverse_iterator<iterator>			reverse_iterator;
	typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;

	reverse_iterator rbegin()
	{
		return reverse_iterator(end());
	}

	const_reverse_iterator rbegin() const
	{
		return const_reverse_iterator(end());
	}

	reverse_iterator rend()
	{ 
		return reverse_iterator(begin());
	}

	const_reverse_iterator rend() const
	{
		return const_reverse_iterator(begin());
	}

	reference operator[](size_type i) 
		/// Element access without range check. If the index is not small than the given size, the behavior is undefined.
	{ 
		poco_assert( i < N && "out of range" ); 
		return elems[i];
	}

	const_reference operator[](size_type i) const 
		/// Element access without range check. If the index is not small than the given size, the behavior is undefined.
	{	 
		poco_assert( i < N && "out of range" ); 
		return elems[i]; 
	}

	reference at(size_type i)
		/// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
	{ 
		if(i>=size())
			throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
		return elems[i]; 
	}

	const_reference at(size_type i) const
		/// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
	{
		if(i>=size())
			throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
		return elems[i]; 
	}

	reference front() 
	{ 
		return elems[0]; 
	}

	const_reference front() const 
	{
		return elems[0];
	}

	reference back() 
	{ 
		return elems[N-1]; 
	}

	const_reference back() const 
	{ 
		return elems[N-1]; 
	}

	static size_type size()
	{
		return N; 
	}

	static bool empty()
	{ 
		return false; 
	}

	static size_type max_size()
	{ 
		return N; 
	}

	enum { static_size = N };

	void swap (Array<T,N>& y) {
		std::swap_ranges(begin(),end(),y.begin());
	}

	const T* data() const
		/// Direct access to data (read-only)
	{ 
		return elems; 
	}

	T* data()
	{ 
		return elems;
	}

	T* c_array(){ 
		/// Use array as C array (direct read/write access to data)
		return elems;
	}

	template <typename Other>
	Array<T,N>& operator= (const Array<Other,N>& rhs)
		/// Assignment with type conversion 
	{
		std::copy(rhs.begin(),rhs.end(), begin());
		return *this;
	}

	void assign (const T& value)
		/// Assign one value to all elements
	{
		std::fill_n(begin(),size(),value);
	}

public:

	T elems[N];	
		/// Fixed-size array of elements of type T, public specifier used to make this class a aggregate.

};

// comparisons
template<class T, std::size_t N>
bool operator== (const Array<T,N>& x, const Array<T,N>& y) 
{
	return std::equal(x.begin(), x.end(), y.begin());
}

template<class T, std::size_t N>
bool operator< (const Array<T,N>& x, const Array<T,N>& y) 
{
	return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
}

template<class T, std::size_t N>
bool operator!= (const Array<T,N>& x, const Array<T,N>& y) 
{
	return !(x==y);
}

template<class T, std::size_t N>
bool operator> (const Array<T,N>& x, const Array<T,N>& y) 
{
	return y<x;
}

template<class T, std::size_t N>
bool operator<= (const Array<T,N>& x, const Array<T,N>& y) 
{
	return !(y<x);
}

template<class T, std::size_t N>
bool operator>= (const Array<T,N>& x, const Array<T,N>& y) 
{
	return !(x<y);
}

template<class T, std::size_t N>
inline void swap (Array<T,N>& x, Array<T,N>& y) 
	/// global swap()
{
	x.swap(y);
}

}// namespace Poco

#endif // Foundation_Array_INCLUDED