Debian Codegraph @

terps/tads/tads3/vmvec.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmvec.h - T3 VM Vector class
12 Function
13 A Vector is a subclass of Collection. A Vector is a List whose
14 elements can be changed in-place, and whose size can change.
15 Notes
16
17 Modified
18 05/14/00 MJRoberts - Creation
19 */
20
21 #ifndef VMVEC_H
22 #define VMVEC_H
23
24 #include <stdlib.h>
25 #include "vmtype.h"
26 #include "vmobj.h"
27 #include "vmcoll.h"
28 #include "vmglob.h"
29 #include "vmstack.h"
30 #include "vmrun.h"
31
32
33 /* ------------------------------------------------------------------------ */
34 /*
35 * Vectors are very similar to lists, but a vector's contents can be
36 * modified during execution. Because vectors are dynamic, a vector's
37 * image file data are copied into the heap.
38 *
39 * A vector's image file data structure looks like this:
40 *
41 * UINT2 elements_allocated
42 *. UINT2 number_of_elements_used
43 *. DATAHOLDER element[1]
44 *. DATAHOLDER element[2]
45 *. etc, up to number_of_elements_used (NOT the number allocated)
46 *
47 * The object extension for a vector is almost identical, but adds a prefix
48 * giving the "allocated" size of the vector, and a suffix with a pointer
49 * to our original image data (if applicable) plus one bit per element
50 * after the end of the element[] list for undo bits:
51 *
52 * UINT2 allocated_elements
53 *. UINT2 number_of_elements
54 *. DATAHOLDER element[1]
55 *. DATAHOLDER element[2]
56 *. etc
57 *. undo_bits
58 *
59 * The allocated_elements counter gives the total number of elements
60 * allocated in the vector; this can be greater than the number of elements
61 * actually in use, in which case we have free space that we can use to add
62 * elements without reallocating the vector's memory.
63 *
64 * The undo_bits are packed 8 bits per byte. Note that the undo_bits
65 * follow all allocated slots, so these do not change location when we
66 * change the number of elements actually in use.
67 */
68
69 /* ------------------------------------------------------------------------ */
70 /*
71 * Vector metaclass
72 */
73 class CVmObjVector: public CVmObjCollection
74 {
75 friend class CVmMetaclassVector;
76 friend class CVmQSortVector;
77
78 public:
79 /* metaclass registration object */
80 static class CVmMetaclass *metaclass_reg_;
81 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
82
83 /* am I of the given metaclass? */
84 virtual int is_of_metaclass(class CVmMetaclass *meta) const
85 {
86 /* try my own metaclass and my base class */
87 return (meta == metaclass_reg_
88 || CVmObjCollection::is_of_metaclass(meta));
89 }
90
91 /* create dynamically using stack arguments */
92 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
93 uint argc);
94
95 /* create with no initial contents */
96 static vm_obj_id_t create(VMG_ int in_root_set);
97
98 /* create with a given number of elements */
99 static vm_obj_id_t create(VMG_ int in_root_set, size_t element_count);
100
101 /* create with initial contents */
102 static vm_obj_id_t create_fill(VMG_ int in_root_set, size_t element_count,
103 ...);
104
105 /*
106 * determine if an object is a vector - it is if the object's
107 * virtual metaclass registration index matches the class's static
108 * index
109 */
110 static int is_vector_obj(VMG_ vm_obj_id_t obj)
111 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
112
113 /* vectors are list-like for read access */
114 int is_listlike(VMG_ vm_obj_id_t) { return TRUE; }
115 int ll_length(VMG_ vm_obj_id_t) { return get_element_count(); }
116
117 /* set a property */
118 void set_prop(VMG_ class CVmUndo *undo,
119 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
120
121 /* call a static property */
122 static int call_stat_prop(VMG_ vm_val_t *result,
123 const uchar **pc_ptr, uint *argc,
124 vm_prop_id_t prop);
125
126 /* get a property */
127 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
128 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
129
130 /* cast to string */
131 const char *cast_to_string(VMG_ vm_obj_id_t self, vm_val_t *new_str) const
132 {
133 /* join the vector into a string, using commas as separators */
134 join(vmg_ new_str, self, ",", 1);
135
136 /* return the new string */
137 return new_str->get_as_string(vmg0_);
138 }
139
140 virtual const char *explicit_to_string(
141 VMG_ vm_obj_id_t self, vm_val_t *new_str, int radix, int flags) const;
142
143 /* add a value to the vector, yielding a new vector */
144 int add_val(VMG_ vm_val_t *result,
145 vm_obj_id_t self, const vm_val_t *val);
146
147 /* subtract a value from the vector, yielding a new vector */
148 int sub_val(VMG_ vm_val_t *result,
149 vm_obj_id_t self, const vm_val_t *val);
150
151 /* undo operations */
152 void notify_new_savept();
153 void apply_undo(VMG_ struct CVmUndoRecord *rec);
154 void mark_undo_ref(VMG_ struct CVmUndoRecord *rec);
155
156 /* we keep only strong references */
157 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
158
159 /* mark references */
160 void mark_refs(VMG_ uint state);
161
162 /*
163 * remove weak references - we keep only normal (strong) references,
164 * so this routine doesn't need to do anything
165 */
166 void remove_stale_weak_refs(VMG0_) { }
167
168 /* rebuild for image file */
169 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
170
171 /* reserve constant data */
172 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
173 vm_obj_id_t self);
174
175 /* convert to constant data */
176 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
177 vm_obj_id_t self);
178
179 /* save to a file */
180 void save_to_file(VMG_ class CVmFile *fp);
181
182 /* restore from a file */
183 void restore_from_file(VMG_ vm_obj_id_t self,
184 class CVmFile *fp, class CVmObjFixup *fixups);
185
186 /* load from an image file */
187 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
188
189 /* reload from the image file */
190 void reload_from_image(VMG_ vm_obj_id_t self,
191 const char *ptr, size_t siz);
192
193 /* index the vector */
194 int index_val_q(VMG_ vm_val_t *result, vm_obj_id_t self,
195 const vm_val_t *index_val);
196
197 /* set an indexed element of the vector */
198 int set_index_val_q(VMG_ vm_val_t *new_container, vm_obj_id_t self,
199 const vm_val_t *index_val, const vm_val_t *new_val);
200
201 /*
202 * Check a value for equality. We will match any list or vector with
203 * the same number of elements and the same value for each element.
204 */
205 int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const;
206
207 /* calculate a hash value for the vector */
208 uint calc_hash(VMG_ vm_obj_id_t self, int depth) const;
209
210 /*
211 * assume that we've been changed since loading, if we came from the
212 * image file
213 */
214 int is_changed_since_load() const { return TRUE; }
215
216 /*
217 * get the allocated number of elements of the vector - this will be
218 * greater than or equal to get_element_count(), and reflects the
219 * actual allocated size
220 */
221 size_t get_allocated_count() const
222 { return vmb_get_len(get_vector_ext_ptr()); }
223
224 /* get the number of elements in the vector */
225 size_t get_element_count() const
226 { return vmb_get_len(get_vector_ext_ptr() + 2); }
227
228 /* get an element without any range checking; 0-based index */
229 void get_element(size_t idx, vm_val_t *val) const
230 {
231 /* get the data from the data holder in our extension */
232 vmb_get_dh(get_element_ptr(idx), val);
233 }
234
235 /* append an element, with no undo */
236 void append_element(VMG_ vm_obj_id_t self, const vm_val_t *val);
237
238 /* set an element; 0-based index; no bounds checking or undo */
239 void set_element(size_t idx, const vm_val_t *val)
240 {
241 /* set the element's data holder from the value */
242 vmb_put_dh(get_element_ptr(idx), val);
243 }
244
245 /* set an element, recording undo for the change; 0-based index */
246 void set_element_undo(VMG_ vm_obj_id_t self,
247 size_t idx, const vm_val_t *val);
248
249 /*
250 * join the list into a string - this is the C++ interface to the
251 * self.join() method
252 */
253 void join(VMG_ vm_val_t *retval, vm_obj_id_t self,
254 const char *sep, size_t sep_len) const;
255
256 protected:
257 /* load image data */
258 void load_image_data(VMG_ const char *ptr, size_t siz);
259
260 /* for construction, remove all redundant elements */
261 void cons_uniquify(VMG0_);
262
263 /* uniquify the value for an appendUnique operation */
264 void uniquify_for_append(VMG_ vm_obj_id_t self);
265
266 /* create an iterator */
267 virtual void new_iterator(VMG_ vm_val_t *retval,
268 const vm_val_t *self_val);
269
270 /* create a live iterator */
271 virtual void new_live_iterator(VMG_ vm_val_t *retval,
272 const vm_val_t *self_val);
273
274 /* create a copy of the object */
275 vm_obj_id_t create_copy(VMG_ const vm_val_t *self_val);
276
277 /* create with no initial contents */
278 CVmObjVector() { ext_ = 0; }
279
280 /* create with a given number of elements */
281 CVmObjVector(VMG_ size_t element_count);
282
283 /* get a pointer to my vector data */
284 const char *get_vector_ext_ptr() const { return ext_; }
285
286 /*
287 * get my extension data pointer for construction purposes - this is a
288 * writable pointer, so that a caller can fill our data buffer during
289 * construction
290 */
291 char *cons_get_vector_ext_ptr() const { return ext_; }
292
293 /*
294 * Calculate the amount of space we need to store a list of a given
295 * length. We require two bytes for the length prefix, plus the space
296 * for each element, plus the space for the undo bits (one bit per
297 * element, but we pack eight bits per byte).
298 */
299 static size_t calc_alloc(size_t elecnt)
300 {
301 return (calc_alloc_prefix_and_ele(elecnt)
302 + ((elecnt + 7) & ~7));
303 }
304
305 /*
306 * calculate the allocation amount, including only the count prefix
307 * and element data
308 */
309 static size_t calc_alloc_prefix_and_ele(size_t elecnt)
310 {
311 return (2*VMB_LEN + calc_alloc_ele(elecnt));
312 }
313
314 /* calculate the allocation amount, including only the element storage */
315 static size_t calc_alloc_ele(size_t elecnt)
316 {
317 return elecnt * VMB_DATAHOLDER;
318 }
319
320 /* notify of deletion */
321 void notify_delete(VMG_ int in_root_set);
322
323 /* allocate space for the vector, given the number of elements */
324 void alloc_vector(VMG_ size_t element_count);
325
326 /* expand to accommodate the given number of new elements */
327 void expand_by(VMG_ vm_obj_id_t self, size_t added_elements);
328
329 /* get the allocation count increment */
330 size_t get_alloc_count_increment() const
331 {
332 /*
333 * we might want to make this parameterizable; for now use a
334 * fixed increment size
335 */
336 return 16;
337 }
338
339 /* push an element onto the stack */
340 void push_element(VMG_ size_t idx) const
341 {
342 /* push a stack element */
343 vm_val_t *p = G_stk->push();
344
345 /*
346 * get the data from the data holder in our extension directly into
347 * our new stack element
348 */
349 vmb_get_dh(get_element_ptr(idx), p);
350 }
351
352 /* set the allocated size */
353 void set_allocated_count(size_t cnt);
354
355 /* given an index, get a pointer to the element's data in the list */
356 char *get_element_ptr(size_t idx) const
357 {
358 /*
359 * figure out where this element's data holder is by skipping the
360 * count prefix, then skipping past preceding data holders
361 */
362 return cons_get_vector_ext_ptr() + 2*VMB_LEN + (idx * VMB_DATAHOLDER);
363 }
364
365 /* get a constant pointer to an element */
366 static const char *get_element_ptr_const(const char *ext, size_t idx)
367 {
368 return ext + 2*VMB_LEN + (idx * VMB_DATAHOLDER);
369 }
370
371 /* increment an element pointer */
372 static void inc_element_ptr(char **ptr) { *ptr += VMB_DATAHOLDER; }
373
374 /* get a given undo bit */
375 int get_undo_bit(size_t idx) const
376 {
377 char *p;
378 int bitnum;
379
380 /* get the byte containing the bit for this index */
381 p = get_undo_ptr() + (idx >> 3);
382
383 /* get the bit number within the byte */
384 bitnum = (idx & 7);
385
386 /* get this bit */
387 return ((*p & (1 << bitnum)) != 0);
388 }
389
390 /* set a given undo bit */
391 void set_undo_bit(size_t idx, int val)
392 {
393 char *p;
394 int bitnum;
395
396 /* get the byte containing the bit for this index */
397 p = get_undo_ptr() + (idx >> 3);
398
399 /* get the bit number within the byte */
400 bitnum = (idx & 7);
401
402 /* set or clear the bit */
403 if (val)
404 *p |= (1 << bitnum);
405 else
406 *p &= ~(1 << bitnum);
407 }
408
409 /* clear all undo bits */
410 void clear_undo_bits()
411 {
412 memset(get_undo_ptr(), 0, ((get_allocated_count() + 7) & ~7));
413 }
414
415 /* get a pointer to the first byte of the undo bits */
416 char *get_undo_ptr() const
417 {
418 /* the undo bits follow the last element data */
419 return (cons_get_vector_ext_ptr()
420 + 2*VMB_LEN
421 + get_allocated_count()*VMB_DATAHOLDER);
422 }
423
424 /* set the number of elements in the vector; no undo or size checking */
425 void set_element_count(size_t cnt)
426 { vmb_put_len(cons_get_vector_ext_ptr() + 2, cnt); }
427
428 /* set the element count, saving undo for the change */
429 void set_element_count_undo(VMG_ vm_obj_id_t self,
430 size_t new_element_count);
431
432 /* insert elements into the vector, keeping undo */
433 void insert_elements_undo(VMG_ vm_obj_id_t self,
434 int ins_idx, int add_cnt);
435
436 /* delete elements from the vector, keeping undo */
437 void remove_elements_undo(VMG_ vm_obj_id_t self,
438 size_t start_idx, size_t del_cnt);
439
440 /* general handler for indexOf, lastIndexOf, and countOf */
441 int gen_index_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc,
442 int forward, int count_only);
443
444 /* general handler for indexWhich, lastIndexWhich, and countWhich */
445 int gen_index_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc,
446 int forward, int count_only, vm_rcdesc *rc);
447
448 /* property evaluator - undefined function */
449 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
450
451 /* property evaluator - convert to a list */
452 int getp_to_list(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
453
454 /* property evaluator - get the number of elements */
455 int getp_get_size(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
456
457 /* property evaluator - copy from another vector or list */
458 int getp_copy_from(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
459
460 /* property evaluator - fill with a value */
461 int getp_fill_val(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
462
463 /* property evaluator - select a subset */
464 int getp_subset(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
465
466 /* property evaluator - apply a callback to each element */
467 int getp_apply_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
468
469 /* property evaluator - find the first element satisfying a condition */
470 int getp_index_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
471
472 /* property evaluator - call a callback on each element */
473 int getp_for_each(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
474
475 /* property evaluator - call a callback(key,val) on each element */
476 int getp_for_each_assoc(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
477
478 /* general processor for forEach/forEachAssoc */
479 int for_each_gen(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc,
480 int pass_key_to_cb, vm_rcdesc *rc);
481
482 /* property evaluator - mapAll */
483 int getp_map_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
484
485 /* property evaluator - indexOf */
486 int getp_index_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
487
488 /* property evaluator - valWhich */
489 int getp_val_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
490
491 /* property evaluator - lastIndexOf */
492 int getp_last_index_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
493
494 /* property evaluator - lastIndexWhich */
495 int getp_last_index_which(VMG_ vm_obj_id_t self,
496 vm_val_t *val, uint *argc);
497
498 /* property evaluator - valWhich */
499 int getp_last_val_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
500
501 /* property evaluator - countOf */
502 int getp_count_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
503
504 /* property evaluator - countWhich */
505 int getp_count_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
506
507 /* property evaluator - getUnique */
508 int getp_get_unique(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
509
510 /* property evaluator - appendUnique */
511 int getp_append_unique(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
512
513 /* property evaluator - sort */
514 int getp_sort(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
515
516 /* property evaluator - set the length */
517 int getp_set_length(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
518
519 /* property evaluator - insertAt */
520 int getp_insert_at(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
521
522 /* property evaluator - remove a single element at a given index */
523 int getp_remove_element_at(VMG_ vm_obj_id_t self, vm_val_t *retval,
524 uint *argc);
525
526 /* property evaluator - removeRange */
527 int getp_remove_range(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
528
529 /* property evaluator - append a value */
530 int getp_append(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
531
532 /* property evaluator - prepend a value */
533 int getp_prepend(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
534
535 /* property evaluator - append everything from a collection */
536 int getp_append_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
537
538 /* property evaluator - remove any elements with a given value */
539 int getp_remove_element(VMG_ vm_obj_id_t self, vm_val_t *retval,
540 uint *argc);
541
542 /* property evaluator - join into a string */
543 int getp_join(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
544
545 /* property evaluator - generate */
546 int getp_generate(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc)
547 { return static_getp_generate(vmg_ retval, argc); }
548
549 /* static property evaluator - generate */
550 static int static_getp_generate(VMG_ vm_val_t *retval, uint *argc);
551
552 /* property evaluator - splice */
553 int getp_splice(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
554
555 /* property evaluator - get the index of the minimum element */
556 int getp_indexOfMin(VMG_ vm_obj_id_t self, vm_val_t *retval,
557 uint *argc);
558
559 /* property evaluator - get the minimum value in the list */
560 int getp_minVal(VMG_ vm_obj_id_t self, vm_val_t *retval,
561 uint *argc);
562
563 /* property evaluator - get the index of the maximum element */
564 int getp_indexOfMax(VMG_ vm_obj_id_t self, vm_val_t *retval,
565 uint *argc);
566
567 /* property evaluator - get the maximum value in the list */
568 int getp_maxVal(VMG_ vm_obj_id_t self, vm_val_t *retval,
569 uint *argc);
570
571 /* general handler for the various min/max methods */
572 int get_minmax(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc,
573 const vm_rcdesc *rc, int sense, int want_index);
574
575 /* property evaluation function table */
576 static int (CVmObjVector::*func_table_[])(VMG_ vm_obj_id_t self,
577 vm_val_t *retval, uint *argc);
578 };
579
580
581 /* ------------------------------------------------------------------------ */
582 /*
583 * Registration table object
584 */
585 class CVmMetaclassVector: public CVmMetaclass
586 {
587 public:
588 /* get the global name */
589 const char *get_meta_name() const { return "vector/030005"; }
590
591 /* create from image file */
592 void create_for_image_load(VMG_ vm_obj_id_t id)
593 {
594 new (vmg_ id) CVmObjVector();
595 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
596 }
597
598 /* create from restoring from saved state */
599 void create_for_restore(VMG_ vm_obj_id_t id)
600 {
601 new (vmg_ id) CVmObjVector();
602 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
603 }
604
605 /* create dynamically using stack arguments */
606 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
607 { return CVmObjVector::create_from_stack(vmg_ pc_ptr, argc); }
608
609 /* call a static property */
610 int call_stat_prop(VMG_ vm_val_t *result,
611 const uchar **pc_ptr, uint *argc,
612 vm_prop_id_t prop)
613 {
614 return CVmObjVector::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
615 }
616
617 /* I'm a Collection object */
618 CVmMetaclass *get_supermeta_reg() const
619 { return CVmObjCollection::metaclass_reg_; }
620 };
621
622 #endif /* VMVEC_H */
623
624 /*
625 * Register the class
626 */
627 VM_REGISTER_METACLASS(CVmObjVector)