#ifndef ROOT_TTableSorter
#define ROOT_TTableSorter
#include "TNamed.h"
#include "TTableDescriptor.h"
class table_head_st;
typedef Int_t (*COMPAREMETHOD)(const void **, const void **);
typedef Int_t (*SEARCHMETHOD) (const void *, const void **);
class TTableSorter : public TNamed {
private:
union { Char_t fChar;
Int_t fInt;
Long_t fLong;
Float_t fFloat;
Double_t fDouble;
} fValue;
protected:
void **fSortIndex;
Int_t fLastFound;
Int_t fFirstRow;
Int_t fNumberOfRows;
TString fColName;
Int_t fColOffset;
Int_t fColSize;
Int_t *fIndexArray;
Int_t fColDimensions;
const Char_t *fsimpleArray;
const TTable *fParentTable;
SEARCHMETHOD fSearchMethod;
COMPAREMETHOD fCompareMethod;
TTable::EColumnType fColType;
Long_t fParentRowSize;
const char *fFirstParentRow;
static int CompareFloat_t (const void **, const void **);
static int CompareInt_t (const void **, const void **);
static int CompareLong_t (const void **, const void **);
static int CompareULong_t (const void **, const void **);
static int CompareUInt_t (const void **, const void **);
static int CompareShort_t (const void **, const void **);
static int CompareDouble_t (const void **, const void **);
static int CompareUShort_t (const void **, const void **);
static int CompareUChar_t (const void **, const void **);
static int CompareChar_t (const void **, const void **);
static int CompareBool_t (const void **, const void **);
Int_t BSearch(const void *value) const;
Int_t BSearch(Float_t value ) const;
Int_t BSearch(Int_t value ) const;
Int_t BSearch(ULong_t value ) const;
Int_t BSearch(Long_t value ) const;
Int_t BSearch(UInt_t value ) const;
Int_t BSearch(Short_t value ) const;
Int_t BSearch(Double_t value ) const;
Int_t BSearch(UShort_t value ) const;
Int_t BSearch(UChar_t value ) const;
Int_t BSearch(Char_t value ) const;
Int_t BSearch(Bool_t value ) const;
Bool_t FillIndexArray();
Long_t GetRowSize();
void QSort();
void LearnTable();
static int SearchFloat_t (const void *, const void **);
static int SearchInt_t (const void *, const void **);
static int SearchULong_t (const void *, const void **);
static int SearchLong_t (const void *, const void **);
static int SearchUInt_t (const void *, const void **);
static int SearchShort_t (const void *, const void **);
static int SearchDouble_t (const void *, const void **);
static int SearchUShort_t (const void *, const void **);
static int SearchUChar_t (const void *, const void **);
static int SearchChar_t (const void *, const void **);
static int SearchBool_t (const void *, const void **);
Int_t SelectSearch(Float_t value ) const;
Int_t SelectSearch(Int_t value ) const;
Int_t SelectSearch(ULong_t value ) const;
Int_t SelectSearch(Long_t value ) const;
Int_t SelectSearch(UInt_t value ) const;
Int_t SelectSearch(Short_t value ) const;
Int_t SelectSearch(Double_t value ) const;
Int_t SelectSearch(UShort_t value ) const;
Int_t SelectSearch(UChar_t value ) const;
Int_t SelectSearch(Char_t value ) const;
Int_t SelectSearch(Bool_t value ) const;
void SetSearchMethod();
void SetSimpleArray(Int_t arraySize, Int_t firstRow,Int_t numberRows);
void BuildSorter(TString &colName, Int_t firstRow, Int_t numberRows);
const char *At(Int_t i) const;
public:
TTableSorter();
TTableSorter(const TTable &table, TString &colName, Int_t firstRow=0,Int_t numbeRows=0);
TTableSorter(const TTable *table, TString &colName, Int_t firstRow=0,Int_t numbeRows=0);
TTableSorter(const TTable &table, SEARCHMETHOD search, COMPAREMETHOD compare, Int_t firstRow=0,Int_t numbeRows=0);
TTableSorter(const TTable *table, SEARCHMETHOD search, COMPAREMETHOD compare, Int_t firstRow=0,Int_t numbeRows=0);
TTableSorter(const Float_t *simpleArray, Int_t arraySize, Int_t firstRow=0,Int_t numberRows=0);
TTableSorter(const Double_t *simpleArray, Int_t arraySize, Int_t firstRow=0,Int_t numberRows=0);
TTableSorter(const Long_t *simpleArray, Int_t arraySize, Int_t firstRow=0,Int_t numberRows=0);
virtual ~TTableSorter();
virtual Int_t CountKey(const void *key, Int_t firstIndx=0,Bool_t bSearch=kTRUE,Int_t *firstRow=0) const;
virtual Int_t CountKeys() const;
virtual Int_t FindFirstKey(const void *key) const;
Int_t BinarySearch(Float_t value ) const;
Int_t BinarySearch(Int_t value ) const;
Int_t BinarySearch(ULong_t value ) const;
Int_t BinarySearch(Long_t value ) const;
Int_t BinarySearch(UInt_t value ) const;
Int_t BinarySearch(Short_t value ) const;
Int_t BinarySearch(Double_t value ) const;
Int_t BinarySearch(UShort_t value ) const;
Int_t BinarySearch(UChar_t value ) const;
Int_t BinarySearch(Char_t value ) const;
Int_t BinarySearch(Bool_t value ) const;
virtual const char *GetColumnName() const { return fColName.Data();}
Int_t GetIndex(UInt_t sortedIndex) const;
virtual const void *GetKeyAddress(Int_t indx) { return (fSortIndex && indx >= 0) ?fSortIndex[indx]:(void *)(-1);}
virtual Int_t GetLastFound() const { return fLastFound; }
virtual const char *GetTableName() const;
virtual const char *GetTableTitle() const;
virtual const char *GetTableType() const;
virtual TTable *GetTable() const;
virtual Int_t GetNRows() const { return fNumberOfRows;}
virtual Int_t GetFirstRow() const { return fFirstRow;}
Int_t operator[](Int_t value) const;
Int_t operator[](Long_t value) const;
Int_t operator[](Double_t value) const;
Int_t operator[](void *value) const;
Int_t operator()(Float_t value);
Int_t operator()(Int_t value);
Int_t operator()(Long_t value);
Int_t operator()(Double_t value);
ClassDef(TTableSorter,0)
};
inline const char *TTableSorter::At(Int_t i) const {return fFirstParentRow + i*fParentRowSize;}
inline Long_t TTableSorter::GetRowSize() { return fParentRowSize; }
inline Int_t TTableSorter::operator[](Int_t value) const { return BSearch(value); }
inline Int_t TTableSorter::operator[](Long_t value) const { return BSearch(value); }
inline Int_t TTableSorter::operator[](Double_t value) const { return BSearch(value); }
inline Int_t TTableSorter::operator[](void *value) const { return BSearch(value); }
inline Int_t TTableSorter::operator()(Float_t value) { return BinarySearch(value); }
inline Int_t TTableSorter::operator()(Int_t value) { return BinarySearch(value); }
inline Int_t TTableSorter::operator()(Long_t value) { return BinarySearch(value); }
inline Int_t TTableSorter::operator()(Double_t value) { return BinarySearch(value); }
#endif