2009-01-25 17:35:25 +05:00
|
|
|
#ifndef FILE_Array
|
|
|
|
#define FILE_Array
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
/* File: array.hpp */
|
|
|
|
/* Author: Joachim Schoeberl */
|
|
|
|
/* Date: 01. Jun. 95 */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
|
2009-07-20 14:36:36 +06:00
|
|
|
namespace netgen
|
|
|
|
{
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
// template <class T, int B1, int B2> class IndirectArray;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/**
|
|
|
|
A simple array container.
|
|
|
|
Array represented by size and data-pointer.
|
|
|
|
No memory allocation and deallocation, must be provided by user.
|
|
|
|
Helper functions for printing.
|
|
|
|
Optional range check by macro RANGE_CHECK
|
2009-01-13 04:40:13 +05:00
|
|
|
*/
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
template <class T, int BASE = 0>
|
|
|
|
class FlatArray
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// the size
|
|
|
|
int size;
|
|
|
|
/// the data
|
|
|
|
T * data;
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// provide size and memory
|
|
|
|
FlatArray (int asize, T * adata)
|
|
|
|
: size(asize), data(adata) { ; }
|
|
|
|
|
|
|
|
/// the size
|
|
|
|
int Size() const { return size; }
|
|
|
|
|
|
|
|
int Begin() const { return BASE; }
|
|
|
|
int End() const { return size+BASE; }
|
|
|
|
|
|
|
|
/// Access array. BASE-based
|
|
|
|
T & operator[] (int i) const
|
|
|
|
{
|
2009-01-13 04:40:13 +05:00
|
|
|
#ifdef DEBUG
|
2009-10-28 05:04:19 +05:00
|
|
|
if (i-BASE < 0 || i-BASE >= size)
|
|
|
|
cout << "array<" << typeid(T).name() << "> out of range, i = " << i << ", s = " << size << endl;
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
return data[i-BASE];
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// Access array, one-based (old fashioned)
|
|
|
|
T & Elem (int i)
|
|
|
|
{
|
2009-01-13 04:40:13 +05:00
|
|
|
#ifdef DEBUG
|
2009-10-28 05:04:19 +05:00
|
|
|
if (i < 1 || i > size)
|
|
|
|
cout << "Array<" << typeid(T).name()
|
|
|
|
<< ">::Elem out of range, i = " << i
|
|
|
|
<< ", s = " << size << endl;
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
return ((T*)data)[i-1];
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// Access array, one-based (old fashioned)
|
|
|
|
const T & Get (int i) const
|
|
|
|
{
|
2009-01-13 04:40:13 +05:00
|
|
|
#ifdef DEBUG
|
2009-10-28 05:04:19 +05:00
|
|
|
if (i < 1 || i > size)
|
|
|
|
cout << "Array<" << typeid(T).name() << ">::Get out of range, i = " << i
|
|
|
|
<< ", s = " << size << endl;
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
return ((const T*)data)[i-1];
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// Access array, one-based (old fashioned)
|
|
|
|
void Set (int i, const T & el)
|
|
|
|
{
|
2009-01-13 04:40:13 +05:00
|
|
|
#ifdef DEBUG
|
2009-10-28 05:04:19 +05:00
|
|
|
if (i < 1 || i > size)
|
|
|
|
cout << "Array<" << typeid(T).name() << ">::Set out of range, i = " << i
|
|
|
|
<< ", s = " << size << endl;
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
((T*)data)[i-1] = el;
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// access first element
|
|
|
|
T & First () const
|
|
|
|
{
|
|
|
|
return data[0];
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// access last element. check by macro CHECK_RANGE
|
|
|
|
T & Last () const
|
|
|
|
{
|
|
|
|
return data[size-1];
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// Fill array with value val
|
|
|
|
FlatArray & operator= (const T & val)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
data[i] = val;
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// takes range starting from position start of end-start elements
|
|
|
|
const FlatArray<T> Range (int start, int end)
|
|
|
|
{
|
|
|
|
return FlatArray<T> (end-start, data+start);
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// first position of element elem, returns -1 if element not contained in array
|
|
|
|
int Pos(const T & elem) const
|
|
|
|
{
|
|
|
|
int pos = -1;
|
|
|
|
for(int i=0; pos==-1 && i < this->size; i++)
|
|
|
|
if(elem == data[i]) pos = i;
|
|
|
|
return pos;
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// does the array contain element elem ?
|
|
|
|
bool Contains(const T & elem) const
|
|
|
|
{
|
|
|
|
return ( Pos(elem) >= 0 );
|
|
|
|
}
|
|
|
|
};
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
// print array
|
|
|
|
template <class T, int BASE>
|
|
|
|
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a)
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
2009-10-28 05:04:19 +05:00
|
|
|
for (int i = a.Begin(); i < a.End(); i++)
|
|
|
|
s << i << ": " << a[i] << endl;
|
|
|
|
return s;
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/**
|
|
|
|
Dynamic array container.
|
|
|
|
|
|
|
|
Array<T> is an automatically increasing array container.
|
|
|
|
The allocated memory doubles on overflow.
|
|
|
|
Either the container takes care of memory allocation and deallocation,
|
|
|
|
or the user provides one block of data.
|
|
|
|
*/
|
|
|
|
template <class T, int BASE = 0>
|
|
|
|
class Array : public FlatArray<T, BASE>
|
|
|
|
{
|
|
|
|
protected:
|
2011-01-11 01:18:01 +05:00
|
|
|
using FlatArray<T,BASE>::size;
|
|
|
|
using FlatArray<T,BASE>::data;
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// physical size of array
|
|
|
|
int allocsize;
|
|
|
|
/// memory is responsibility of container
|
|
|
|
bool ownmem;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Generate array of logical and physical size asize
|
|
|
|
explicit Array(int asize = 0)
|
|
|
|
: FlatArray<T, BASE> (asize, asize ? new T[asize] : 0)
|
|
|
|
{
|
|
|
|
allocsize = asize;
|
|
|
|
ownmem = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate array in user data
|
|
|
|
Array(int asize, T* adata)
|
|
|
|
: FlatArray<T, BASE> (asize, adata)
|
|
|
|
{
|
|
|
|
allocsize = asize;
|
|
|
|
ownmem = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// array copy
|
|
|
|
explicit Array (const Array<T> & a2)
|
|
|
|
: FlatArray<T, BASE> (a2.Size(), a2.Size() ? new T[a2.Size()] : 0)
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
allocsize = size;
|
2009-10-28 05:04:19 +05:00
|
|
|
ownmem = 1;
|
2011-01-11 01:18:01 +05:00
|
|
|
for (int i = BASE; i < size+BASE; i++)
|
2009-10-28 05:04:19 +05:00
|
|
|
(*this)[i] = a2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// if responsible, deletes memory
|
|
|
|
~Array()
|
|
|
|
{
|
|
|
|
if (ownmem)
|
2011-01-11 01:18:01 +05:00
|
|
|
delete [] data;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Change logical size. If necessary, do reallocation. Keeps contents.
|
|
|
|
void SetSize(int nsize)
|
|
|
|
{
|
|
|
|
if (nsize > allocsize)
|
|
|
|
ReSize (nsize);
|
2011-01-11 01:18:01 +05:00
|
|
|
size = nsize;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Change physical size. Keeps logical size. Keeps contents.
|
|
|
|
void SetAllocSize (int nallocsize)
|
|
|
|
{
|
|
|
|
if (nallocsize > allocsize)
|
|
|
|
ReSize (nallocsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Add element at end of array. reallocation if necessary.
|
|
|
|
int Append (const T & el)
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
if (size == allocsize)
|
|
|
|
ReSize (size+1);
|
|
|
|
data[size] = el;
|
|
|
|
size++;
|
|
|
|
return size;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T2, int B2>
|
|
|
|
void Append (FlatArray<T2, B2> a2)
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
if (size+a2.Size() > allocsize)
|
|
|
|
ReSize (size+a2.Size());
|
2009-10-28 05:04:19 +05:00
|
|
|
for (int i = 0; i < a2.Size(); i++)
|
2011-01-11 01:18:01 +05:00
|
|
|
data[size+i] = a2[i+B2];
|
|
|
|
size += a2.Size();
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Delete element i (0-based). Move last element to position i.
|
|
|
|
void Delete (int i)
|
|
|
|
{
|
2009-01-25 17:35:25 +05:00
|
|
|
#ifdef CHECK_Array_RANGE
|
2009-10-28 05:04:19 +05:00
|
|
|
RangeCheck (i+1);
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2011-01-11 01:18:01 +05:00
|
|
|
data[i] = data[size-1];
|
|
|
|
size--;
|
2009-10-28 05:04:19 +05:00
|
|
|
// DeleteElement (i+1);
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/// Delete element i (1-based). Move last element to position i.
|
|
|
|
void DeleteElement (int i)
|
|
|
|
{
|
2009-01-25 17:35:25 +05:00
|
|
|
#ifdef CHECK_Array_RANGE
|
2009-10-28 05:04:19 +05:00
|
|
|
RangeCheck (i);
|
2009-01-13 04:40:13 +05:00
|
|
|
#endif
|
|
|
|
|
2011-01-11 01:18:01 +05:00
|
|
|
data[i-1] = data[size-1];
|
|
|
|
size--;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete last element.
|
|
|
|
void DeleteLast ()
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
size--;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocate memory
|
|
|
|
void DeleteAll ()
|
|
|
|
{
|
|
|
|
if (ownmem)
|
2011-01-11 01:18:01 +05:00
|
|
|
delete [] data;
|
|
|
|
data = 0;
|
|
|
|
size = allocsize = 0;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fill array with val
|
|
|
|
Array & operator= (const T & val)
|
|
|
|
{
|
|
|
|
FlatArray<T, BASE>::operator= (val);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// array copy
|
|
|
|
Array & operator= (const Array & a2)
|
|
|
|
{
|
|
|
|
SetSize (a2.Size());
|
2011-01-11 01:18:01 +05:00
|
|
|
for (int i = BASE; i < size+BASE; i++)
|
2009-10-28 05:04:19 +05:00
|
|
|
(*this)[i] = a2[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// array copy
|
|
|
|
Array & operator= (const FlatArray<T> & a2)
|
|
|
|
{
|
|
|
|
SetSize (a2.Size());
|
2011-01-11 01:18:01 +05:00
|
|
|
for (int i = BASE; i < size+BASE; i++)
|
2009-10-28 05:04:19 +05:00
|
|
|
(*this)[i] = a2[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// resize array, at least to size minsize. copy contents
|
|
|
|
void ReSize (int minsize)
|
|
|
|
{
|
|
|
|
int nsize = 2 * allocsize;
|
|
|
|
if (nsize < minsize) nsize = minsize;
|
|
|
|
|
2011-01-11 01:18:01 +05:00
|
|
|
if (data)
|
2009-10-28 05:04:19 +05:00
|
|
|
{
|
|
|
|
T * p = new T[nsize];
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2011-01-11 01:18:01 +05:00
|
|
|
int mins = (nsize < size) ? nsize : size;
|
|
|
|
memcpy (p, data, mins * sizeof(T));
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
if (ownmem)
|
2011-01-11 01:18:01 +05:00
|
|
|
delete [] data;
|
2009-10-28 05:04:19 +05:00
|
|
|
ownmem = 1;
|
2011-01-11 01:18:01 +05:00
|
|
|
data = p;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
data = new T[nsize];
|
2009-10-28 05:04:19 +05:00
|
|
|
ownmem = 1;
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
allocsize = nsize;
|
|
|
|
}
|
|
|
|
};
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
template <class T, int S>
|
|
|
|
class ArrayMem : public Array<T>
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
using Array<T>::size;
|
|
|
|
using Array<T>::data;
|
|
|
|
using Array<T>::ownmem;
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
// T mem[S]; // Intel C++ calls dummy constructor
|
|
|
|
// char mem[S*sizeof(T)];
|
|
|
|
double mem[(S*sizeof(T)+7) / 8];
|
|
|
|
public:
|
|
|
|
/// Generate array of logical and physical size asize
|
|
|
|
explicit ArrayMem(int asize = 0)
|
|
|
|
: Array<T> (S, static_cast<T*> (static_cast<void*>(&mem[0])))
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
size = asize;
|
2009-10-28 05:04:19 +05:00
|
|
|
if (asize > S)
|
|
|
|
{
|
2011-01-11 01:18:01 +05:00
|
|
|
data = new T[asize];
|
|
|
|
ownmem = 1;
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
2011-01-11 01:18:01 +05:00
|
|
|
// SetSize (asize);
|
2009-10-28 05:04:19 +05:00
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
ArrayMem & operator= (const T & val)
|
|
|
|
{
|
|
|
|
Array<T>::operator= (val);
|
|
|
|
return *this;
|
|
|
|
}
|
2010-07-19 18:00:52 +06:00
|
|
|
|
|
|
|
/// array copy
|
|
|
|
ArrayMem & operator= (const FlatArray<T> & a2)
|
|
|
|
{
|
2012-05-24 17:12:37 +06:00
|
|
|
this->SetSize (a2.Size());
|
2011-01-11 01:18:01 +05:00
|
|
|
for (int i = 0; i < size; i++)
|
2010-07-19 18:00:52 +06:00
|
|
|
(*this)[i] = a2[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
};
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
/*
|
|
|
|
template <class T, int B1, int B2>
|
|
|
|
class IndirectArray
|
|
|
|
{
|
|
|
|
const FlatArray<T, B1> & array;
|
|
|
|
const FlatArray<int, B2> & ia;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia)
|
2009-01-13 04:40:13 +05:00
|
|
|
: array(aa), ia(aia) { ; }
|
2009-10-28 05:04:19 +05:00
|
|
|
int Size() const { return ia.Size(); }
|
|
|
|
const T & operator[] (int i) const { return array[ia[i]]; }
|
|
|
|
};
|
|
|
|
*/
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
template <class T, int BASE = 0>
|
|
|
|
class MoveableArray
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int allocsize;
|
|
|
|
DynamicMem<T> data;
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
public:
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
MoveableArray()
|
|
|
|
{
|
|
|
|
size = allocsize = 0;
|
|
|
|
data.SetName ("MoveableArray");
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
MoveableArray(int asize)
|
|
|
|
: size(asize), allocsize(asize), data(asize)
|
|
|
|
{ ; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
~MoveableArray () { ; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
int Size() const { return size; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
void SetSize(int nsize)
|
|
|
|
{
|
|
|
|
if (nsize > allocsize)
|
|
|
|
{
|
|
|
|
data.ReAlloc (nsize);
|
|
|
|
allocsize = nsize;
|
|
|
|
}
|
|
|
|
size = nsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAllocSize (int nallocsize)
|
|
|
|
{
|
|
|
|
data.ReAlloc (nallocsize);
|
|
|
|
allocsize = nallocsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
T & operator[] (int i)
|
|
|
|
{ return ((T*)data)[i-BASE]; }
|
|
|
|
|
|
|
|
///
|
|
|
|
const T & operator[] (int i) const
|
|
|
|
{ return ((const T*)data)[i-BASE]; }
|
|
|
|
|
|
|
|
///
|
|
|
|
T & Elem (int i)
|
|
|
|
{ return ((T*)data)[i-1]; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
const T & Get (int i) const
|
|
|
|
{ return ((const T*)data)[i-1]; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
void Set (int i, const T & el)
|
|
|
|
{ ((T*)data)[i-1] = el; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
T & Last ()
|
|
|
|
{ return ((T*)data)[size-1]; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
const T & Last () const
|
|
|
|
{ return ((const T*)data)[size-1]; }
|
2009-01-13 04:40:13 +05:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
///
|
|
|
|
int Append (const T & el)
|
|
|
|
{
|
|
|
|
if (size == allocsize)
|
|
|
|
{
|
|
|
|
SetAllocSize (2*allocsize+1);
|
|
|
|
}
|
|
|
|
((T*)data)[size] = el;
|
|
|
|
size++;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
void Delete (int i)
|
|
|
|
{
|
|
|
|
DeleteElement (i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
void DeleteElement (int i)
|
|
|
|
{
|
|
|
|
((T*)data)[i-1] = ((T*)data)[size-1];
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
void DeleteLast ()
|
|
|
|
{ size--; }
|
|
|
|
|
|
|
|
///
|
|
|
|
void DeleteAll ()
|
|
|
|
{
|
|
|
|
size = allocsize = 0;
|
|
|
|
data.Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
void PrintMemInfo (ostream & ost) const
|
|
|
|
{
|
|
|
|
ost << Size() << " elements of size " << sizeof(T) << " = "
|
|
|
|
<< Size() * sizeof(T) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
MoveableArray & operator= (const T & el)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
((T*)data)[i] = el;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MoveableArray & Copy (const MoveableArray & a2)
|
|
|
|
{
|
|
|
|
SetSize (a2.Size());
|
|
|
|
for (int i = 0; i < this->size; i++)
|
|
|
|
data[i] = a2.data[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// array copy
|
|
|
|
MoveableArray & operator= (const MoveableArray & a2)
|
|
|
|
{
|
|
|
|
return Copy(a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SetName (const char * aname)
|
|
|
|
{
|
|
|
|
data.SetName(aname);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
///
|
|
|
|
//MoveableArray & operator= (MoveableArray &); //???
|
|
|
|
///
|
|
|
|
//MoveableArray (const MoveableArray &); //???
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline ostream & operator<< (ostream & ost, MoveableArray<T> & a)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < a.Size(); i++)
|
|
|
|
ost << i << ": " << a[i] << endl;
|
|
|
|
return ost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// bubble sort array
|
|
|
|
template <class T>
|
|
|
|
inline void BubbleSort (const FlatArray<T> & data)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Size(); i++)
|
|
|
|
for (int j = i+1; j < data.Size(); j++)
|
|
|
|
if (data[i] > data[j])
|
|
|
|
{
|
|
|
|
T hv = data[i];
|
|
|
|
data[i] = data[j];
|
|
|
|
data[j] = hv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// bubble sort array
|
|
|
|
template <class T, class S>
|
|
|
|
inline void BubbleSort (FlatArray<T> & data, FlatArray<S> & slave)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < data.Size(); i++)
|
|
|
|
for (int j = i+1; j < data.Size(); j++)
|
|
|
|
if (data[i] > data[j])
|
|
|
|
{
|
|
|
|
T hv = data[i];
|
|
|
|
data[i] = data[j];
|
|
|
|
data[j] = hv;
|
2010-07-19 18:00:52 +06:00
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
S hvs = slave[i];
|
|
|
|
slave[i] = slave[j];
|
|
|
|
slave[j] = hvs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T, class S>
|
2009-10-28 05:22:10 +05:00
|
|
|
void QuickSortRec (FlatArray<T> & data,
|
|
|
|
FlatArray<S> & slave,
|
|
|
|
int left, int right)
|
2009-10-28 05:04:19 +05:00
|
|
|
{
|
|
|
|
int i = left;
|
|
|
|
int j = right;
|
|
|
|
T midval = data[(left+right)/2];
|
|
|
|
|
|
|
|
do
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
2009-10-28 05:04:19 +05:00
|
|
|
while (data[i] < midval) i++;
|
|
|
|
while (midval < data[j]) j--;
|
|
|
|
|
|
|
|
if (i <= j)
|
|
|
|
{
|
|
|
|
Swap (data[i], data[j]);
|
|
|
|
Swap (slave[i], slave[j]);
|
|
|
|
i++; j--;
|
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
2009-10-28 05:04:19 +05:00
|
|
|
while (i <= j);
|
2009-10-28 05:22:10 +05:00
|
|
|
if (left < j) QuickSortRec (data, slave, left, j);
|
|
|
|
if (i < right) QuickSortRec (data, slave, i, right);
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
template <class T, class S>
|
2009-10-28 05:22:10 +05:00
|
|
|
void QuickSort (FlatArray<T> & data, FlatArray<S> & slave)
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
2009-10-28 05:22:10 +05:00
|
|
|
QuickSortRec (data, slave, 0, data.Size()-1);
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-28 05:04:19 +05:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2,
|
|
|
|
Array<T> & out)
|
2009-01-13 04:40:13 +05:00
|
|
|
{
|
2009-10-28 05:04:19 +05:00
|
|
|
out.SetSize(0);
|
|
|
|
for(int i=0; i<in1.Size(); i++)
|
|
|
|
if(in2.Contains(in1[i]))
|
|
|
|
out.Append(in1[i]);
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2, const FlatArray<T> & in3,
|
|
|
|
Array<T> & out)
|
|
|
|
{
|
|
|
|
out.SetSize(0);
|
|
|
|
for(int i=0; i<in1.Size(); i++)
|
|
|
|
if(in2.Contains(in1[i]) && in3.Contains(in1[i]))
|
|
|
|
out.Append(in1[i]);
|
2009-01-13 04:40:13 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 14:36:36 +06:00
|
|
|
}
|
2009-01-13 04:40:13 +05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|