00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef NAMELIST_TEMPLATE_H
00026 #define NAMELIST_TEMPLATE_H
00027
00028 #include <string.h>
00029 #include "ResizeArray.h"
00030 #include "utilities.h"
00031 #include "hash.h"
00032
00033 #define NLISTSIZE 64
00034
00039 template<class T>
00040 class NameList {
00041 protected:
00042 int Num;
00043 ResizeArray<char *> names;
00044 ResizeArray<T> Data;
00045 hash_t hash;
00046
00047 public:
00049
00050 NameList(void) : names(NLISTSIZE), Data(NLISTSIZE) {
00051 Num = 0;
00052 hash_init(&hash, 127);
00053 }
00054
00056 virtual ~NameList(void) {
00057 for(int i=0; i < Num; i++) {
00058 if(names[i])
00059 delete [] names[i];
00060 }
00061 hash_destroy(&hash);
00062 }
00063
00064 int num(void) const { return Num; }
00065
00066
00067
00068 int add_name(const char *nm, const T &val) {
00069 char tmpnm[128];
00070
00071 if (!nm)
00072 return (-1);
00073
00074
00075 char *s = tmpnm;
00076 while (*nm && *nm == ' ')
00077 nm++;
00078
00079 int len = 127;
00080 while (*nm && len--)
00081 *(s++) = *(nm++);
00082
00083 *s = 0;
00084 while(s != tmpnm && *(--s) == ' ')
00085 *s = '\0';
00086
00087 int myindex;
00088 if ((myindex = hash_lookup(&hash, tmpnm)) != HASH_FAIL) {
00089 return myindex;
00090 }
00091
00092
00093 names.append(stringdup(tmpnm));
00094
00095 myindex = hash_insert(&hash, names[Num], Num);
00096
00097 Data.append(val);
00098 return Num++;
00099 }
00100
00101
00102
00103
00104 const char * name(int a) const {
00105 if (a >= 0 && a < Num) {
00106 return names[a];
00107 }
00108 return NULL;
00109 }
00110
00111
00112
00113
00114
00115
00116 int typecode(const char *nm) const {
00117 if (!nm)
00118 return -1;
00119
00120 return hash_lookup(&hash, nm);
00121 }
00122
00123
00124
00125
00126
00127 T data(const char *nm) const {
00128 if (!nm)
00129 return Data[0];
00130
00131 int myindex = hash_lookup(&hash, nm);
00132 if (myindex != HASH_FAIL)
00133 return Data[myindex];
00134
00135 return Data[0];
00136 }
00137
00138
00139
00140 T data(int a) const {
00141 if (a >= 0 && a < Num) {
00142 return Data[a];
00143 }
00144 return Data[0];
00145 }
00146
00147
00148
00149 void set_data(int a, const T &val) {
00150 if(a >= 0 && a < Num) {
00151 Data[a] = val;
00152 }
00153
00154 }
00155
00156
00157 void set_name(int a, const char *nm) {
00158 if (a < 0 || a >= Num)
00159 return;
00160
00161
00162 hash_delete(&hash, names[a]);
00163 delete [] names[a];
00164 names[a] = stringdup(nm);
00165
00166 hash_insert(&hash, names[a], a);
00167 }
00168
00169 };
00170
00171
00172
00173 typedef NameList<int> *NameListIntPtr;
00174 typedef NameList<float> *NameListFloatPtr;
00175 typedef NameList<char> *NameListCharPtr;
00176 typedef NameList<char *> *NameListStringPtr;
00177
00178 #endif
00179