container.hpp 81.6 KB
Newer Older
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
1

Kirill Terekhov's avatar
Kirill Terekhov committed
2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef _CONTAINER_HPP
#define _CONTAINER_HPP


#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <memory>
#include <new>
#include <iterator>
#include <cmath>
#include <assert.h>
#include <limits>
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
15
#include "inmost_common.h"
Kirill Terekhov's avatar
Kirill Terekhov committed
16 17 18 19 20

//#define OUT_OF_RANGE

template<class element, class T1> struct isInputRandomIterators
{
Kirill Terekhov's avatar
Kirill Terekhov committed
21
	static void constraints(T1 a, T1 b) { /*element x = static_cast<element>(*a); (void)x;*/ ++a; (void)a++; a==a; a!=a; a-b; }
Kirill Terekhov's avatar
Kirill Terekhov committed
22 23 24 25 26
	isInputRandomIterators() { void(*p)(T1,T1) = constraints; (void)p; }
};

template<class element, class T1> struct isInputForwardIterators
{
Kirill Terekhov's avatar
Kirill Terekhov committed
27
	static void constraints(T1 a) { /*element x = static_cast<element>(*a); (void)x;*/ ++a; (void)a++; }
Kirill Terekhov's avatar
Kirill Terekhov committed
28 29 30 31 32
	isInputForwardIterators() { void(*p)(T1) = constraints; (void)p; }
};

namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
33 34 35 36 37 38 39 40 41 42 43 44
	//add more templates as needed
	template<class T> struct make_unsigned;
	template<> struct make_unsigned<char> {typedef unsigned char type;};
	template<> struct make_unsigned<short> {typedef unsigned short type;};
	template<> struct make_unsigned<int> {typedef unsigned int type;};
	template<> struct make_unsigned<long> {typedef unsigned long type;};
	template<> struct make_unsigned<long long> {typedef unsigned long long type;};
	template<> struct make_unsigned<unsigned char> {typedef unsigned char type;};
	template<> struct make_unsigned<unsigned short> {typedef unsigned short type;};
	template<> struct make_unsigned<unsigned int> {typedef unsigned int type;};
	template<> struct make_unsigned<unsigned long> {typedef unsigned long type;};
	template<> struct make_unsigned<unsigned long long> {typedef unsigned long long type;};
Kirill Terekhov's avatar
Kirill Terekhov committed
45
	
Kirill Terekhov's avatar
Kirill Terekhov committed
46
#define USE_OPTIMIZED_ARRAY_ALLOCATION
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
47 48 49
#if defined(PACK_ARRAY)
#pragma pack(push,r1,4)
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
50 51 52
	// notice: array class would not properly support classes that contain self-references
	//         like std::map
	// notice: next class shell have to implement same algorithms as array
Kirill Terekhov's avatar
Kirill Terekhov committed
53 54 55 56
	template<typename element>//, typename enumerator = unsigned int>
	class array
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
57 58
		typedef unsigned size_type;
		typedef make_unsigned<size_type>::type uenum;
Kirill Terekhov's avatar
Kirill Terekhov committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		template<typename etype>
		class _iterator
		{
		private:
			etype * e;
		public:
			typedef etype * pointer;
			typedef etype & reference;
			typedef etype value_type;
			typedef ptrdiff_t difference_type;
			typedef std::random_access_iterator_tag iterator_category;
			_iterator():e(NULL){}
			_iterator(etype * i):e(i){}
			_iterator(const _iterator & other){e = other.e;}
			~_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
74 75 76 77
			_iterator operator -(size_t n) { return _iterator(e-n); }
			_iterator & operator -=(size_t n) { e-=n; return *this; }
			_iterator operator +(size_t n) { return _iterator(e+n); }
			_iterator & operator +=(size_t n) { e+=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
78 79 80 81 82 83 84 85
			_iterator & operator ++(){ ++e; return *this;}
			_iterator operator ++(int){ return _iterator(e++); }
			_iterator & operator --(){ --e; return *this; }
			_iterator operator --(int){ return _iterator(e--); }
			ptrdiff_t operator -(const _iterator & other) const {return e-other.e;}
			etype & operator *() { return *e; }
			etype * operator ->() { return e; }
			_iterator & operator =(_iterator const & other) { e = other.e; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
86 87 88 89 90 91
			bool operator ==(const _iterator & other) const { return e == other.e;}
			bool operator !=(const _iterator & other) const { return e != other.e;}
			bool operator <(const _iterator & other) const { return e < other.e;}
			bool operator >(const _iterator & other) const { return e > other.e;}
			bool operator <=(const _iterator & other) const { return e <= other.e;}
			bool operator >=(const _iterator & other) const { return e >= other.e;}
Kirill Terekhov's avatar
Kirill Terekhov committed
92
			operator void *() {return static_cast<void *> (e);}
Kirill Terekhov's avatar
Kirill Terekhov committed
93
			operator const void *() {return const_cast<const void *> (e);}
Kirill Terekhov's avatar
Kirill Terekhov committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		};
		typedef _iterator<element> iterator;
		typedef _iterator<const element> const_iterator;
		template<typename etype>
		class _reverse_iterator
		{
		private:
			etype * e;
		public:
			typedef etype * pointer;
			typedef etype & reference;
			typedef etype value_type;
			typedef ptrdiff_t difference_type;
			typedef std::random_access_iterator_tag iterator_category;
			_reverse_iterator():e(NULL){}
			_reverse_iterator(etype * i):e(i){}
			_reverse_iterator(const _reverse_iterator & other){e = other.e;}
			~_reverse_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
112 113 114 115
			_reverse_iterator operator -(size_t n) { return _reverse_iterator(e+n); }
			_reverse_iterator & operator -=(size_t n) { e+=n; return *this; }
			_reverse_iterator operator +(size_t n) {return _reverse_iterator(e-n); }
			_reverse_iterator & operator +=(size_t n) { e-=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
116 117 118 119 120 121 122 123
			_reverse_iterator & operator ++(){ --e; return *this;}
			_reverse_iterator operator ++(int){ return _reverse_iterator(e--); }
			_reverse_iterator & operator --(){ ++e; return *this; }
			_reverse_iterator operator --(int){ return _reverse_iterator(e++); }
			ptrdiff_t operator -(const _reverse_iterator & other) const {return other.e-e;}
			etype & operator *() { return *e; }
			etype * operator ->() { return e; }
			_reverse_iterator & operator =(_reverse_iterator const & other) { e = other.e; return *this;}
Kirill Terekhov's avatar
Kirill Terekhov committed
124 125 126 127 128 129
			bool operator ==(const _reverse_iterator & other) const { return e == other.e;}
			bool operator !=(const _reverse_iterator & other) const { return e != other.e;}
			bool operator <(const _reverse_iterator & other) const { return e < other.e;}
			bool operator >(const _reverse_iterator & other) const { return e > other.e;}
			bool operator <=(const _reverse_iterator & other) const { return e <= other.e;}
			bool operator >=(const _reverse_iterator & other) const { return e >= other.e;}
Kirill Terekhov's avatar
Kirill Terekhov committed
130 131 132 133 134
			operator void *() {return static_cast<void *> (e);}
		};
		typedef _reverse_iterator<element> reverse_iterator;
		typedef _reverse_iterator<const element> const_reverse_iterator;
	private:
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
135

Kirill Terekhov's avatar
Kirill Terekhov committed
136
		element * m_arr;
Kirill Terekhov's avatar
Kirill Terekhov committed
137
		size_type m_size;
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
138

Kirill Terekhov's avatar
Kirill Terekhov committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		//notice: push_back, pop_back, insert, erase are optimized for current growth_formula
		__INLINE static size_type growth_formula(size_type future_size)
		{
			uenum v = static_cast<uenum>(future_size);
			v--;
			v|= (v>>1);
			v|= (v>>2);
			v|= (v>>4);
			v|= (v>>8);
			v|= (v>>16);
			//TODO produces compiler warning
			//if( sizeof(size_type) > 4 ) v|= (v>>32); //must be compile time brench
			v++;
			return static_cast<size_type>(v);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
154
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
155 156
		__INLINE element * data() {return m_arr;}
		__INLINE const element * data() const {return m_arr;}
Kirill Terekhov's avatar
Kirill Terekhov committed
157
		array() {m_arr = NULL; m_size = 0;}
Kirill Terekhov's avatar
Kirill Terekhov committed
158
		array(size_type n,element c = element())
Kirill Terekhov's avatar
Kirill Terekhov committed
159 160
		{
			m_size = n;
Kirill Terekhov's avatar
Kirill Terekhov committed
161
			m_arr = static_cast<element *>(malloc(sizeof(element)*growth_formula(m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
162
			assert(m_arr != NULL);
Kirill Terekhov's avatar
Kirill Terekhov committed
163
			for(size_type i = 0; i < m_size; i++) new (m_arr+i) element(c);
Kirill Terekhov's avatar
Kirill Terekhov committed
164 165 166 167
		}
		template<class InputIterator>
		array(InputIterator first, InputIterator last)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
168 169 170
			//isInputForwardIterators<element,InputIterator>();
			m_size = static_cast<size_type>(std::distance(first,last));
			m_arr = static_cast<element *>(malloc(sizeof(element)*growth_formula(m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
171 172
			assert(m_arr != NULL);
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
173
				size_type i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
174 175 176 177 178 179 180 181 182
				InputIterator it = first;
				while(it != last) new (m_arr+i++) element(*it++);
			}
		}
		array(const array & other)
		{
			m_size = other.m_size;
			if( m_size ) 
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
183
				m_arr = static_cast<element *>(malloc(sizeof(element)*growth_formula(m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
184 185 186
				assert(m_arr != NULL);
			}
			else m_arr = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
187
			for(size_type i = 0; i < m_size; i++) new (m_arr+i) element(other.m_arr[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
188 189 190
		}
		~array()
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
191
			for(size_type i = 0; i < m_size; i++) m_arr[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
192 193 194 195
			if( m_arr != NULL ) free(m_arr);
			m_arr = NULL;
			m_size = 0;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
196
		__INLINE const element & operator [] (size_type n) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
197 198 199 200
		{
			assert(n < m_size);
			return m_arr[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
201
		__INLINE element & operator [] (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
202 203 204 205
		{
			assert(n < m_size);
			return m_arr[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
206
		__INLINE const element & at (size_type n) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
207 208 209 210
		{
			assert(n < m_size);
			return m_arr[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
211
		__INLINE element & at (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
212 213 214 215
		{
			assert(n < m_size);
			return m_arr[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
216
		__INLINE element & at_safe (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
217 218 219 220 221 222 223 224
		{
			if( n >= m_size ) resize(n+1);
			return m_arr[n];
		}
		array & operator =(array const & other)
		{
			if( this != &other )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
225
				for(size_type i = 0; i < m_size; i++) m_arr[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
226 227 228 229 230 231 232 233
				if(m_arr != NULL ) 
				{
					free(m_arr);
					m_arr = NULL;
				}
				if( other.m_arr != NULL )
				{
					m_size = other.m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
234
					m_arr = static_cast<element *>(malloc(sizeof(element)*growth_formula(m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
235 236 237 238 239 240 241 242
					assert(m_arr != NULL);
					memcpy(m_arr,other.m_arr,sizeof(element)*m_size);
				}
			}
			return *this;
		}
		void push_back(const element & e)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
243 244 245 246 247 248 249 250 251 252 253 254 255
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
			//unoptimized variant
			if( m_size+1 > growth_formula(m_size) )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*growth_formula(++m_size)));
			else ++m_size;
#else
			//optimized for current growth_formula
			if( m_size < 2 )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*(++m_size)));
			else if( ((m_size+1) & (m_size-1)) == 1 )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*(m_size++ << 1)));
			else m_size++;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
256 257 258 259 260 261 262
			assert(m_arr != NULL);
			new (m_arr+m_size-1) element(e);
		}
		void pop_back()
		{
			assert(m_arr != NULL);
			m_arr[m_size--].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
263 264 265 266 267 268 269 270 271 272 273
			if( m_size > 0)
			{
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
				//unoptimized variant
				size_type gf = growth_formula(m_size);
				if( m_size+1 > gf )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*gf));
#else
				if( ((m_size+1) & (m_size-1)) == 1 || m_size == 1)
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*m_size));
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
274 275 276 277 278 279 280 281
				assert(m_arr != NULL);
			} 
			else 
			{
				free(m_arr);
				m_arr = NULL;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
282
		__INLINE element & back() 
Kirill Terekhov's avatar
Kirill Terekhov committed
283 284 285 286 287
		{
			assert(m_arr != NULL);
			assert(m_size > 0);
			return m_arr[m_size-1];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
288
		__INLINE const element & back() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
289 290 291 292 293
		{
			assert(m_arr != NULL);
			assert(m_size > 0);
			return m_arr[m_size-1];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
294
		__INLINE element & front() 
Kirill Terekhov's avatar
Kirill Terekhov committed
295 296 297 298 299
		{
			assert(m_arr != NULL);
			assert(m_size > 0);
			return m_arr[0];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
300
		__INLINE const element & front() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
301 302 303 304 305
		{
			assert(m_arr != NULL);
			assert(m_size > 0);
			return m_arr[0];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
306
		__INLINE size_type capacity() { return m_size; }
Kirill Terekhov's avatar
Kirill Terekhov committed
307
		__INLINE bool empty() const { if( m_size ) return false; return true; }
Kirill Terekhov's avatar
Kirill Terekhov committed
308
		void resize(size_type n, element c = element() )
Kirill Terekhov's avatar
Kirill Terekhov committed
309
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
310
			size_type oldsize = m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
311
			m_size = n;
Kirill Terekhov's avatar
Kirill Terekhov committed
312
			for(size_type i = m_size; i < oldsize; i++) m_arr[i].~element(); //delete elements, located over the size
Kirill Terekhov's avatar
Kirill Terekhov committed
313 314
			if( m_size > 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
315 316
				if( growth_formula(oldsize) != growth_formula(m_size) )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*growth_formula(m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
317
				assert(m_arr != NULL);
Kirill Terekhov's avatar
Kirill Terekhov committed
318
				for(size_type i = oldsize; i < m_size; i++) new (m_arr+i) element(c); //initialize extra entities
Kirill Terekhov's avatar
Kirill Terekhov committed
319 320 321 322 323 324 325
			}
			else
			{
				free(m_arr);
				m_arr = NULL;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
326 327
		__INLINE size_type size() const {return m_size;}
		__INLINE size_type capacity() const {return growth_formula(m_size);}
Kirill Terekhov's avatar
Kirill Terekhov committed
328 329
		void clear() 
		{ 
Kirill Terekhov's avatar
Kirill Terekhov committed
330
			for(size_type i = 0; i < m_size; i++) m_arr[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
331 332 333 334 335 336 337
			m_size = 0; 
			if( m_arr ) free(m_arr); 
			m_arr = NULL; 
		}
		void swap(array<element> & other)
		{
			element * t_m_arr = m_arr;
Kirill Terekhov's avatar
Kirill Terekhov committed
338
			size_type t_m_size = m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
339 340 341 342 343
			m_arr = other.m_arr;
			m_size = other.m_size;
			other.m_arr = t_m_arr;
			other.m_size = t_m_size;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
344 345 346 347 348 349 350 351
		__INLINE iterator begin() { return m_arr; }
		__INLINE iterator end() { return m_arr+m_size; }
		__INLINE const_iterator begin() const { return m_arr; }
		__INLINE const_iterator end() const { return m_arr+m_size; }
		__INLINE reverse_iterator rbegin() { return reverse_iterator(m_arr+m_size-1); }
		__INLINE reverse_iterator rend() { return reverse_iterator(m_arr-1); }
		__INLINE const_reverse_iterator rbegin() const { return const_reverse_iterator(m_arr+m_size-1); }
		__INLINE const_reverse_iterator rend() const { return const_reverse_iterator(m_arr-1); }
Kirill Terekhov's avatar
Kirill Terekhov committed
352 353 354 355 356 357 358 359 360
		iterator erase(iterator pos) 
		{ 
			ptrdiff_t d = pos-begin();
			ptrdiff_t s = iterator(m_arr+m_size-1)-pos;
			(*pos).~element();
			m_size--;
			if( m_size > 0 )
			{
				if( s > 0 ) memmove(m_arr+d,m_arr+d+1,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
361 362 363 364 365 366 367 368 369
				//unoptimized variant
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
				size_type gf = growth_formula(m_size);
				if( m_size+1 > gf )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*gf));
#else
				if( ((m_size+1) & (m_size-1)) == 1 || m_size == 1)
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*m_size));
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
				assert(m_arr != NULL);
			}
			else
			{
				free(m_arr);
				m_arr = NULL;
			}
			return m_arr+d;
		}
		iterator erase(iterator b, iterator e)
		{
			ptrdiff_t d = b-begin();
			ptrdiff_t s = end()-e;
			ptrdiff_t n = e-b;
			for(iterator i = b; i != e; i++) (*i).~element();
			m_size -= n;
			if( m_size > 0 )
			{
				if( s > 0 ) memmove(m_arr+d,m_arr+d+1,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
389 390 391
				size_type gf = growth_formula(m_size);
				if( m_size+n > gf )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*gf));
Kirill Terekhov's avatar
Kirill Terekhov committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
				assert(m_arr != NULL);
			}
			else
			{
				free(m_arr);
				m_arr = NULL;
			}
			return m_arr+d;
		}
		iterator insert(iterator pos, const element & x)
		{
			if( static_cast<void *>(pos) == NULL)
			{
				assert(m_arr == NULL);
				pos = iterator(m_arr = static_cast<element *>(malloc(sizeof(element))));
				assert(m_arr != NULL);
			}
			ptrdiff_t d = pos-begin();
			ptrdiff_t s = end()-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424

			//unoptimized variant
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
			if( m_size+1 > growth_formula(m_size) )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*growth_formula(++m_size)));
			else ++m_size;
#else
			//optimized for current growth_formula
			if( m_size < 2 )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*(++m_size)));
			else if( ((m_size+1) & (m_size-1)) == 1 )
				m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*(m_size++ << 1)));
			else ++m_size;
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
425 426 427 428 429
			assert(m_arr != NULL);
			if( s > 0 ) memmove(m_arr+d+1,m_arr+d,sizeof(element)*s);
			new (m_arr+d) element(x);
			return m_arr+d;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
430
		void insert(iterator pos, size_type n, const element & x)
Kirill Terekhov's avatar
Kirill Terekhov committed
431 432 433 434 435 436 437 438 439 440 441
		{
			if( n > 0 )
			{
				if( static_cast<void *>(pos) == NULL)
				{
					assert(m_arr == NULL);
					pos = iterator(m_arr = static_cast<element *>(malloc(sizeof(element))));
					assert(m_arr != NULL);
				}
				ptrdiff_t d = pos-begin();
				ptrdiff_t s = end()-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
442 443 444

				if( m_size+n > growth_formula(m_size) )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*growth_formula(m_size+n)));
Kirill Terekhov's avatar
Kirill Terekhov committed
445
				m_size+=n;
Kirill Terekhov's avatar
Kirill Terekhov committed
446

Kirill Terekhov's avatar
Kirill Terekhov committed
447 448
				assert(m_arr != NULL);
				if( s > 0 ) memmove(m_arr+d+n,m_arr+d,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
449
				for(size_type i = 0; i < n; i++) new (m_arr+d+i) element(x);
Kirill Terekhov's avatar
Kirill Terekhov committed
450 451 452 453 454
			}
		}
		template <class InputIterator>
		void insert(iterator pos, InputIterator first, InputIterator last)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
455
			size_type n = static_cast<size_type>(std::distance(first,last));
Kirill Terekhov's avatar
Kirill Terekhov committed
456 457 458 459 460 461 462 463 464 465
			if( n > 0 )
			{
				if( static_cast<void *>(pos) == NULL)
				{
					assert(m_arr == NULL);
					pos = iterator(m_arr = static_cast<element *>(malloc(sizeof(element))));
					assert(m_arr != NULL);
				}
				ptrdiff_t d = pos-begin();
				ptrdiff_t s = end()-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
466 467 468

				if( m_size+n > growth_formula(m_size) )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*growth_formula(m_size+n)));
Kirill Terekhov's avatar
Kirill Terekhov committed
469
				m_size+=n;
Kirill Terekhov's avatar
Kirill Terekhov committed
470
				
Kirill Terekhov's avatar
Kirill Terekhov committed
471 472 473 474
				assert(m_arr != NULL);
				if( s > 0 ) memmove(m_arr+d+n,m_arr+d,sizeof(element)*s);
				{
					InputIterator it = first;
Kirill Terekhov's avatar
Kirill Terekhov committed
475
					size_type i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
476 477 478 479 480 481 482 483
					while(it != last) new (m_arr+d+i++) element(*it++);
				}
			}
		}
		template <class InputIterator>
		void replace(iterator m_first, iterator m_last, InputIterator first, InputIterator last)
		{
			assert( m_size >= 0 );
Kirill Terekhov's avatar
Kirill Terekhov committed
484
			ptrdiff_t n = static_cast<ptrdiff_t>(std::distance(first,last));
Kirill Terekhov's avatar
Kirill Terekhov committed
485 486 487 488 489 490 491 492 493 494 495 496
			if( static_cast<void *>(m_first) == NULL && m_arr == NULL)
			{
				assert(m_arr == NULL);
				m_first = m_last = iterator(m_arr = static_cast<element *>(malloc(sizeof(element))));
				assert(m_arr != NULL);
			}
			ptrdiff_t q = m_last-m_first; 
			ptrdiff_t d = m_first-iterator(m_arr);
			ptrdiff_t s = iterator(m_arr+m_size)-m_last;
			for(iterator it = m_first; it != m_last; it++) (*it).~element();
			if( n-q != 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
497 498 499 500
				size_type gf = growth_formula(m_size+(n-q));
				if( gf != growth_formula(m_size) )
					m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*gf));
				m_size+=(n-q);
Kirill Terekhov's avatar
Kirill Terekhov committed
501 502 503 504
				if( s > 0 ) memmove(m_arr+d+n,m_arr+d+q,sizeof(element)*s);
			}
			{
				InputIterator it = first;
Kirill Terekhov's avatar
Kirill Terekhov committed
505
				size_type i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
506 507 508 509 510
				while(it != last) new (m_arr+d+i++) element(*it++);
			}
		}
		template<class> friend class shell;
	};	
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
511 512 513
#if defined(PACK_ARRAY)
#pragma pack(pop,r1)
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
514 515 516 517
	template<typename element>
	class shell
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
518
		typedef typename array<element>::size_type size_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		template<typename dtype>
		class _iterator
		{
		private:
			dtype * e;
		public:
			typedef dtype * pointer;
			typedef dtype & reference;
			typedef dtype value_type;
			typedef ptrdiff_t difference_type;
			typedef std::random_access_iterator_tag iterator_category;
			_iterator():e(NULL){}
			_iterator(dtype * i):e(i){}
			_iterator(const _iterator & other){e = other.e;}
			~_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
534 535 536 537
			_iterator operator -(size_t n) { return _iterator(e-n); }
			_iterator & operator -=(size_t n) { e-=n; return *this; }
			_iterator operator +(size_t n) { return _iterator(e+n); }
			_iterator & operator +=(size_t n) { e+=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
			_iterator & operator ++(){ ++e; return *this;}
			_iterator operator ++(int){ return _iterator(e++); }
			_iterator & operator --(){ --e; return *this; }
			_iterator operator --(int){ return _iterator(e--); }
			ptrdiff_t operator -(const _iterator & other) const {return e-other.e;}
			dtype & operator *() { return *e; }
			dtype * operator ->() { return e; }
			_iterator & operator =(_iterator const & other) { e = other.e; return *this; }
			bool operator ==(const _iterator & other) { return e == other.e;}
			bool operator !=(const _iterator & other) { return e != other.e;}
			bool operator <(const _iterator & other) { return e < other.e;}
			bool operator >(const _iterator & other) { return e > other.e;}
			bool operator <=(const _iterator & other) { return e <= other.e;}
			bool operator >=(const _iterator & other) { return e >= other.e;}
			operator void *() {return static_cast<void *> (e);}
		};
		typedef _iterator<element> iterator;
		typedef _iterator<const element> const_iterator;
		template<typename dtype>
		class _reverse_iterator
		{
		private:
			dtype * e;
		public:
			typedef dtype * pointer;
			typedef dtype & reference;
			typedef dtype value_type;
			typedef ptrdiff_t difference_type;
			typedef std::random_access_iterator_tag iterator_category;
			_reverse_iterator():e(NULL){}
			_reverse_iterator(dtype * i):e(i){}
			_reverse_iterator(const _reverse_iterator & other){e = other.e;}
			~_reverse_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
571 572 573 574
			_reverse_iterator operator -(size_t n) { return _reverse_iterator(e+n); }
			_reverse_iterator & operator -=(size_t n) { e+=n; return *this; }
			_reverse_iterator operator +(size_t n) {return _reverse_iterator(e-n); }
			_reverse_iterator & operator +=(size_t n) { e-=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
			_reverse_iterator & operator ++(){ --e; return *this;}
			_reverse_iterator operator ++(int){ return _reverse_iterator(e--); }
			_reverse_iterator & operator --(){ ++e; return *this; }
			_reverse_iterator operator --(int){ return _reverse_iterator(e++); }
			ptrdiff_t operator -(const _reverse_iterator & other) const {return other.e-e;}
			dtype & operator *() { return *e; }
			dtype * operator ->() { return e; }
			_reverse_iterator & operator =(_reverse_iterator const & other) { e = other.e; return *this;}
			bool operator ==(const _reverse_iterator & other) { return e == other.e;}
			bool operator !=(const _reverse_iterator & other) { return e != other.e;}
			bool operator <(const _reverse_iterator & other) { return e < other.e;}
			bool operator >(const _reverse_iterator & other) { return e > other.e;}
			bool operator <=(const _reverse_iterator & other) { return e <= other.e;}
			bool operator >=(const _reverse_iterator & other) { return e >= other.e;}
			operator void *() {return static_cast<void *> (e);}
		};
		typedef _reverse_iterator<element> reverse_iterator;
		typedef _reverse_iterator<const element> const_reverse_iterator;
	private:
		element ** m_arr;
Kirill Terekhov's avatar
Kirill Terekhov committed
595
		size_type * m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
596
		element * local_link;
Kirill Terekhov's avatar
Kirill Terekhov committed
597
		size_type local_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
598 599
		bool fixed;
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
600 601
		__INLINE element * data() {return *m_arr;}
		__INLINE const element * data() const {return *m_arr;}
Kirill Terekhov's avatar
Kirill Terekhov committed
602 603 604 605 606 607 608
		shell() {m_arr = NULL; m_size = NULL; fixed = false;}
		shell(array<element> & arr) //dynamic
		{
			m_arr = &arr.m_arr;
			m_size = &arr.m_size;
			fixed = false;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
609
		shell(element * link, size_type size) //fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		{
			m_arr = &local_link;
			*m_arr = link;
			m_size = &local_size;
			*m_size = size;
			fixed = true;
		}
		shell(const shell & other)
		{
			fixed = other.fixed;
			if( fixed )
			{
				m_size = &local_size;
				m_arr = &local_link;
				*m_size = *other.m_size;
				*m_arr = *other.m_arr;
			}
			else
			{
				m_size = other.m_size;
				m_arr = other.m_arr;
			}
		}
		~shell()
		{
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
636
		__INLINE const element & operator [] (size_type n) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
637 638 639 640
		{
			assert(n < *m_size );
			return *((*m_arr)+n);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
641
		__INLINE element & operator [] (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
642 643 644 645
		{
			assert(n < *m_size );
			return *((*m_arr)+n);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
646
		__INLINE const element & at (size_type n) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
647 648 649 650
		{
			assert(n < *m_size );
			return *((*m_arr)+n);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
651
		__INLINE element & at (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		{
			assert(n < *m_size );
			return *((*m_arr)+n);
		}
		shell & operator =(shell const & other)
		{
			fixed = other.fixed;
			if( fixed )
			{
				m_size = &local_size;
				m_arr = &local_link;	
				*m_size = *other.m_size;
				*m_arr = *other.m_arr;
			}
			else
			{
				m_size = other.m_size;
				m_arr = other.m_arr;
			}
			return *this;
		}
		void push_back(const element & e)
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
675
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
676 677 678 679 680 681 682 683 684 685 686
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
			//unoptimized variant
			if( (*m_size)+1 > array<element>::growth_formula(*m_size) )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*array<element>::growth_formula(++(*m_size))));
			else ++(*m_size);
#else
			//optimized for current growth_formula
			if( *m_size < 2 )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*(++(*m_size))));
			else if( (((*m_size)+1) & ((*m_size)-1)) == 1 )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*((*m_size)++ << 1)));
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
687
			else ++(*m_size);
Kirill Terekhov's avatar
Kirill Terekhov committed
688
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
689 690 691 692 693
			assert((*m_arr) != NULL);
			new ((*m_arr)+(*m_size)-1) element(e);
		}
		void pop_back()
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
694
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
695 696 697 698
			assert((*m_arr) != NULL);
			(*m_arr)[(*m_size)--].~element();
			if( (*m_size) > 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
699 700 701 702 703 704 705 706 707
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
				//unoptimized variant
				size_type gf = array<element>::growth_formula(*m_size);
				if( (*m_size)+1 > gf )
					*m_arr = static_cast<element *>(realloc(m_arr,sizeof(element)*gf));
#else
				if( (((*m_size)+1) & ((*m_size)-1)) == 1 || (*m_size) == 1)
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*(*m_size)));
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
708 709 710 711 712 713 714 715
				assert( (*m_arr) != NULL );
			}
			else
			{
				free(*m_arr);
				(*m_arr) = NULL;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
716
		__INLINE element & back() 
Kirill Terekhov's avatar
Kirill Terekhov committed
717 718 719 720 721
		{
			assert(*m_arr != NULL);
			assert(*m_size > 0 );
			return (*m_arr)[(*m_size)-1];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
722
		__INLINE const element & back() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
723 724 725 726 727
		{
			assert(*m_arr != NULL);
			assert(*m_size > 0 );
			return (*m_arr)[(*m_size)-1];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
728
		__INLINE element & front() 
Kirill Terekhov's avatar
Kirill Terekhov committed
729 730 731 732 733
		{
			assert(*m_arr != NULL);
			assert(*m_size > 0 );
			return (*m_arr)[0];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
734
		__INLINE const element & front() const 
Kirill Terekhov's avatar
Kirill Terekhov committed
735 736 737 738 739
		{
			assert(*m_arr != NULL);
			assert(*m_size > 0 );
			return (*m_arr)[0];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
740
		__INLINE size_type capacity() { return array<element>::growth_formula(*m_size); }
Kirill Terekhov's avatar
Kirill Terekhov committed
741
		__INLINE bool empty() const { if( *m_size ) return false; return true; }
Kirill Terekhov's avatar
Kirill Terekhov committed
742
		void resize(size_type n, element c = element() )
Kirill Terekhov's avatar
Kirill Terekhov committed
743
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
744
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
745
			size_type oldsize = *m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
746
			*m_size = n;
Kirill Terekhov's avatar
Kirill Terekhov committed
747
			for(size_type i = *m_size; i < oldsize; i++) (*m_arr)[i].~element(); //delete elements, located over the size
Kirill Terekhov's avatar
Kirill Terekhov committed
748 749
			if( *m_size > 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
750 751
				if( array<element>::growth_formula(oldsize) != array<element>::growth_formula(*m_size) )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*array<element>::growth_formula(*m_size)));
Kirill Terekhov's avatar
Kirill Terekhov committed
752
				assert( (*m_arr) != NULL );
Kirill Terekhov's avatar
Kirill Terekhov committed
753
				for(size_type i = oldsize; i < *m_size; i++) new ((*m_arr)+i) element(c); //initialize extra entities
Kirill Terekhov's avatar
Kirill Terekhov committed
754 755 756 757 758 759 760
			}
			else
			{
				free(*m_arr);
				*m_arr = NULL;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
761
		__INLINE size_type size() const {return *m_size;}
Kirill Terekhov's avatar
Kirill Terekhov committed
762 763
		void clear() 
		{ 
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
764
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
765
			for(size_type i = 0; i < *m_size; i++) (*m_arr)[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
766 767 768 769 770 771 772
			*m_size = 0; 
			if( *m_arr ) free(*m_arr); 
			*m_arr = NULL; 
		}
		void swap(shell<element> & other)
		{
			element * t_m_arr = *m_arr;
Kirill Terekhov's avatar
Kirill Terekhov committed
773
			size_type t_m_size = *m_size;
Kirill Terekhov's avatar
Kirill Terekhov committed
774 775 776 777 778
			*m_arr = *other.m_arr;
			*m_size = *other.m_size;
			*other.m_arr = t_m_arr;
			*other.m_size = t_m_size;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
779 780 781 782 783 784 785 786
		__INLINE iterator begin() { return *m_arr; }
		__INLINE iterator end() { return *m_arr+(*m_size); }
		__INLINE const_iterator begin() const { return *m_arr; }
		__INLINE const_iterator end() const { return *m_arr+(*m_size); }
		__INLINE reverse_iterator rbegin() { return reverse_iterator(*m_arr+(*m_size)-1); }
		__INLINE reverse_iterator rend() { return reverse_iterator(*m_arr-1); }
		__INLINE const_reverse_iterator rbegin() const { return const_reverse_iterator(*m_arr+(*m_size)-1); }
		__INLINE const_reverse_iterator rend() const { return const_reverse_iterator(*m_arr-1); }
Kirill Terekhov's avatar
Kirill Terekhov committed
787 788
		iterator erase(iterator pos) 
		{ 
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
789
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
790 791 792 793 794 795 796
			ptrdiff_t d = pos-begin();
			ptrdiff_t s = iterator(*m_arr+(*m_size)-1)-pos;
			(*pos).~element();
			(*m_size)--;
			if( (*m_size) > 0 )
			{
				if( s > 0 )memmove(*m_arr+d,*m_arr+d+1,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
797 798 799 800 801 802 803 804
#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
				size_type gf = array<element>::growth_formula(*m_size);
				if( (*m_size)+1 > gf )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*gf));
#else
				if( (((*m_size)+1) & ((*m_size)-1)) == 1 || (*m_size) == 1)
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*(*m_size)));
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
805 806 807 808 809 810 811 812 813 814 815
				assert((*m_arr) != NULL);
			}
			else
			{
				free(*m_arr);
				*m_arr = NULL;
			}
			return (*m_arr)+d;
		}
		iterator erase(iterator b, iterator e)
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
816
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
817 818 819 820 821 822 823
			ptrdiff_t d = b-begin();
			ptrdiff_t s = end()-e;
			ptrdiff_t n = e-b;
			for(iterator i = b; i != e; i++) (*i).~element();
			(*m_size) -= n;
			if( (*m_size) > 0 )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
824 825 826 827
				if( s > 0 ) memmove(*m_arr+d,*m_arr+d+n,sizeof(element)*s);	
				size_type gf = array<element>::growth_formula(*m_size);
				if( (*m_size)+n > gf )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*gf));
Kirill Terekhov's avatar
Kirill Terekhov committed
828 829 830 831 832 833 834 835 836 837 838
				assert((*m_arr) != NULL);
			}
			else
			{
				free(*m_arr);
				*m_arr = NULL;
			}
			return (*m_arr)+d;
		}
		iterator insert(iterator pos, const element & x)
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
839
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
840 841 842 843 844 845 846 847
			if( static_cast<void *>(pos) == NULL )
			{
				assert((*m_arr) == NULL);
				pos = iterator((*m_arr) = static_cast<element *>(malloc(sizeof(element))));
				assert((*m_arr) != NULL);
			}
			ptrdiff_t d = pos-begin();
			ptrdiff_t s = end()-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861

#if !defined(USE_OPTIMIZED_ARRAY_ALLOCATION)
			//unoptimized variant
			if( (*m_size)+1 > array<element>::growth_formula(*m_size) )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*array<element>::growth_formula(++(*m_size))));
			else ++(*m_size);
#else
			//optimized for current growth_formula
			if( *m_size < 2 )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*(++(*m_size))));
			else if( (((*m_size)+1) & ((*m_size)-1)) == 1 )
				*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*((*m_size)++ << 1)));
			else ++(*m_size);
#endif
Kirill Terekhov's avatar
Kirill Terekhov committed
862 863 864 865 866
			assert((*m_arr) != NULL);
			if( s > 0 ) memmove((*m_arr)+d+1,(*m_arr)+d,sizeof(element)*s);
			new ((*m_arr)+d) element(x);
			return (*m_arr)+d;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
867
		void insert(iterator pos, size_type n, const element & x)
Kirill Terekhov's avatar
Kirill Terekhov committed
868
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
869
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
870 871 872 873 874 875 876 877 878 879
			if( n > 0 )
			{
				if( static_cast<void *>(pos) == NULL)
				{
					assert((*m_arr) == NULL);
					pos = iterator((*m_arr) = static_cast<element *>(malloc(sizeof(element))));
					assert((*m_arr) != NULL);
				}
				ptrdiff_t d = pos-iterator(*m_arr);
				ptrdiff_t s = iterator((*m_arr)+(*m_size))-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
880 881 882

				if( (*m_size)+n > array<element>::growth_formula(*m_size) )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*array<element>::growth_formula((*m_size)+n)));
Kirill Terekhov's avatar
Kirill Terekhov committed
883
				(*m_size)+=n;
Kirill Terekhov's avatar
Kirill Terekhov committed
884
				
Kirill Terekhov's avatar
Kirill Terekhov committed
885 886
				assert((*m_arr) != NULL);
				if( s > 0 ) memmove((*m_arr)+d+n,(*m_arr)+d,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
887
				for(size_type i = 0; i < n; i++) new ((*m_arr)+d+i) element(x);
Kirill Terekhov's avatar
Kirill Terekhov committed
888 889 890 891 892
			}
		}
		template <class InputIterator>
		void insert(iterator pos, InputIterator first, InputIterator last)
		{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
893
			assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
894
			ptrdiff_t n = static_cast<ptrdiff_t>(std::distance(first,last));
Kirill Terekhov's avatar
Kirill Terekhov committed
895 896 897 898 899 900 901 902 903 904
			if( n > 0 )
			{
				if( static_cast<void *>(pos) == NULL)
				{
					assert((*m_arr) == NULL);
					pos = iterator((*m_arr) = static_cast<element *>(malloc(sizeof(element))));
					assert((*m_arr) != NULL);
				}
				ptrdiff_t d = pos-iterator(*m_arr);
				ptrdiff_t s = iterator((*m_arr)+(*m_size))-pos;
Kirill Terekhov's avatar
Kirill Terekhov committed
905 906 907 908 909 910


				if( (*m_size)+n > array<element>::growth_formula(*m_size) )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*array<element>::growth_formula((*m_size)+static_cast<size_type>(n))));
				(*m_size)+=static_cast<size_type>(n);

Kirill Terekhov's avatar
Kirill Terekhov committed
911 912 913 914
				assert((*m_arr) != NULL);
				if( s > 0 ) memmove((*m_arr)+d+n,(*m_arr)+d,sizeof(element)*s);
				{
					InputIterator it = first;
Kirill Terekhov's avatar
Kirill Terekhov committed
915
					size_type i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
916 917 918 919 920 921 922
					while(it != last) new ((*m_arr)+d+i++) element(*it++);
				}
			}
		}
		template <class InputIterator>
		void replace(iterator m_first, iterator m_last, InputIterator first, InputIterator last)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
923
			ptrdiff_t n = static_cast<ptrdiff_t>(std::distance(first,last));
Kirill Terekhov's avatar
Kirill Terekhov committed
924 925 926 927 928 929 930 931 932 933 934 935
			if( static_cast<void *>(m_first) == NULL)
			{
				assert((*m_arr)==NULL);
				m_first = m_last = iterator((*m_arr) = static_cast<element *>(malloc(sizeof(element))));
				assert((*m_arr)!=NULL);
			}
			ptrdiff_t q = m_last-m_first; 
			ptrdiff_t d = m_first-iterator(*m_arr);
			ptrdiff_t s = iterator((*m_arr)+(*m_size))-m_last;
			for(iterator it = m_first; it != m_last; it++) (*it).~element();
			if( n-q != 0 )
			{
Kirill Terekhov's avatar
Fixes  
Kirill Terekhov committed
936
				assert( !fixed ); // array size is fixed
Kirill Terekhov's avatar
Kirill Terekhov committed
937 938 939 940
				size_type gf = array<element>::growth_formula((*m_size)+static_cast<size_type>(n-q));
				if( gf != array<element>::growth_formula(*m_size) )
					*m_arr = static_cast<element *>(realloc(*m_arr,sizeof(element)*gf));
				(*m_size)+=static_cast<size_type>(n-q);
Kirill Terekhov's avatar
Kirill Terekhov committed
941
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
942 943
			if( s > 0 ) 
				memmove((*m_arr)+d+n,(*m_arr)+d+q,sizeof(element)*s);
Kirill Terekhov's avatar
Kirill Terekhov committed
944 945
			{
				InputIterator it = first;
Kirill Terekhov's avatar
Kirill Terekhov committed
946
				size_type i = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
				while(it != last) new ((*m_arr)+d+i++) element(*it++);
			}
		}
	};


	
	template <typename IndType,typename ValType>	
	class sparse_data
	{
	public:
		typedef unsigned enumerate;
		static const enumerate prealloc = 4;
		typedef struct pair_t
		{
			IndType first;
			ValType second; 
			pair_t() :first(0),second(0.0) {}
			pair_t(IndType first, ValType second) :first(0), second(0.0) {}
		} pair;
		typedef pair * iterator;
		typedef const iterator const_iterator;
	private:
		static int comparator(const void * pa, const void * pb)
		{
			pair * a = (pair *)pa;
			pair * b = (pair *)pb;
			return a->first - b->first;
		}
		pair * array;
		enumerate arr_size;
		enumerate arr_alloc;
		void test_allocate()
		{
			if( arr_size > arr_alloc )
			{
				enumerate old_arr_alloc = arr_alloc;
				while(arr_size > arr_alloc) arr_alloc = arr_alloc << 1;
				array = static_cast<pair *>(realloc(array,arr_alloc*sizeof(pair)));
				assert(array != NULL);
				for (enumerate i = old_arr_alloc; i < arr_alloc; i++) array[i].first = std::numeric_limits<IndType>::max();
				//memset(array+old_arr_alloc,0xff,sizeof(pair)*(arr_alloc-old_arr_alloc));
			}
		}
	public:
		void swap(sparse_data<IndType, ValType> & other)
		{
			pair * tmp = array;
			array = other.array;
			other.array = tmp;
			enumerate itmp = arr_size;
			arr_size = other.arr_size;
			other.arr_size = itmp;
			itmp = arr_alloc;
			arr_alloc = other.arr_alloc;
			other.arr_alloc = itmp;
		}
		void reserve(enumerate size)
		{
			enumerate new_alloc = 1;
			while( new_alloc < size ) new_alloc = new_alloc << 1;
			array = static_cast<pair *>(realloc(array,new_alloc*sizeof(pair)));
			assert(array != NULL);
			for (enumerate i = arr_alloc; i < new_alloc; i++) array[i].first = std::numeric_limits<IndType>::max();
			//memset(array+arr_alloc,0xff,sizeof(pair)*(new_alloc-arr_alloc));
			arr_alloc = new_alloc;
		}
		iterator lower_bound(IndType ind)
		{
			/*
			if( arr_size < 16 )
			{	
				for(enumerate i = 0; i < arr_size; i++)
					if( array[i].first >= ind ) return array+i;
				return array+arr_size;
			}
			*/			
			unsigned k = 0;
			for(unsigned b = arr_alloc >> 1; b ; b = b >> 1)
			{
				unsigned j = k | b;
				if( array[j].first <= ind ) k = j;
			}
			if( array[k].first < ind ) k++;	
			
			return array+k;
		}
		iterator find(IndType ind)
		{
			iterator k = lower_bound(ind);
			if( k->first == ind ) return k;
			return end();
		}
		iterator insert(iterator pos, const IndType & x)
		{
			assert(pos == end() || x < pos->first);//check here that we don't break the order
			ptrdiff_t d = pos-array;
			ptrdiff_t s = arr_size-d;
			arr_size++;
			test_allocate();
			if( s ) memmove(array+d+1,array+d,sizeof(pair)*s);
			(array+d)->first = x;
			new (&(array[d].second)) ValType();
			return array+d;
		}
		void push_back(const pair & in)
		{
			assert(arr_size == 0 || in.first < (array+arr_size-1)->first);//check here that we don't break the order
			arr_size++;
			test_allocate();
			(array+arr_size-1)->first = in.first;
			(array+arr_size-1)->second = in.second;
		}
		iterator erase(iterator pos)
		{ 
			ptrdiff_t d = pos-array;
			ptrdiff_t s = (array+arr_size-1)-pos;
			(pos->second).~ValType();
			memmove(array+d,array+d+1,sizeof(pair)*s);
			arr_size--;
			return array+d;
		}
		sparse_data(pair * first, pair * last)
		{
			arr_size = last-first;
			arr_alloc = static_cast<enumerate>(prealloc);
			if( arr_size <= arr_alloc )
				array = static_cast<pair *>(malloc(arr_alloc*sizeof(pair)));
			else
			{
				array = NULL;
				test_allocate();
			}
			assert(array != NULL);
			memcpy(array,first,sizeof(pair)*arr_size);
			for (enumerate i = arr_alloc; i < arr_size; i++) array[i].first = std::numeric_limits<IndType>::max();
			//memset(array+arr_size,0xff,sizeof(pair)*(arr_alloc-arr_size));
			bool need_sort = false;
			for(enumerate k = 1; k < arr_size; k++)
				if( array[k].first < array[k-1].first )
				{
					need_sort = true;
					break;
				}
			if( need_sort ) qsort(array,sizeof(pair),arr_size,comparator);
		}
		sparse_data()
		{
			arr_size = 0;
			arr_alloc = static_cast<enumerate>(prealloc);
			array = static_cast<pair *>(malloc(sizeof(pair)*arr_alloc));
			assert(array != NULL);
			for (enumerate i = 0; i < arr_alloc; i++) array[i].first = std::numeric_limits<IndType>::max();
			//memset(array,0xff,sizeof(pair)*arr_alloc);
		}
		sparse_data(const sparse_data & other)
		{
			arr_size = other.arr_size;
			arr_alloc = other.arr_alloc;
			array = static_cast<pair *>(malloc(arr_alloc*sizeof(pair)));
			assert(array != NULL);
			memcpy(array,other.array,other.arr_alloc*sizeof(pair));
		}
		~sparse_data()
		{
			for(iterator i = begin(); i != end(); i++) (i->second).~ValType();
			free(array);
			arr_size = arr_alloc = 0;
		}
		sparse_data & operator =(sparse_data const & other)
		{
			if( &other != this )
			{
				for(iterator i = begin(); i != end(); i++) (i->second).~ValType();
				arr_size = other.arr_size;
				arr_alloc = other.arr_alloc;
				array = static_cast<pair *>(realloc(array,arr_alloc*sizeof(pair)));
				assert(array != NULL);
				memcpy(array,other.array,arr_alloc*sizeof(pair));
			}
			return *this;
		}
		ValType & operator [](IndType row)
		{
			iterator q = lower_bound(row);
			if( q != end() && q->first == row ) return q->second;
			return insert(q,row)->second;
		}
		ValType operator [](IndType row) const
		{
			iterator q = lower_bound(row);
			assert(q != end() && q->first == row);
			return q->second;
		}
		enumerate size() const { return arr_size; }
		bool empty() const {return size() == 0; }
		iterator begin() { return array; }
		const_iterator begin() const { return array; }
		iterator end() { return array+arr_size; }
		const_iterator end() const { return array+arr_size; }
		void clear()
		{
			for(iterator i = begin(); i != end(); i++) (i->second).~ValType();
			for (enumerate i = 0; i < arr_size; i++) array[i].first = std::numeric_limits<IndType>::max();
			//memset(array,0xff,sizeof(pair)*arr_size);
			arr_size = 0;	
		}
		enumerate capacity() {return arr_alloc;}
		
	};
	
	
	
	
	template<typename IndType,typename ValType>
	class interval
	{
	public:
		typedef ValType * iterator;
		typedef ValType const * const_iterator;
		//typedef const iterator const_iterator;
	private:
		ValType * array;
		IndType beg_index, end_index;
	public:
		void clear()
		{
			for (IndType i = beg_index; i < end_index; i++) (array[i]).~ValType();
			if (beg_index != end_index) free(array + beg_index);
			array = NULL;
			beg_index = end_index = 0;
		}
		void swap(interval<IndType, ValType> & other)
		{
			{
				IndType tmp = beg_index;
				beg_index = other.beg_index;
				other.beg_index = tmp;
				tmp = end_index;
				end_index = other.end_index;
				other.end_index = tmp;
			}
			{
				ValType * tmp = array;
				array = other.array;
				other.array = tmp;
			}
		}
		interval()
		{
			beg_index = 0;
			end_index = 0;
			array = NULL;//static_cast<ValType *>(malloc(sizeof(ValType)*(end_index-beg_index)));
			//assert(array != NULL);
			//array = array - beg_index;
			//for(IndType i = beg_index; i != end_index; ++i) new (array+i) ValType();
		}
		interval(IndType beg)
		{
			beg_index = beg;
			end_index = beg_index;//+2;
			array = NULL; //static_cast<ValType *>(malloc(sizeof(ValType)*(end_index-beg_index)));
			//assert(array != NULL);
			//array = array - beg_index;
			//for(IndType i = beg_index; i != end_index; ++i) new (array+i) ValType();
		}
		interval(IndType beg, IndType end, ValType c = ValType())
		{
			beg_index = beg;
			end_index = end;
			if (beg != end)
			{
				array = static_cast<ValType *>(malloc(sizeof(ValType)*(end_index - beg_index)));
				assert(array != NULL);
				array = array - beg_index;
				for (IndType i = beg_index; i < end_index; ++i) new (array + i) ValType(c);
Kirill Terekhov's avatar
Kirill Terekhov committed
1223 1224

				//std::cout << __FUNCTION__ << " address " << array << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1225 1226 1227 1228 1229
			}
			else array = NULL;
		}
		interval(const interval & other)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1230
			//std::cout << __FUNCTION__ << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1231 1232 1233 1234 1235 1236 1237
			beg_index = other.beg_index;
			end_index = other.end_index;
			if( beg_index != end_index )
			{
				array = static_cast<ValType *>(malloc(sizeof(ValType)*(end_index-beg_index)));
				assert(array != NULL);
				array = array - beg_index;
Kirill Terekhov's avatar
Kirill Terekhov committed
1238 1239 1240 1241 1242
				for(IndType i = beg_index; i < end_index; ++i) 
				{
					new (array+i) ValType(other.array[i]);
				}
				//std::cout << __FUNCTION__ << " address " << array << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1243 1244 1245 1246 1247
			}
			else array = NULL;
		}
		~interval()
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1248
			//std::cout << __FUNCTION__ << " delete address " << array << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
			for(IndType i = beg_index; i < end_index; i++) (array[i]).~ValType();
			if( beg_index != end_index ) free(array+beg_index);
			array = NULL;
		}
		interval & operator =(interval const & other)
		{
			if( &other != this )
			{
				for(iterator i = begin(); i != end(); ++i) (*i).~ValType();
				beg_index = other.beg_index;
				end_index = other.end_index;
				if( beg_index != end_index )
				{
					array = static_cast<ValType *>(realloc(array+beg_index,sizeof(ValType)*(end_index-beg_index)));
					assert(array != NULL);
					array = array - beg_index;
					for(IndType i = beg_index; i < end_index; ++i) new (array+i) ValType(other.array[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
1266
					//std::cout << __FUNCTION__ << " address " << array << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
				}
				else 
				{
					free(array+beg_index);
					array = NULL;
				}
			}
			return *this;
		}
		ValType & at(IndType row)
		{
			assert(row >= beg_index);
			assert(row < end_index);
			return array[row];
		}
		const ValType & at(IndType row) const
		{
			assert(row >= beg_index);
			assert(row < end_index);
			return array[row];
		}
		ValType & operator [](IndType row)
		{
			//std::cout << "pos: " << row << std::endl;
			/*
			if( row >= end_index )
			{
				IndType new_end_index = 1;
				IndType temp = row-beg_index;
				while( new_end_index <= temp ) new_end_index = new_end_index << 1;
				new_end_index += beg_index;
				//std::cout << "end: " << end_index << " new end: " << new_end_index << std::endl;
				array = static_cast<ValType *>(realloc(array,sizeof(ValType)*(new_end_index-beg_index)));
				IndType end = new_end_index-beg_index;
				for(IndType i = end_index-beg_index; i != end; ++i) new (array+i) ValType();
				end_index = new_end_index;
			}
			if( row >= last_index ) last_index = row+1;
			*/
			assert(row >= beg_index );
			assert(row < end_index );
			return array[row];
		}
		const ValType & operator [](IndType row) const
		{
			assert(row >= beg_index );
			assert(row < end_index );
			return array[row];
		}
		void set_interval_beg(IndType beg)
		{
			IndType shift = beg-beg_index;
			shift_interval(shift);
		}
		void set_interval_end(IndType end)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1323
			if( end == end_index ) return;
Kirill Terekhov's avatar
Kirill Terekhov committed
1324 1325
			if( beg_index != end )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1326 1327 1328 1329 1330 1331 1332 1333 1334
				ValType * array_new = static_cast<ValType *>(malloc(sizeof(ValType)*(end-beg_index)));
				assert(array_new != NULL);
				array_new = array_new - beg_index;
				for(IndType i = beg_index; i < std::min(end,end_index); ++i) new (array_new+i) ValType(array[i]);
				for(IndType i = end_index; i < end; ++i) new (array_new+i) ValType();
				for(IndType i = beg_index; i < end_index; ++i) array[i].~ValType();

				if( array != NULL ) free(array+beg_index);
				array = array_new;
Kirill Terekhov's avatar
Kirill Terekhov committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
			}
			else
			{
				free(array+beg_index);
				array = NULL;
			}
			end_index = end;
		}
		
		void shift_interval(IndType shift)
		{
			array = array + beg_index;
			beg_index += shift;
			end_index += shift;
			array = array - beg_index;
		}
		iterator begin() {return array+beg_index;}
		const_iterator begin() const {return array+beg_index;}
		//const_iterator begin() {return array;}
		iterator end() {return array + end_index;}
		const_iterator end() const {return array + end_index;}
		//const_iterator end() {return array + (end_index-end_index);}
		IndType get_interval_beg() const { return beg_index; }
		IndType get_interval_end() const { return end_index; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1359
		int size() const {return end_index - beg_index;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
		bool empty() const {return beg_index == end_index;}
	};
	
	//this version is safe for std::map
	/*
	template<typename IndType,typename ValType>
	class interval
	{
	public:
		typedef ValType * iterator;
		typedef ValType const * const_iterator;
	private:
		ValType * array;
		IndType beg_index, end_index, last_index;
	public:
		interval()
		{
			beg_index = 0;
			last_index = beg_index;
			end_index = 0;
			array = NULL;
		}
		interval(IndType beg)
		{
			beg_index = beg;
			last_index = beg_index;
			end_index = beg_index;
			array = NULL;
		}
		interval(IndType beg, IndType end)
		{
			beg_index = beg;
			last_index = end;
			end_index = end;
			if( end_index-beg_index > 0 )
			{
				array = static_cast<ValType *>(malloc(sizeof(ValType)*(end_index-beg_index)));
				assert(array != NULL);
				IndType cycle_end = end_index-beg_index;
				for(IndType i = 0; i != cycle_end; ++i) new (array+i) ValType();
			}
			else array = NULL;
		}
		interval(const interval & other)
		{
			beg_index = other.beg_index;
			last_index = other.last_index;
			end_index = other.end_index;
			array = static_cast<ValType *>(malloc(sizeof(ValType)*(end_index-beg_index)));
			assert(array != NULL);
			IndType end = end_index-beg_index;
			for(IndType i = 0; i != end; ++i) 
			{
				//std::cout << this << " " << __FILE__  << ":" << __LINE__ << " call constructor " << i << " obj " << array+i << std::endl;
				new (array+i) ValType(other.array[i]);
			}
		}
		~interval()
		{
			//for(iterator i = begin(); i != end(); ++i) (*i).~ValType();
			for(IndType i = 0; i < end_index-beg_index; i++) (array[i]).~ValType();
			free(array);
		}
		interval & operator =(interval const & other)
		{
			if( &other != this )
			{
				for(iterator i = begin(); i != end(); ++i) (*i).~ValType();
				beg_index = other.beg_index;
				last_index = other.last_index;
				end_index = other.end_index;
				array = static_cast<ValType *>(realloc(array,sizeof(ValType)*(end_index-beg_index)));
				assert(array != NULL);
				IndType end = end_index-beg_index;
				for(IndType i = 0; i != end; ++i) 
				{
					//std::cout << this << " " << __FILE__  << ":" << __LINE__ << " call constructor " << i << " obj " << array+i << std::endl;
					new (array+i) ValType(other.array[i]);
				}
			}
			return *this;
		}
		ValType & operator [](IndType row)
		{
			//std::cout << "pos: " << row << std::endl;
			if( row >= end_index )
			{
				IndType new_end_index = 1;
				IndType temp = row-beg_index;
				while( new_end_index <= temp ) new_end_index = new_end_index << 1;
				new_end_index += beg_index;
				ValType * array_new = static_cast<ValType *>(malloc(sizeof(ValType)*(new_end_index-beg_index)));
				assert(array_new != NULL);
				for(IndType i = 0; i != end_index-beg_index; ++i) 
				{
					new (array_new+i) ValType(*(array+i));
					(*(array+i)).~ValType();
				}
				IndType end = new_end_index-beg_index;
				for(IndType i = end_index-beg_index; i != end; ++i) 
				{
					//std::cout << this << " " << __FILE__  << ":" << __LINE__ << " call constructor " << i << " obj " << array+i << std::endl;
					new (array_new+i) ValType();
				}
				end_index = new_end_index;
				free(array);
				array = array_new;
			}
			if( row >= last_index ) last_index = row+1;
			return array[row-beg_index];
		}
		const ValType & operator [](IndType row) const
		{
			assert(row >= beg_index );
			assert(row < end_index );
			return array[row-beg_index];
		}
		bool empty() const {return beg_index == last_index;}
		void set_interval_beg(IndType beg)
		{
			IndType shift = beg-beg_index;
			shift_interval(shift);
		}
		void set_interval_end(IndType end)
		{
			
			if( end > end_index )
			{
				ValType * array_new = static_cast<ValType *>(malloc(sizeof(ValType)*(end-beg_index)));
				assert(array_new != NULL);
				IndType cycle_end = end_index-beg_index;
				for(IndType i = 0; i != cycle_end; ++i) 
				{
					new (array_new+i) ValType(*(array+i));
					(*(array+i)).~ValType();
				}
				cycle_end = end-beg_index;
				for(IndType i = end_index-beg_index; i != cycle_end; ++i) 
				{
					//std::cout << this << " " << __FILE__  << ":" << __LINE__ << " call constructor " << i << " obj " << array+i << std::endl;
					new (array_new+i) ValType();
				}
				end_index = end;
				free(array);
				array = array_new;
			}
			last_index = end;
		}
		
		void shift_interval(IndType shift)
		{
			beg_index += shift;
			end_index += shift;
			last_index += shift;
		}
		iterator begin() {return array;}
		const_iterator begin() const {return array;}
		iterator end() {return array + (last_index-beg_index);}
		
		const_iterator end() const {return array + (last_index-beg_index);}
		IndType get_interval_beg() const { return beg_index; }
		IndType get_interval_end() const { return last_index; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1522
		int size() const {return last_index - beg_index;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1523 1524 1525 1526 1527 1528
	};
	*/
	template<typename element, unsigned int stacked>
	class dynarray
	{
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
1529
		typedef size_t size_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
1530 1531 1532 1533 1534 1535 1536 1537 1538
		template<typename dtype>
		class _iterator
		{
		private:
			dtype * e;
		public:
			typedef dtype * pointer;
			typedef dtype & reference;
			typedef dtype value_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
1539
			typedef size_type difference_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
1540 1541 1542 1543 1544
			typedef std::random_access_iterator_tag iterator_category;
			_iterator():e(NULL){}
			_iterator(dtype * i):e(i){}
			_iterator(const _iterator & other){e = other.e;}
			~_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
1545 1546 1547 1548
			_iterator operator -(size_type n) { return _iterator(e-n); }
			_iterator & operator -=(size_type n) { e-=n; return *this; }
			_iterator operator +(size_type n) { return _iterator(e+n); }
			_iterator & operator +=(size_type n) { e+=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1549 1550 1551 1552
			_iterator & operator ++(){ ++e; return *this;}
			_iterator operator ++(int){ return _iterator(e++); }
			_iterator & operator --(){ --e; return *this; }
			_iterator operator --(int){ return _iterator(e--); }
Kirill Terekhov's avatar
Kirill Terekhov committed
1553
			size_type operator -(const _iterator & other) const {return static_cast<size_type>(e-other.e);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
			dtype & operator *() { return *e; }
			dtype * operator ->() { return e; }
			_iterator & operator =(_iterator const & other) { e = other.e; return *this; }
			bool operator ==(const _iterator & other) { return e == other.e;}
			bool operator !=(const _iterator & other) { return e != other.e;}
			bool operator <(const _iterator & other) { return e < other.e;}
			bool operator >(const _iterator & other) { return e > other.e;}
			bool operator <=(const _iterator & other) { return e <= other.e;}
			bool operator >=(const _iterator & other) { return e >= other.e;}
			operator void *() {return static_cast<void *> (e);}
			void * cast_to_void(){return static_cast<void *>(e);}
		};
		typedef _iterator<element> iterator;
		typedef _iterator<const element> const_iterator;
		template<typename dtype>
		class _reverse_iterator
		{
		private:
			dtype * e;
		public:
			typedef dtype * pointer;
			typedef dtype & reference;
			typedef dtype value_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
1577
			typedef size_type difference_type;
Kirill Terekhov's avatar
Kirill Terekhov committed
1578 1579 1580 1581 1582
			typedef std::random_access_iterator_tag iterator_category;
			_reverse_iterator():e(NULL){}
			_reverse_iterator(dtype * i):e(i){}
			_reverse_iterator(const _reverse_iterator & other){e = other.e;}
			~_reverse_iterator() {};
Kirill Terekhov's avatar
Kirill Terekhov committed
1583 1584 1585 1586
			_reverse_iterator operator -(size_type n) { return _reverse_iterator(e+n); }
			_reverse_iterator & operator -=(size_type n) { e+=n; return *this; }
			_reverse_iterator operator +(size_type n) {return _reverse_iterator(e-n); }
			_reverse_iterator & operator +=(size_type n) { e-=n; return *this; }
Kirill Terekhov's avatar
Kirill Terekhov committed
1587 1588 1589 1590
			_reverse_iterator & operator ++(){ --e; return *this;}
			_reverse_iterator operator ++(int){ return _reverse_iterator(e--); }
			_reverse_iterator & operator --(){ ++e; return *this; }
			_reverse_iterator operator --(int){ return _reverse_iterator(e++); }
Kirill Terekhov's avatar
Kirill Terekhov committed
1591
			size_type operator -(const _reverse_iterator & other) const {return static_cast<size_type>(other.e-e);}
Kirill Terekhov's avatar
Kirill Terekhov committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
			dtype & operator *() { return *e; }
			dtype * operator ->() { return e; }
			_reverse_iterator & operator =(_reverse_iterator const & other) { e = other.e; return *this;}
			bool operator ==(const _reverse_iterator & other) { return e == other.e;}
			bool operator !=(const _reverse_iterator & other) { return e != other.e;}
			bool operator <(const _reverse_iterator & other) { return e < other.e;}
			bool operator >(const _reverse_iterator & other) { return e > other.e;}
			bool operator <=(const _reverse_iterator & other) { return e <= other.e;}
			bool operator >=(const _reverse_iterator & other) { return e >= other.e;}
			operator void *() {return static_cast<void *> (e);}
			void * cast_to_void(){return static_cast<void *>(e);}
		};
		typedef _reverse_iterator<element> reverse_iterator;
		typedef _reverse_iterator<const element> const_reverse_iterator;
	private:
		element stack[stacked];
		element * pbegin;
		element * pend;
		element * preserved;
Kirill Terekhov's avatar
Kirill Terekhov committed
1611
		void preallocate(size_type n)
Kirill Terekhov's avatar
Kirill Terekhov committed
1612
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1613
			if( n <= static_cast<size_type>(stacked) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1614 1615 1616
			{
				pbegin = stack;
				pend = pbegin + n;
Kirill Terekhov's avatar
Kirill Terekhov committed
1617
				preserved = stack+static_cast<size_type>(stacked);
Kirill Terekhov's avatar
Kirill Terekhov committed
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
			}
			else
			{
				pbegin = static_cast<element *>(malloc(sizeof(element)*n));
				assert(pbegin != NULL);
				pend = pbegin+n;
				preserved = pbegin+n;
			}
		}
	public:
Kirill Terekhov's avatar
Kirill Terekhov committed
1628 1629
		__INLINE element * data() {return pbegin;}
		__INLINE const element * data() const {return pbegin;}
Kirill Terekhov's avatar
Kirill Terekhov committed
1630 1631 1632 1633 1634 1635 1636 1637 1638
		void report_addr()
		{
			std::cout << "stack:     " << &stack << std::endl;
			std::cout << "pbegin:    " << pbegin << std::endl;
			std::cout << "pend:      " << pend << std::endl;
			std::cout << "preserved: " << preserved << std::endl;
			std::cout << "size:      " << pend-pbegin << std::endl;
			std::cout << "reserved:  " << preserved-pbegin << std::endl;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1639
		void reserve(size_type n)
Kirill Terekhov's avatar
Kirill Terekhov committed
1640 1641
		{
			//std::cout << n << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1642 1643
			size_type k = size();
			if( n > static_cast<size_type>(stacked) )
Kirill Terekhov's avatar
Kirill Terekhov committed
1644
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1645
				for(size_type i = n; i < k; i++) pbegin[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
1646 1647 1648 1649
				if( pbegin == stack )
				{
					pbegin = static_cast<element *>(malloc(sizeof(element)*n));
					assert(pbegin != NULL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1650
					for(size_type i = 0; i < k; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1651 1652 1653 1654 1655 1656 1657 1658 1659
					{
						new (pbegin+i) element(stack[i]);
						stack[i].~element();
					}
				}
				else
				{
					element * pbegin_new = static_cast<element *>(malloc(sizeof(element)*n));
					assert(pbegin_new != NULL);
Kirill Terekhov's avatar
Kirill Terekhov committed
1660
					for(size_type i = 0; i < k; i++) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
					{
						new (pbegin_new+i) element(pbegin[i]);
						pbegin[i].~element();
					}
					free(pbegin);
					pbegin = pbegin_new;
				}
				pend = pbegin+ (k < n ? k : n);
				preserved = pbegin + n;
			}
			/*
			else if( pbegin != stack )
			{
				memcpy(stack,pbegin,sizeof(element)*n);
Kirill Terekhov's avatar
Kirill Terekhov committed
1675
				for(size_type i = n; i < k; i++) pbegin[i].~element();
Kirill Terekhov's avatar
Kirill Terekhov committed
1676 1677 1678
				free(pbegin);
				pbegin = stack;
				pend = stack+n;
Kirill Terekhov's avatar
Kirill Terekhov committed
1679
				preserved = stack+static_cast<size_type>(stacked);
Kirill Terekhov's avatar
Kirill Terekhov committed
1680 1681 1682 1683 1684 1685
			}
			*/
		}
		dynarray()
		{
			pbegin = pend = stack;
Kirill Terekhov's avatar
Kirill Terekhov committed
1686
			preserved = stack+static_cast<size_type>(stacked);
Kirill Terekhov's avatar
Kirill Terekhov committed
1687
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1688
		dynarray(size_type n,element c = element())
Kirill Terekhov's avatar
Kirill Terekhov committed
1689 1690 1691 1692 1693 1694 1695
		{
			preallocate(n);
			for(element * i = pbegin; i < pend; i++) new (i) element(c);
		}
		template<class InputIterator>
		dynarray(InputIterator first, InputIterator last)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1696
			size_type n = static_cast<size_type>(std::distance(first,last));
Kirill Terekhov's avatar
Kirill Terekhov committed
1697 1698 1699 1700 1701 1702 1703 1704 1705
			preallocate(n);
			{
				InputIterator it = first;
				element * i = pbegin;
				while(it != last) {new (i++) element(*(it++));}
			}
		}
		dynarray(const dynarray & other)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
1706
			//std::cout << __FUNCTION__ << std::endl;
Kirill Terekhov's avatar
Kirill Terekhov committed
1707
			size_type n = other.size();
Kirill Terekhov's avatar
Kirill Terekhov committed
1708
			preallocate(n);
Kirill Terekhov's avatar
Kirill Terekhov committed
1709
			for(size_type k = 0; k < n; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
				new (pbegin+k) element(other.pbegin[k]);
		}
		
		~dynarray()
		{
			for(element * i = pbegin; i < pend; i++) (*i).~element();
			if( pbegin != stack ) free(pbegin);
		}
		dynarray & operator =(dynarray const & other)
		{
			if( this != &other )
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
1722
				size_type n = size();
Kirill Terekhov's avatar
Kirill Terekhov committed
1723 1724 1725 1726
				for(element * i = pbegin; i != pend; ++i) (*i).~element();
				if(pbegin != stack) free(pbegin);
				n = other.size();
				preallocate(n);
Kirill Terekhov's avatar
Kirill Terekhov committed
1727
				for(size_type k = 0; k < n; k++)
Kirill Terekhov's avatar
Kirill Terekhov committed
1728 1729 1730 1731
					new (pbegin+k) element(other.pbegin[k]);
			}
			return *this;
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1732
		__INLINE const element & operator [] (size_type n) const 
Kirill Terekhov's avatar
Kirill Terekhov committed
1733 1734 1735 1736
		{
			assert(pbegin+n < pend);
			return pbegin[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1737
		__INLINE element & operator [] (size_type n) 
Kirill Terekhov's avatar
Kirill Terekhov committed
1738 1739 1740 1741
		{
			assert(pbegin+n < pend);
			return pbegin[n];
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
1742
		__INLINE const element & at (