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