8 #include <mix/SortedAr.H>
56 #error "AssocAr must be defined as name of generated class of array"
60 #error "AssocItemType must be defined as type of item"
64 #error "AssocKeysCmp(key1,key2) must be defined in strcmp() manner"
68 #error "AssocKeyField must be field in AssocItemType structure"
72 #error "AssocKeyType must be type of AssocKeyField"
76 #define AssocArQuant DEFAULT_QUANT
80 #define AssocArVolume START_VOLUME
88 AssocItemType& ref (
unsigned i)
const
89 {
return *(AssocItemType*)item_ptr(i);};
95 AssocAr (
unsigned quant = AssocArQuant,
96 unsigned volume = AssocArVolume)
97 :
SortedAr(
sizeof(AssocItemType), quant, volume) {};
100 unsigned key_to_index (AssocKeyType key)
const
103 #ifdef AssocKeyAssign
106 item.AssocKeyField = key;
108 register int index = quick_find_ascent((
const char*)&item);
109 if(-1 == index) Throw(ERROR__DA_UNKNOWN_KEY);
112 AssocKeyType index_to_key (
unsigned index)
const
113 {
return ref(index).AssocKeyField;}
117 compare (
const char* ptr1,
const char* ptr2)
const
119 AssocKeyType key1 = (*(AssocItemType*)ptr1).AssocKeyField;
120 AssocKeyType key2 = (*(AssocItemType*)ptr2).AssocKeyField;
121 return (Compar)AssocKeysCmp(key1, key2);
125 AssocItemType& assoc_fetch (AssocKeyType key)
126 {
return ref(key_to_index(key));};
127 AssocItemType assoc_get (AssocKeyType key)
const
128 {
return ref(key_to_index(key));};
131 AssocItemType& operator[] (AssocKeyType key)
132 {
return assoc_fetch(key);};
133 AssocItemType operator() (AssocKeyType key)
const
134 {
return assoc_get(key);};
137 void add (
const AssocItemType& val)
139 if(known_key(val.AssocKeyField)){
140 Throw(ERROR__DA_KNOWN_KEY);
142 SortedAr::addh((
const char*)&val);
147 void put (
const AssocItemType& val)
149 AssocKeyType key=val.AssocKeyField;
151 memcpy(item_ptr(key_to_index(key)), &val,
sizeof(AssocItemType));
153 SortedAr::addh((
const char*)&val);
160 int add (
int val_n,
const AssocItemType* vals)
165 int val_i, added_n = 0;
166 for(val_i = 0; val_i < val_n; ++val_i)
167 if(!known_key(vals[val_i].AssocKeyField)){
168 SortedAr::addh((
const char*)(vals + val_i));
176 void put (
int val_n,
const AssocItemType* vals)
182 for(val_i = 0; val_i < val_n; ++val_i){
183 AssocKeyType key = vals[val_i].AssocKeyField;
185 memcpy(item_ptr(key_to_index(key)), vals + val_i,
186 sizeof(AssocItemType));
188 SortedAr::addh((
const char*)(vals + val_i));
194 Logic known_key (AssocKeyType key)
const
197 #ifdef AssocKeyAssign
200 item.AssocKeyField = key;
202 register int index = quick_find_ascent((
const char*)&item);
203 return (-1 != index);
207 AssocItemType& fetch (
unsigned i)
209 AssocItemType
get (
unsigned i)
const
213 #ifdef AssocPrintItem
214 virtual void print_item (
unsigned i)
const
215 {AssocItemType&item=ref(i);AssocPrintItem;}
216 #undef AssocPrintItem
217 #elif defined(AssocFormatSpec) && defined(AssocPrintList)
218 virtual void print_item (
unsigned i)
const
219 {AssocItemType&item=ref(i);
220 fprintf(stderr, AssocFormatSpec, AssocPrintList);}
221 #undef AssocFormatSpec
222 #undef AssocPrintList
232 #undef AssocKeyAssign
Definition: SortedAr.H:14