mirror of
https://github.com/NGSolve/netgen.git
synced 2025-04-13 00:27:28 +05:00
quicksort for arrays
This commit is contained in:
parent
e2f16032cc
commit
94a06d8cae
@ -11,11 +11,11 @@
|
|||||||
namespace netgen
|
namespace netgen
|
||||||
{
|
{
|
||||||
|
|
||||||
// template <class T, int B1, int B2> class IndirectArray;
|
// template <class T, int B1, int B2> class IndirectArray;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A simple array container.
|
A simple array container.
|
||||||
Array represented by size and data-pointer.
|
Array represented by size and data-pointer.
|
||||||
No memory allocation and deallocation, must be provided by user.
|
No memory allocation and deallocation, must be provided by user.
|
||||||
@ -23,15 +23,15 @@ namespace netgen
|
|||||||
Optional range check by macro RANGE_CHECK
|
Optional range check by macro RANGE_CHECK
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template <class T, int BASE = 0>
|
template <class T, int BASE = 0>
|
||||||
class FlatArray
|
class FlatArray
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
/// the size
|
/// the size
|
||||||
int size;
|
int size;
|
||||||
/// the data
|
/// the data
|
||||||
T * data;
|
T * data;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// provide size and memory
|
/// provide size and memory
|
||||||
FlatArray (int asize, T * adata)
|
FlatArray (int asize, T * adata)
|
||||||
@ -47,10 +47,10 @@ public:
|
|||||||
/// access array.
|
/// access array.
|
||||||
T & operator[] (int i)
|
T & operator[] (int i)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (i-BASE < 0 || i-BASE >= size)
|
if (i-BASE < 0 || i-BASE >= size)
|
||||||
cout << "array<" << typeid(T).name() << "> out of range, i = " << i << ", s = " << size << endl;
|
cout << "array<" << typeid(T).name() << "> out of range, i = " << i << ", s = " << size << endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return data[i-BASE];
|
return data[i-BASE];
|
||||||
}
|
}
|
||||||
@ -153,39 +153,39 @@ public:
|
|||||||
{
|
{
|
||||||
return ( Pos(elem) >= 0 );
|
return ( Pos(elem) >= 0 );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// print array
|
// print array
|
||||||
template <class T, int BASE>
|
template <class T, int BASE>
|
||||||
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a)
|
inline ostream & operator<< (ostream & s, const FlatArray<T,BASE> & a)
|
||||||
{
|
{
|
||||||
for (int i = a.Begin(); i < a.End(); i++)
|
for (int i = a.Begin(); i < a.End(); i++)
|
||||||
s << i << ": " << a[i] << endl;
|
s << i << ": " << a[i] << endl;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Dynamic array container.
|
Dynamic array container.
|
||||||
|
|
||||||
Array<T> is an automatically increasing array container.
|
Array<T> is an automatically increasing array container.
|
||||||
The allocated memory doubles on overflow.
|
The allocated memory doubles on overflow.
|
||||||
Either the container takes care of memory allocation and deallocation,
|
Either the container takes care of memory allocation and deallocation,
|
||||||
or the user provides one block of data.
|
or the user provides one block of data.
|
||||||
*/
|
*/
|
||||||
template <class T, int BASE = 0>
|
template <class T, int BASE = 0>
|
||||||
class Array : public FlatArray<T, BASE>
|
class Array : public FlatArray<T, BASE>
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
/// physical size of array
|
/// physical size of array
|
||||||
int allocsize;
|
int allocsize;
|
||||||
/// memory is responsibility of container
|
/// memory is responsibility of container
|
||||||
bool ownmem;
|
bool ownmem;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Generate array of logical and physical size asize
|
/// Generate array of logical and physical size asize
|
||||||
explicit Array(int asize = 0)
|
explicit Array(int asize = 0)
|
||||||
@ -336,7 +336,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/// resize array, at least to size minsize. copy contents
|
/// resize array, at least to size minsize. copy contents
|
||||||
void ReSize (int minsize)
|
void ReSize (int minsize)
|
||||||
@ -364,17 +364,17 @@ private:
|
|||||||
|
|
||||||
allocsize = nsize;
|
allocsize = nsize;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <class T, int S>
|
template <class T, int S>
|
||||||
class ArrayMem : public Array<T>
|
class ArrayMem : public Array<T>
|
||||||
{
|
{
|
||||||
// T mem[S]; // Intel C++ calls dummy constructor
|
// T mem[S]; // Intel C++ calls dummy constructor
|
||||||
// char mem[S*sizeof(T)];
|
// char mem[S*sizeof(T)];
|
||||||
double mem[(S*sizeof(T)+7) / 8];
|
double mem[(S*sizeof(T)+7) / 8];
|
||||||
public:
|
public:
|
||||||
/// Generate array of logical and physical size asize
|
/// Generate array of logical and physical size asize
|
||||||
explicit ArrayMem(int asize = 0)
|
explicit ArrayMem(int asize = 0)
|
||||||
: Array<T> (S, static_cast<T*> (static_cast<void*>(&mem[0])))
|
: Array<T> (S, static_cast<T*> (static_cast<void*>(&mem[0])))
|
||||||
@ -393,26 +393,26 @@ public:
|
|||||||
Array<T>::operator= (val);
|
Array<T>::operator= (val);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
template <class T, int B1, int B2>
|
template <class T, int B1, int B2>
|
||||||
class IndirectArray
|
class IndirectArray
|
||||||
{
|
{
|
||||||
const FlatArray<T, B1> & array;
|
const FlatArray<T, B1> & array;
|
||||||
const FlatArray<int, B2> & ia;
|
const FlatArray<int, B2> & ia;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia)
|
IndirectArray (const FlatArray<T,B1> & aa, const FlatArray<int, B2> & aia)
|
||||||
: array(aa), ia(aia) { ; }
|
: array(aa), ia(aia) { ; }
|
||||||
int Size() const { return ia.Size(); }
|
int Size() const { return ia.Size(); }
|
||||||
const T & operator[] (int i) const { return array[ia[i]]; }
|
const T & operator[] (int i) const { return array[ia[i]]; }
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -422,15 +422,15 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
///
|
///
|
||||||
template <class T, int BASE = 0>
|
template <class T, int BASE = 0>
|
||||||
class MoveableArray
|
class MoveableArray
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
int allocsize;
|
int allocsize;
|
||||||
DynamicMem<T> data;
|
DynamicMem<T> data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MoveableArray()
|
MoveableArray()
|
||||||
{
|
{
|
||||||
@ -560,77 +560,115 @@ public:
|
|||||||
{
|
{
|
||||||
data.SetName(aname);
|
data.SetName(aname);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
///
|
///
|
||||||
//MoveableArray & operator= (MoveableArray &); //???
|
//MoveableArray & operator= (MoveableArray &); //???
|
||||||
///
|
///
|
||||||
//MoveableArray (const MoveableArray &); //???
|
//MoveableArray (const MoveableArray &); //???
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline ostream & operator<< (ostream & ost, MoveableArray<T> & a)
|
inline ostream & operator<< (ostream & ost, MoveableArray<T> & a)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < a.Size(); i++)
|
for (int i = 0; i < a.Size(); i++)
|
||||||
ost << i << ": " << a[i] << endl;
|
ost << i << ": " << a[i] << endl;
|
||||||
return ost;
|
return ost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// bubble sort array
|
/// bubble sort array
|
||||||
template <class T>
|
template <class T>
|
||||||
inline void BubbleSort (const FlatArray<T> & data)
|
inline void BubbleSort (const FlatArray<T> & data)
|
||||||
{
|
{
|
||||||
T hv;
|
|
||||||
for (int i = 0; i < data.Size(); i++)
|
for (int i = 0; i < data.Size(); i++)
|
||||||
for (int j = i+1; j < data.Size(); j++)
|
for (int j = i+1; j < data.Size(); j++)
|
||||||
if (data[i] > data[j])
|
if (data[i] > data[j])
|
||||||
{
|
{
|
||||||
hv = data[i];
|
T hv = data[i];
|
||||||
data[i] = data[j];
|
data[i] = data[j];
|
||||||
data[j] = hv;
|
data[j] = hv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// bubble sort array
|
|
||||||
template <class T, class S>
|
/// bubble sort array
|
||||||
inline void BubbleSort (FlatArray<T> & data, FlatArray<S> & slave)
|
template <class T, class S>
|
||||||
{
|
inline void BubbleSort (FlatArray<T> & data, FlatArray<S> & slave)
|
||||||
T hv;
|
{
|
||||||
S hvs;
|
|
||||||
for (int i = 0; i < data.Size(); i++)
|
for (int i = 0; i < data.Size(); i++)
|
||||||
for (int j = i+1; j < data.Size(); j++)
|
for (int j = i+1; j < data.Size(); j++)
|
||||||
if (data[i] > data[j])
|
if (data[i] > data[j])
|
||||||
{
|
{
|
||||||
hv = data[i];
|
T hv = data[i];
|
||||||
data[i] = data[j];
|
data[i] = data[j];
|
||||||
data[j] = hv;
|
data[j] = hv;
|
||||||
|
|
||||||
hvs = slave[i];
|
S hvs = slave[i];
|
||||||
slave[i] = slave[j];
|
slave[i] = slave[j];
|
||||||
slave[j] = hvs;
|
slave[j] = hvs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <class T>
|
template <class T, class S>
|
||||||
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2,
|
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)
|
Array<T> & out)
|
||||||
{
|
{
|
||||||
out.SetSize(0);
|
out.SetSize(0);
|
||||||
for(int i=0; i<in1.Size(); i++)
|
for(int i=0; i<in1.Size(); i++)
|
||||||
if(in2.Contains(in1[i]))
|
if(in2.Contains(in1[i]))
|
||||||
out.Append(in1[i]);
|
out.Append(in1[i]);
|
||||||
}
|
}
|
||||||
template <class T>
|
template <class T>
|
||||||
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2, const FlatArray<T> & in3,
|
void Intersection (const FlatArray<T> & in1, const FlatArray<T> & in2, const FlatArray<T> & in3,
|
||||||
Array<T> & out)
|
Array<T> & out)
|
||||||
{
|
{
|
||||||
out.SetSize(0);
|
out.SetSize(0);
|
||||||
for(int i=0; i<in1.Size(); i++)
|
for(int i=0; i<in1.Size(); i++)
|
||||||
if(in2.Contains(in1[i]) && in3.Contains(in1[i]))
|
if(in2.Contains(in1[i]) && in3.Contains(in1[i]))
|
||||||
out.Append(in1[i]);
|
out.Append(in1[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user