1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// This may look like C code, but it is really -*- C++ -*-
/*
Copyright (C) 1988 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
based on code by Marc Shapiro (shapiro@sor.inria.fr)
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
*/
#ifndef _<T>MPlex_h
#ifdef __GNUG__
#pragma once
#pragma interface
#endif
#define _<T>MPlex_h 1
#include "<T>.Plex.h"
// Number of bits per long, used in MChunk bit map operations
#define _MAP_BITS 32
class <T>MChunk : public <T>IChunk
{
protected:
unsigned long* map; // bitmap of slots
int unused; // number of unused internal slots
void mark(int); // bitmap operations
void free(int);
int valid(int) const;
public:
<T>MChunk(<T>* d, // ptr to array of elements
int base_idx, // initial indices
int low_idx, // & initially clear map
int fence_idx,
int top_idx);
~<T>MChunk();
// virtuals
int first_index() const;
int last_index() const;
int succ(int idx) const;
int pred(int idx) const;
<T>* first_pointer() const;
<T>* last_pointer() const;
<T>* succ(<T>*) const;
<T>* pred(<T>*) const;
int empty() const;
int full() const;
int valid_index(int i) const;
int valid_pointer(const <T>* p) const;
<T>* grow_high ();
<T>* grow_low ();
void shrink_high ();
void shrink_low ();
void clear(int);
void cleardown(int);
int OK() const;
// extensions
int unused_indices() const; // how many free slot in low..fence?
int unused_index() const; // return index of free slot
int del(int i); // delete data indexed by i
// return true if was present
int undel(int idx); // un-delete data indexed by i
// return true if already present
void reset_low(); // reset low = lowest valid index;
void reset_high(); // same for high
};
class <T>MPlex: public <T>Plex
{
<T>MChunk* ch; // cached chunk
int unused; // # of free slots between low & fence
void make_initial_chunks(int up = 1);
void cache(int idx) const;
void cache(const <T>* p) const;
int dopred(int) const;
int dosucc(int) const;
void set_cache(const <T>MChunk* t) const; // logically,
// not physically const
public:
<T>MPlex(); // set low = 0;
// fence = 0;
// csize = default
<T>MPlex(int ch_size); // low = 0;
// fence = 0;
// csize = ch_size
<T>MPlex(int lo, // low = lo;
int ch_size); // fence=lo
// csize = ch_size
<T>MPlex(int lo, // low = lo
int hi, // fence = hi+1
const <T&> initval,// fill with initval,
int ch_size = 0); // csize= ch_size
// or fence-lo if 0
<T>MPlex(const <T>MPlex&);
void operator= (const <T>MPlex&);
// virtuals
<T>& high_element ();
<T>& low_element ();
const <T>& high_element () const;
const <T>& low_element () const;
Pix first() const;
Pix last() const ;
void prev(Pix& ptr) const;
void next(Pix& ptr) const;
int owns(Pix p) const;
<T>& operator () (Pix p);
const <T>& operator () (Pix p) const;
int low() const;
int high() const;
int valid(int idx) const;
void prev(int& idx) const;
void next(int& x) const;
<T>& operator [] (int index);
const <T>& operator [] (int index) const;
int Pix_to_index(Pix p) const;
Pix index_to_Pix(int idx) const;
int can_add_high() const;
int can_add_low() const;
int full() const;
int add_high(const <T&> elem);
int del_high ();
int add_low (const <T&> elem);
int del_low ();
void clear();
int OK () const;
// extensions
int count() const; // # valid elements
int available() const; // # deleted elements
int unused_index()const; // return index of a deleted elem
Pix unused_Pix() const; // return Pix of a deleted elem
int del_index(int idx); // logically delete at idx;
// return true if was present
int del_Pix(Pix p); // delete at p
void undel_index(int idx); // undelete at idx;
void undel_Pix(Pix p); // undelete at p;
void adjust_bounds(); // reset lo, hi to lowest &
// highest valid indices
int add(const <T&> elem); // add anywhere
};
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
inline <T>MChunk:: ~<T>MChunk()
{
delete map;
}
inline void <T>MChunk::mark(int idx)
{
unsigned int i = idx - base;
map[i / _MAP_BITS] |= 1 << (i & (_MAP_BITS - 1));
}
inline void <T>MChunk::free(int idx)
{
unsigned int i = idx - base;
map[i / _MAP_BITS] &= ~(1 << (i & (_MAP_BITS - 1)));
}
inline int <T>MChunk::valid(int idx) const
{
unsigned int i = idx - base;
return map[i / _MAP_BITS] & (1 << (i & (_MAP_BITS - 1)));
}
inline int <T>MChunk:: valid_index(int i) const
{
return i >= low && i < fence && valid(i);
}
inline int <T>MChunk:: valid_pointer(const <T>* p) const
{
int i = ((int)p - (int)data) / sizeof(<T>);
return i >= 0 && i < (fence - base) &&
(map[(unsigned)i / _MAP_BITS] & (1 << (i & (_MAP_BITS - 1))));
}
inline int <T>MChunk::empty() const
{
return fence - low - unused == 0;
}
inline int <T>MChunk::full() const
{
return unused + (top - fence) + (low - base) == 0;
}
inline int <T>MChunk::succ(int idx) const
{
int i = (idx < low)? low : idx + 1;
while (i < fence && !valid(i)) ++i;
return i;
}
inline int <T>MChunk::pred(int idx) const
{
int i = (idx > fence)? (fence - 1) : idx - 1;
while (i >= low && !valid(i)) --i;
return i;
}
inline int <T>MChunk::unused_indices() const
{
return unused;
}
inline <T>* <T>MChunk:: grow_high ()
{
if (!can_grow_high()) full_error();
mark(fence);
return &(data[fence++ - base]);
}
inline <T>* <T>MChunk:: grow_low ()
{
if (!can_grow_low()) full_error();
mark(--low);
return &(data[low - base]);
}
inline void <T>MChunk::reset_low()
{
while (low < fence && !valid(low))
{
--unused;
++low;
}
}
inline void <T>MChunk::reset_high()
{
while (fence > low && !valid(fence - 1))
{
--unused;
--fence;
}
}
inline int <T>MPlex::full () const
{
return 0;
}
inline int <T>MPlex::can_add_high() const
{
return 1;
}
inline int <T>MPlex::can_add_low() const
{
return 1;
}
inline int <T>MPlex::available() const
{
return unused;
}
inline int <T>MPlex::count() const
{
return fnc - lo - unused;
}
inline void <T>MPlex::set_cache(const <T>MChunk* t) const
{
((<T>MPlex*)(this))->ch = (<T>MChunk*)t;
}
inline <T>& <T>MPlex:: operator [] (int idx)
{
if (!ch-><T>MChunk::valid_index(idx)) cache(idx);
return * (ch->pointer_to(idx));
}
inline const <T>& <T>MPlex:: operator [] (int idx) const
{
if (!ch-><T>MChunk::valid_index(idx)) cache(idx);
return * ((const <T>*)(ch->pointer_to(idx)));
}
inline int <T>MPlex::Pix_to_index(Pix p) const
{
if (!ch-><T>MChunk::valid_pointer((<T>*)p)) cache((<T>*)p);
return ch->index_of((<T>*)p);
}
inline int <T>MPlex::high() const
{
return (((const <T>MChunk*)tl())-><T>MChunk::valid_index(fnc-1)) ?
fnc-1 : dopred(fnc-1);
}
inline int <T>MPlex::low() const
{
return (((const <T>MChunk*)hd)-><T>MChunk::valid_index(lo))? lo : dosucc(lo);
}
inline <T>& <T>MPlex::low_element ()
{
return (*this)[low()];
}
inline const <T>& <T>MPlex::low_element () const
{
return (*this)[low()];
}
inline <T>& <T>MPlex::high_element ()
{
return (*this)[high()];
}
inline const <T>& <T>MPlex::high_element () const
{
return (*this)[high()];
}
inline Pix <T>MPlex::index_to_Pix(int idx) const
{
if (!ch-><T>MChunk::valid_index(idx)) cache(idx);
return Pix(ch->pointer_to(idx));
}
inline void <T>MPlex::next(int& idx) const
{
idx = (ch-><T>MChunk::valid_index(idx+1))? idx+1 : dosucc(idx);
}
inline void <T>MPlex::prev(int& idx) const
{
idx = (ch-><T>MChunk::valid_index(idx-1))? idx-1 : dopred(idx);
}
inline Pix <T>MPlex::first() const
{
return index_to_Pix(low());
}
inline Pix <T>MPlex::last() const
{
return index_to_Pix(high());
}
inline void <T>MPlex::undel_Pix(Pix p)
{
undel_index(Pix_to_index(p));
}
inline int <T>MPlex::del_Pix(Pix p)
{
return del_index(Pix_to_index(p));
}
inline <T>& <T>MPlex:: operator () (Pix p)
{
return *((<T>*)p);
}
inline const <T>& <T>MPlex:: operator () (Pix p) const
{
return *((const <T>*)p);
}
#endif
#endif
|