UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DynAr.H
1 // DynAr.H $Revision: 2.20 $ $Date: 2003/11/24 13:31:17 $
2 
3 #if !defined __DynAr_H
4 
5 #define __DynAr_H
6 
7 #include <mix/Exceptions.h>
8 
9 #define DEFAULT_QUANT 10
10 #define START_VOLUME 10
11 
12 #ifndef _last_
13 /* A'la Perl :-) */
14 #define _last_ ((unsigned)(-1))
15 #endif /* _last_ */
16 
17 
18 /*************************************************************
19  * Dynamic Array of any elements
20  *************************************************************/
21 
22 class DynAr
23 {
24 protected:
25 
26  char *tar; // sequence of items
27  unsigned tSize; // size of unit
28  unsigned tCount, tVolume, tQuant; // in units
29 
30  void extend_to (unsigned new_volume);
31  void extend_by (int add_volume);
32  void copy_from (const DynAr& dynar, unsigned i1, unsigned cnt);
33  void shift_by (unsigned i1, int offset);
34 
35  virtual void destruct_item (char* /*item*/) {};
36 
37 public:
38 
39  DynAr (const DynAr& dynar);
40  DynAr (unsigned size, unsigned quant, unsigned volume);
41  virtual ~DynAr ();
42 
43  char* item_ptr (unsigned i) const;
44  char* start_ptr () const; // item_ptr(0) without i-check
45 
46  void clean ();
47  void clear_up () {clean();}
48 
49  void pack ();
50 
51  unsigned count () const;
52 
53  // Reserve memory for pointed number of items
54  void reserve (unsigned n);
55 
56  // Insert zero-filled item at i-th position
57  void insert (unsigned i);
58 
59  // Insert some new zero filled items to the array
60  void insertn (unsigned i, unsigned n);
61 
62  // Insert the whole array dynar to this one at i-th position
63  void insert (unsigned i, const DynAr& dynar);
64 
65  // Insert item at i-th position
66  void insert (unsigned i, const char* item);
67 
68  // Add zero-filled item to this array
69  void addh ();
70  void addl ();
71 
72  // Add item to this array
73  void addh (const char* item);
74  void addl (const char* item);
75 
76  // Add the whole array dynar to this
77  void addh (const DynAr& dynar);
78  void addl (const DynAr& dynar);
79 
80  // Add some new zero filled items to the array
81  void addhn (unsigned n);
82  void addln (unsigned n);
83 
84  // Delete i-th item from this
85  void remove (unsigned i);
86 
87  // Delete n-items from array starting from i-item.
88  void removen (unsigned i, unsigned n);
89 
90  DynAr& assign (const DynAr& dynar);
91 
92  // Per byte exchange
93  virtual void exchange (unsigned i1, unsigned i2);
94 
95  // Find item and return index of it or -1 if search failed.
96  virtual int slow_find (const char* item) const;
97 
98  // For debug purpose
99  virtual void print_item (unsigned i) const;
100  virtual void print_contents () const;
101 
102 };
103 
104 
105 /*************************************************************
106  *
107  * Inlined methods:
108  *
109  *************************************************************/
110 
111 /*************************************************************
112  * Add garbage-filled element to the end of array
113  *************************************************************/
114 inline void
115 DynAr::addh ()
116 {
117  insert(tCount);
118 }
119 
120 
121 /*************************************************************
122  * Add garbage-filled element to the begin of array
123  *************************************************************/
124 inline void
125 DynAr::addl ()
126 {
127  insert(0);
128 }
129 
130 
131 /*************************************************************
132  * Add element to the end of array
133  *************************************************************/
134 inline void
135 DynAr::addh (const char* item)
136 {
137  insert(tCount, item);
138 }
139 
140 
141 /*************************************************************
142  * Add element to the begin of array
143  *************************************************************/
144 inline void
145 DynAr::addl (const char* item)
146 {
147  insert(0, item);
148 }
149 
150 
151 /*************************************************************
152  * Returns pointer at the needed element of array
153  *************************************************************/
154 inline char*
155 DynAr::item_ptr (unsigned i) const
156 {
157  if(i == _last_) i = tCount - 1;
158  if(i >= tCount)
159  Throw(ERROR__DA_OUT_OF_RANGE);
160 
161  return tar + i * tSize;
162 }
163 
164 
165 /*************************************************************
166  * Returns pointer at the first element of array
167  *************************************************************/
168 inline char*
169 DynAr::start_ptr () const
170 {
171  return tar;
172 }
173 
174 
175 /*************************************************************
176  * Returns count of elements
177  *************************************************************/
178 inline unsigned
179 DynAr::count () const
180 {
181  return tCount;
182 }
183 
184 
185 /*************************************************************
186  * Pack array to minimum needed size
187  *************************************************************/
188 inline void
189 DynAr::pack ()
190 {
191  extend_to(tCount);
192 }
193 
194 
195 #endif // DynAr.H
Definition: DynAr.H:22