UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnyAr.H
1 // AnyAr.H $Revision: 1.12 $ $Date: 2002/10/16 17:37:47 $
2 
3 // May be included many times
4 
5 #include <stdio.h>
6 #include <mix/SortedAr.H>
7 
8 
9 /*************************************************************
10  * Dynamic Sorted Array of AnyType object.
11  *
12  * This is macro-based template with two mandatory macro
13  * parameters and an optional macro method.
14  *
15  * Format:
16  * // Existant your-type for example: dirent
17  * // Example:
18  * // struct Node {
19  * // Node *list;
20  * // int key;
21  * // char label[10];
22  * // };
23  * // #define AnyType Node
24  * #define AnyType your-type
25  *
26  * // Created your-type-array for example: Dirent
27  * // #define AnyAr NodeAr
28  * #define AnyAr your-type-array
29  *
30  * // Optional macros:
31  *
32  * // Macro method of comparation items x and y of AnyType.
33  * // Must return GREATER if x > y; EQUAL if x == y and LESS
34  * // if x > y.
35  * // #define AnyCmp(x,y) COMPAR0(x.key - y.key)
36  * #define AnyCmp(x,y) your-expression
37  *
38  * // If you define the next two macros AnyAr::print_item(int i)
39  * // will be generated with needed printing specification.
40  * // #define AnyFormatSpec "\n%d: list=0x%x, key=%d, label=%s"
41  * // #define AnyPrintList i, item.list, item.key, item.label
42  * #define AnyFormatSpec format-string-for-printf
43  * #define AnyPrintList list-of-variables-for-printf
44  *
45  * // If you don't want to use fprintf(stderr, FormatSpec, PrintSpec),
46  * // you can define any other messaging procedure:
47  * // #define AnyPrintf printf(
48  * #define AnyPrintf your-printf-like-proc
49  *
50  * // If you define AnyPrintItem, AnyAr::print_item(int i) will
51  * // be generated with pointed expression.
52  * // #define AnyPrintItem item->print_contents()
53  * #define AnyPrintItem expression
54  *
55  * // quant and volume are measured by sizeof(AnyType)
56  * #define AnyArQuant your-quant-of-growing
57  * #define AnyArVolume your-startup-volume
58  *
59  * // Must be included after defining previous macros.
60  * #include <mix/AnyAr.H> // generates class your-type-array
61  * // and defines all methods of it as inline
62  *
63  * Usage:
64  * 1) For a type you want to make array you must define
65  * two mandatory macros: AnyType and AnyAr as in example.
66  * 2) You may also define AnyCmp(x,y) macro, so AnyAr::sort()
67  * method will order the array as you wish. If you don't
68  * need sorting feature, AnyCmp will be created as dummy
69  * expression that always returns EQUAL.
70  * 3) If you did not define AnyFormatSpec or AnyPrintList
71  * AnyAr::print_item(int i) will not be generated and will be
72  * used DynAr::print_item(int i)
73  * 4) If you defined AnyPrintItem, AnyFormatSpec and
74  * AnyPrintList are not used at all.
75  * 5) Include after definitions this header file.
76  *
77  * You can define this way in one source many array types by
78  * simple replication of usage rules. Do not worry about
79  * previous macro definitions: they are undefined in AnyAr.H
80  *************************************************************/
81 
82 #ifndef AnyCmp
83 /* Dummy comparation */
84 #define AnyCmp(x,y) (EQUAL)
85 #endif /* AnyCmp */
86 
87 #ifndef AnyType
88 #error "AnyType must be defined as type of array's item"
89 #endif /* AnyType */
90 
91 #ifndef AnyAr
92 #error "AnyAr must be defined as name of generated class of array"
93 #endif /* AnyAr */
94 
95 #ifndef AnyArQuant
96 #define AnyArQuant DEFAULT_QUANT
97 #endif /* AnyArQuant */
98 
99 #ifndef AnyArVolume
100 #define AnyArVolume START_VOLUME
101 #endif /* AnyArVolume */
102 
103 #ifndef AnyPrintf
104 #define AnyPrintf fprintf(stderr,
105 #endif /* AnyPrintf */
106 
107 class AnyAr : public SortedAr
108 {
109 public:
110 
111  AnyAr (const AnyAr& pArray)
112  :SortedAr(pArray) {};
113  AnyAr (unsigned quant = AnyArQuant,
114  unsigned volume = AnyArVolume)
115  :SortedAr(sizeof(AnyType), quant, volume) {};
116 
117  AnyType& fetch (unsigned i)
118  {return *(AnyType*)item_ptr(i);};
119  AnyType get (unsigned i) const
120  {return *(AnyType*)item_ptr(i);};
121 
122  void insert (unsigned i, const AnyType& val)
123  {SortedAr::insert(i, (const char*)&val);};
124 
125  void addh (const AnyType& val) {SortedAr::addh((const char*)&val);};
126  void addl (const AnyType& val) {SortedAr::addl((const char*)&val);};
127 
128  void addh (const AnyAr& pArray) {SortedAr::addh(pArray);};
129  void addl (const AnyAr& pArray) {SortedAr::addl(pArray);};
130 
131  AnyAr& assign (const AnyAr& pArray)
132  {SortedAr::assign(pArray); return *this;};
133 
134  // Synonyms
135  AnyType& operator[] (unsigned i)
136  {return fetch(i);};
137  AnyType operator() (unsigned i) const
138  {return get(i);};
139  AnyAr& operator= (const AnyAr& pArray)
140  {return assign(pArray);};
141 
142  // Compare between two elements
143  virtual Compar compare (const char* item1, const char* item2) const
144  {return AnyCmp((*(AnyType*)item1), (*(AnyType*)item2));};
145 
146  // Return index of item or -1
147  int find_ascent (AnyType& item) const
148  {return quick_find_ascent((const char*)&item);}
149  int find_descent (AnyType& item) const
150  {return quick_find_descent((const char*)&item);}
151  int find (AnyType& item) const
152  {return slow_find((const char*)&item);}
153 
154  // Return LESS or GREATER in case of out of range and EQUAL means
155  // the L..R is found (L<=R)
156  Compar find_range_ascent (AnyType& value,
157  unsigned& L, unsigned& R) const {
158  return SortedAr::find_range_ascent((const char*)&value, L, R);
159  }
160  Compar find_range_descent (AnyType& value,
161  unsigned& L, unsigned& R) const {
162  return SortedAr::find_range_descent((const char*)&value, L, R);
163  }
164 
165  // Smart print
166 #ifdef AnyPrintItem
167  virtual void print_item (unsigned i) const
168  {AnyType&item=*(AnyType*)item_ptr(i);AnyPrintItem;}
169 #undef AnyPrintItem
170 #elif defined(AnyFormatSpec) && defined(AnyPrintList)
171  virtual void print_item (unsigned i) const
172  {AnyType&item=*(AnyType*)item_ptr(i);
173  AnyPrintf AnyFormatSpec, AnyPrintList);}
174 #undef AnyFormatSpec
175 #undef AnyPrintList
176 #endif
177 
178 };
179 
180 #undef AnyType
181 #undef AnyAr
182 #undef AnyCmp
183 #undef AnyArQuant
184 #undef AnyArVolume
185 #undef AnyPrintf
Definition: SortedAr.H:14
Definition: AnyAr.H:107