//-*-c++-*- #ifndef ARRAY1_H #define ARRAY1_H /** * @file array1.h * @brief Basic dynamic 1-dimensional array of type T - COLUMNWISE ORDERING * @author James R. Diebel, Stanford University * * - History: * - 21 March 2006 - Started */ #include #include /** * @namespace sla * @brief Simple Linear Algebra - library of matrix and vector classes * plus some random utility functions and other containers */ namespace sla { ///////////////////////////////////////////////////////////////////////////// // DECLARATIONS ///////////////////////////////////////////////////////////////////////////// // Forward declarations template class Array; // K-dimensional array template class Array1; // 1-dimensional array //////////////////// Array1 //////////////////// /** * A 1-Dimensional Array */ template class Array1: public Array { public: // Constructors/Destructor Array1(); Array1(const int len_); //Array1(const Array1& a) { set(a); } // Mutators void setSize(const int len_); void sort(const bool ascending = true); void sort(int(*comp)(const void *, const void *), const bool ascending = true); T kthElement(int k); T median(); // Accessors int size(); // inherit member data and functions of parent using Array::x; using Array::reset; protected: // inherit member data and functions of parent using Array::dim; }; //////////////////////////////////////////////////////////////////////////// // DEFINITIONS //////////////////////////////////////////////////////////////////////////// //////////////////// Array1 //////////////////// // Constructors/Destructor /** Ctor that does no initalization. */ template inline Array1::Array1() : Array() {} /** Ctor that starts with given dimensions * @param len_ is the size */ template inline Array1::Array1(const int len_) : Array() { setSize(len_); } /** Set the size of the array * @param n_ the sizes of the array in each dimension */ template inline void Array1::setSize(const int len_) { Array::setSize(Vec(len_)); } /** Sort the array using default comparison function */ template inline void Array1::sort(const bool ascending) { if (ascending) qsort(x,dim(0),sizeof(T),compareAscending); else qsort(x,dim(0),sizeof(T),compareDescending); } /** Sort the array using provided comparison function */ template inline void Array1::sort(int(*comp)(const void *, const void *), const bool ascending) { qsort(x,dim(0),sizeof(T),comp); } /** Find the kth smallest element of the array, zero-based numbering */ template inline T Array1::kthElement(int k) { int i,j,l,m; T y,t; l=0; m=dim(0)-1; while (l inline T Array1::median() { return kthElement((dim(0)&1)?(dim(0)/2):((dim(0)/2)-1)); } // Accessors /** Set the size of the array * @param n_ the sizes of the array in each dimension */ template inline int Array1::size() { return dim(0); } } // end namespace sla #endif