quicksort for arrays

This commit is contained in:
Joachim Schoeberl 2009-10-28 00:04:19 +00:00
parent e2f16032cc
commit 94a06d8cae

View File

@ -11,11 +11,11 @@
namespace netgen
{
// template <class T, int B1, int B2> class IndirectArray;
// template <class T, int B1, int B2> class IndirectArray;
/**
/**
A simple array container.
Array represented by size and data-pointer.
No memory allocation and deallocation, must be provided by user.
@ -23,15 +23,15 @@ namespace netgen
Optional range check by macro RANGE_CHECK
*/
template <class T, int BASE = 0>
class FlatArray
{
protected:
template <class T, int BASE = 0>
class FlatArray
{
protected:
/// the size
int size;
/// the data
T * data;
public:
public:
/// provide size and memory
FlatArray (int asize, T * adata)
@ -47,10 +47,10 @@ public:
/// access array.
T & operator[] (int i)
{
#ifdef DEBUG
#ifdef DEBUG
if (i-BASE < 0 || i-BASE >= size)
cout << "array<" << typeid(T).name() << "> out of range, i = " << i << ", s = " << size << endl;
#endif
#endif
return data[i-BASE];
}
@ -153,39 +153,39 @@ public:
{
return ( Pos(elem) >= 0 );
}
};
};
// print array
template <class T, int BASE>
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a)
{
// print array
template <class T, int BASE>
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a)
{
for (int i = a.Begin(); i < a.End(); i++)
s << i << ": " << a[i] << endl;
return s;
}
}
/**
/**
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:
*/
template <class T, int BASE = 0>
class Array : public FlatArray<T, BASE>
{
protected:
/// physical size of array
int allocsize;
/// memory is responsibility of container
bool ownmem;
public:
public:
/// Generate array of logical and physical size asize
explicit Array(int asize = 0)
@ -336,7 +336,7 @@ public:
}
private:
private:
/// resize array, at least to size minsize. copy contents
void ReSize (int minsize)
@ -364,17 +364,17 @@ private:
allocsize = nsize;
}
};
};
template <class T, int S>
class ArrayMem : public Array<T>
{
template <class T, int S>
class ArrayMem : public Array<T>
{
// T mem[S]; // Intel C++ calls dummy constructor
// char mem[S*sizeof(T)];
double mem[(S*sizeof(T)+7) / 8];
public:
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])))
@ -393,26 +393,26 @@ public:
Array<T>::operator= (val);
return *this;
}
};
};
/*
template <class T, int B1, int B2>
class IndirectArray
{
/*
template <class T, int B1, int B2>
class IndirectArray
{
const FlatArray<T, B1> & array;
const FlatArray<int, B2> & ia;
public:
public:
IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia)
: array(aa), ia(aia) { ; }
int Size() const { return ia.Size(); }
const T & operator[] (int i) const { return array[ia[i]]; }
};
*/
};
*/
@ -422,15 +422,15 @@ public:
///
template <class T, int BASE = 0>
class MoveableArray
{
///
template <class T, int BASE = 0>
class MoveableArray
{
int size;
int allocsize;
DynamicMem<T> data;
public:
public:
MoveableArray()
{
@ -560,77 +560,115 @@ public:
{
data.SetName(aname);
}
private:
private:
///
//MoveableArray & operator= (MoveableArray &); //???
///
//MoveableArray (const MoveableArray &); //???
};
};
template <class T>
inline ostream & operator<< (ostream & ost, MoveableArray<T> & a)
{
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)
{
T hv;
/// 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])
{
hv = data[i];
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)
{
T hv;
S hvs;
}
/// 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])
{
hv = data[i];
T hv = data[i];
data[i] = data[j];
data[j] = hv;
hvs = slave[i];
S hvs = slave[i];
slave[i] = slave[j];
slave[j] = hvs;
}
}
}
template <class T>
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2,
template <class T, class S>
void QickSortRec (FlatArray<T> & data,
FlatArray<T> & slave,
int left, int right)
{
int i = left;
int j = right;
T midval = data[(left+right)/2];
do
{
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--;
}
}
while (i <= j);
if (left < j) QickSortRec (data, slave, left, j);
if (i < right) QickSortRec (data, slave, i, right);
}
template <class T, class S>
void QickSort (FlatArray<T> & data, FlatArray<S> & slave)
{
QickSortRec (data, slave, 0, data.Size()-1);
}
template <class T>
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2,
Array<T> & out)
{
{
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,
}
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]);
}
}
}