| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 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 |
class CVmObjVector: public CVmObjCollection |
| 74 |
{ |
| 75 |
friend class CVmMetaclassVector; |
| 76 |
friend class CVmQSortVector; |
| 77 |
|
| 78 |
public: |
| 79 |
|
| 80 |
static class CVmMetaclass *metaclass_reg_; |
| 81 |
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 82 |
|
| 83 |
|
| 84 |
virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 85 |
{ |
| 86 |
|
| 87 |
return (meta == metaclass_reg_ |
| 88 |
|| CVmObjCollection::is_of_metaclass(meta)); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 93 |
uint argc); |
| 94 |
|
| 95 |
|
| 96 |
static vm_obj_id_t create(VMG_ int in_root_set); |
| 97 |
|
| 98 |
|
| 99 |
static vm_obj_id_t create(VMG_ int in_root_set, size_t element_count); |
| 100 |
|
| 101 |
|
| 102 |
static vm_obj_id_t create_fill(VMG_ int in_root_set, size_t element_count, |
| 103 |
...); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 131 |
const char *cast_to_string(VMG_ vm_obj_id_t self, vm_val_t *new_str) const |
| 132 |
{ |
| 133 |
|
| 134 |
join(vmg_ new_str, self, ",", 1); |
| 135 |
|
| 136 |
|
| 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 |
|
| 144 |
int add_val(VMG_ vm_val_t *result, |
| 145 |
vm_obj_id_t self, const vm_val_t *val); |
| 146 |
|
| 147 |
|
| 148 |
int sub_val(VMG_ vm_val_t *result, |
| 149 |
vm_obj_id_t self, const vm_val_t *val); |
| 150 |
|
| 151 |
|
| 152 |
void notify_new_savept(); |
| 153 |
void apply_undo(VMG_ struct CVmUndoRecord *rec); |
| 154 |
void mark_undo_ref(VMG_ struct CVmUndoRecord *rec); |
| 155 |
|
| 156 |
|
| 157 |
void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 158 |
|
| 159 |
|
| 160 |
void mark_refs(VMG_ uint state); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void remove_stale_weak_refs(VMG0_) { } |
| 167 |
|
| 168 |
|
| 169 |
virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 170 |
|
| 171 |
|
| 172 |
virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper, |
| 173 |
vm_obj_id_t self); |
| 174 |
|
| 175 |
|
| 176 |
virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper, |
| 177 |
vm_obj_id_t self); |
| 178 |
|
| 179 |
|
| 180 |
void save_to_file(VMG_ class CVmFile *fp); |
| 181 |
|
| 182 |
|
| 183 |
void restore_from_file(VMG_ vm_obj_id_t self, |
| 184 |
class CVmFile *fp, class CVmObjFixup *fixups); |
| 185 |
|
| 186 |
|
| 187 |
void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz); |
| 188 |
|
| 189 |
|
| 190 |
void reload_from_image(VMG_ vm_obj_id_t self, |
| 191 |
const char *ptr, size_t siz); |
| 192 |
|
| 193 |
|
| 194 |
int index_val_q(VMG_ vm_val_t *result, vm_obj_id_t self, |
| 195 |
const vm_val_t *index_val); |
| 196 |
|
| 197 |
|
| 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 |
|
| 203 |
|
| 204 |
|
| 205 |
int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const; |
| 206 |
|
| 207 |
|
| 208 |
uint calc_hash(VMG_ vm_obj_id_t self, int depth) const; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
int is_changed_since_load() const { return TRUE; } |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
size_t get_allocated_count() const |
| 222 |
{ return vmb_get_len(get_vector_ext_ptr()); } |
| 223 |
|
| 224 |
|
| 225 |
size_t get_element_count() const |
| 226 |
{ return vmb_get_len(get_vector_ext_ptr() + 2); } |
| 227 |
|
| 228 |
|
| 229 |
void get_element(size_t idx, vm_val_t *val) const |
| 230 |
{ |
| 231 |
|
| 232 |
vmb_get_dh(get_element_ptr(idx), val); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
void append_element(VMG_ vm_obj_id_t self, const vm_val_t *val); |
| 237 |
|
| 238 |
|
| 239 |
void set_element(size_t idx, const vm_val_t *val) |
| 240 |
{ |
| 241 |
|
| 242 |
vmb_put_dh(get_element_ptr(idx), val); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
void set_element_undo(VMG_ vm_obj_id_t self, |
| 247 |
size_t idx, const vm_val_t *val); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 258 |
void load_image_data(VMG_ const char *ptr, size_t siz); |
| 259 |
|
| 260 |
|
| 261 |
void cons_uniquify(VMG0_); |
| 262 |
|
| 263 |
|
| 264 |
void uniquify_for_append(VMG_ vm_obj_id_t self); |
| 265 |
|
| 266 |
|
| 267 |
virtual void new_iterator(VMG_ vm_val_t *retval, |
| 268 |
const vm_val_t *self_val); |
| 269 |
|
| 270 |
|
| 271 |
virtual void new_live_iterator(VMG_ vm_val_t *retval, |
| 272 |
const vm_val_t *self_val); |
| 273 |
|
| 274 |
|
| 275 |
vm_obj_id_t create_copy(VMG_ const vm_val_t *self_val); |
| 276 |
|
| 277 |
|
| 278 |
CVmObjVector() { ext_ = 0; } |
| 279 |
|
| 280 |
|
| 281 |
CVmObjVector(VMG_ size_t element_count); |
| 282 |
|
| 283 |
|
| 284 |
const char *get_vector_ext_ptr() const { return ext_; } |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
char *cons_get_vector_ext_ptr() const { return ext_; } |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 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 |
|
| 307 |
|
| 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 |
|
| 315 |
static size_t calc_alloc_ele(size_t elecnt) |
| 316 |
{ |
| 317 |
return elecnt * VMB_DATAHOLDER; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
void notify_delete(VMG_ int in_root_set); |
| 322 |
|
| 323 |
|
| 324 |
void alloc_vector(VMG_ size_t element_count); |
| 325 |
|
| 326 |
|
| 327 |
void expand_by(VMG_ vm_obj_id_t self, size_t added_elements); |
| 328 |
|
| 329 |
|
| 330 |
size_t get_alloc_count_increment() const |
| 331 |
{ |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
return 16; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
void push_element(VMG_ size_t idx) const |
| 341 |
{ |
| 342 |
|
| 343 |
vm_val_t *p = G_stk->push(); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
vmb_get_dh(get_element_ptr(idx), p); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
void set_allocated_count(size_t cnt); |
| 354 |
|
| 355 |
|
| 356 |
char *get_element_ptr(size_t idx) const |
| 357 |
{ |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
return cons_get_vector_ext_ptr() + 2*VMB_LEN + (idx * VMB_DATAHOLDER); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 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 |
|
| 372 |
static void inc_element_ptr(char **ptr) { *ptr += VMB_DATAHOLDER; } |
| 373 |
|
| 374 |
|
| 375 |
int get_undo_bit(size_t idx) const |
| 376 |
{ |
| 377 |
char *p; |
| 378 |
int bitnum; |
| 379 |
|
| 380 |
|
| 381 |
p = get_undo_ptr() + (idx >> 3); |
| 382 |
|
| 383 |
|
| 384 |
bitnum = (idx & 7); |
| 385 |
|
| 386 |
|
| 387 |
return ((*p & (1 << bitnum)) != 0); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
void set_undo_bit(size_t idx, int val) |
| 392 |
{ |
| 393 |
char *p; |
| 394 |
int bitnum; |
| 395 |
|
| 396 |
|
| 397 |
p = get_undo_ptr() + (idx >> 3); |
| 398 |
|
| 399 |
|
| 400 |
bitnum = (idx & 7); |
| 401 |
|
| 402 |
|
| 403 |
if (val) |
| 404 |
*p |= (1 << bitnum); |
| 405 |
else |
| 406 |
*p &= ~(1 << bitnum); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
void clear_undo_bits() |
| 411 |
{ |
| 412 |
memset(get_undo_ptr(), 0, ((get_allocated_count() + 7) & ~7)); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
char *get_undo_ptr() const |
| 417 |
{ |
| 418 |
|
| 419 |
return (cons_get_vector_ext_ptr() |
| 420 |
+ 2*VMB_LEN |
| 421 |
+ get_allocated_count()*VMB_DATAHOLDER); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
void set_element_count(size_t cnt) |
| 426 |
{ vmb_put_len(cons_get_vector_ext_ptr() + 2, cnt); } |
| 427 |
|
| 428 |
|
| 429 |
void set_element_count_undo(VMG_ vm_obj_id_t self, |
| 430 |
size_t new_element_count); |
| 431 |
|
| 432 |
|
| 433 |
void insert_elements_undo(VMG_ vm_obj_id_t self, |
| 434 |
int ins_idx, int add_cnt); |
| 435 |
|
| 436 |
|
| 437 |
void remove_elements_undo(VMG_ vm_obj_id_t self, |
| 438 |
size_t start_idx, size_t del_cnt); |
| 439 |
|
| 440 |
|
| 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 |
|
| 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 |
|
| 449 |
int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; } |
| 450 |
|
| 451 |
|
| 452 |
int getp_to_list(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 453 |
|
| 454 |
|
| 455 |
int getp_get_size(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 456 |
|
| 457 |
|
| 458 |
int getp_copy_from(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 459 |
|
| 460 |
|
| 461 |
int getp_fill_val(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 462 |
|
| 463 |
|
| 464 |
int getp_subset(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 465 |
|
| 466 |
|
| 467 |
int getp_apply_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 468 |
|
| 469 |
|
| 470 |
int getp_index_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 471 |
|
| 472 |
|
| 473 |
int getp_for_each(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 474 |
|
| 475 |
|
| 476 |
int getp_for_each_assoc(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 477 |
|
| 478 |
|
| 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 |
|
| 483 |
int getp_map_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 484 |
|
| 485 |
|
| 486 |
int getp_index_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 487 |
|
| 488 |
|
| 489 |
int getp_val_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 490 |
|
| 491 |
|
| 492 |
int getp_last_index_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 493 |
|
| 494 |
|
| 495 |
int getp_last_index_which(VMG_ vm_obj_id_t self, |
| 496 |
vm_val_t *val, uint *argc); |
| 497 |
|
| 498 |
|
| 499 |
int getp_last_val_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 500 |
|
| 501 |
|
| 502 |
int getp_count_of(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 503 |
|
| 504 |
|
| 505 |
int getp_count_which(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 506 |
|
| 507 |
|
| 508 |
int getp_get_unique(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 509 |
|
| 510 |
|
| 511 |
int getp_append_unique(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 512 |
|
| 513 |
|
| 514 |
int getp_sort(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 515 |
|
| 516 |
|
| 517 |
int getp_set_length(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 518 |
|
| 519 |
|
| 520 |
int getp_insert_at(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 521 |
|
| 522 |
|
| 523 |
int getp_remove_element_at(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 524 |
uint *argc); |
| 525 |
|
| 526 |
|
| 527 |
int getp_remove_range(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 528 |
|
| 529 |
|
| 530 |
int getp_append(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 531 |
|
| 532 |
|
| 533 |
int getp_prepend(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 534 |
|
| 535 |
|
| 536 |
int getp_append_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 537 |
|
| 538 |
|
| 539 |
int getp_remove_element(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 540 |
uint *argc); |
| 541 |
|
| 542 |
|
| 543 |
int getp_join(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 544 |
|
| 545 |
|
| 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 |
|
| 550 |
static int static_getp_generate(VMG_ vm_val_t *retval, uint *argc); |
| 551 |
|
| 552 |
|
| 553 |
int getp_splice(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 554 |
|
| 555 |
|
| 556 |
int getp_indexOfMin(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 557 |
uint *argc); |
| 558 |
|
| 559 |
|
| 560 |
int getp_minVal(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 561 |
uint *argc); |
| 562 |
|
| 563 |
|
| 564 |
int getp_indexOfMax(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 565 |
uint *argc); |
| 566 |
|
| 567 |
|
| 568 |
int getp_maxVal(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 569 |
uint *argc); |
| 570 |
|
| 571 |
|
| 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 |
|
| 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 |
|
| 584 |
|
| 585 |
class CVmMetaclassVector: public CVmMetaclass |
| 586 |
{ |
| 587 |
public: |
| 588 |
|
| 589 |
const char *get_meta_name() const { return "vector/030005"; } |
| 590 |
|
| 591 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 618 |
CVmMetaclass *get_supermeta_reg() const |
| 619 |
{ return CVmObjCollection::metaclass_reg_; } |
| 620 |
}; |
| 621 |
|
| 622 |
#endif |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
VM_REGISTER_METACLASS(CVmObjVector) |