00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef HxIndxArray_H
00015 #define HxIndxArray_H
00016
00017 #include <stdio.h>
00018 #include <list>
00019 #include "HxMatrix.h"
00020
00021
00022 template<class ArrayT, class HeaderT>
00023 class indexedArray {
00024 public:
00025 indexedArray();
00026 ~indexedArray();
00027
00028 void open(HxString indexFile, int norm = 0);
00029 void close();
00030 void append(HxString indexFile);
00031 void create(HxString indexFile);
00032 void setHeader(HeaderT& header)
00033 {_header = header;}
00034
00035
00036 HeaderT header() const {return _header;}
00037
00038 int size() const {return _key.size();}
00039
00040 virtual int arraySize(const HeaderT& header) const = 0;
00041 virtual double compare(ArrayT *e1, ArrayT *e2,
00042 const HeaderT& header) const = 0;
00043
00044 void add(HxString& key, ArrayT *data);
00045
00046 ArrayT* getData(HxString key) const;
00047
00048 void random(int n);
00049 void search(ArrayT *target, int n, int refine = 0);
00050 void retrieve(HxString key, int n, int refine = 0);
00051 HxString nearest(int n) const;
00052 double score(int n) const;
00053 ArrayT* nearestData(int n) const;
00054
00055 HxMatrix similarity() const;
00056 STD_OSTREAM& put(STD_OSTREAM& os) const;
00057
00058 void import(STD_ISTREAM& is);
00059
00060
00061 ArrayT* at(int ndx) const;
00062
00063
00064 HxString getKey(int ndx) const;
00065
00066 friend STD_OSTREAM& operator<<(STD_OSTREAM& os,
00067 const indexedArray<ArrayT, HeaderT>& l)
00068 {return l.put(os);}
00069
00070
00071 private:
00072 HeaderT _header;
00073 HxString _fileName;
00074
00075 FILE *_fp;
00076 char *_mem;
00077
00078 int _arraySize;
00079
00080 std::list<ArrayT*> _arrays;
00081 std::list<char*> _key;
00082
00083 double *_nearval;
00084 char **_nearkey;
00085 ArrayT **_neardata;
00086
00087 int _normalize;
00088 ArrayT *_minVal;
00089 ArrayT *_normVal;
00090 };
00091
00092 class indexedFloatArray : public indexedArray<float, int> {
00093 int arraySize(const int& header) const
00094 {return header;}
00095 double compare(float *e1, float *e2, const int& size) const
00096 {
00097 double norm2 = 0.0;
00098 double v;
00099
00100 for (int i = size; i > 0; i--) {
00101 v = *e1++ - *e2++;
00102 norm2 += v*v;
00103 }
00104
00105 return sqrt(norm2);
00106 }
00107 };
00108
00109 #ifdef INC_TEMPLATE_SRC
00110 #include "HxIndxArray.c"
00111 #endif
00112
00113 #endif