Skip to content
This repository
branch: constant_unfol…
Fetching contributors…

Cannot retrieve contributors at this time

file 860 lines (604 sloc) 22.067 kb
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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
/*
Copyright (C) 2001-2010, Parrot Foundation.
$Id$

=head1 NAME

src/pmc/orderedhash.pmc - Ordered Hash

=head1 DESCRIPTION

C<OrderedHash> provide the interfaces of C<array> and
C<hash>.

Some limitations:

=over 4

=item *

Keys are always STRING*.

=item *

Values are always PMC*.

=back

To archive ordering for each item we store:

=over 4

=item * C<key>

Original key

=item * C<value>

Original value

=item * C<next>

Pointer to next C<item>

=item * C<prev>

Pointer to previous C<item>

=back

OrderedHash stores next things:

=over 4

=item * C<hash>

Lookup hash for values.

=item * C<first>

Pointer to first inserted value.

=item * C<last>

Pointer to last inserter value.

=back

See F<t/pmc/orderedhash.t> for test cases.

Overall design heavily inspired by C<Tie::StoredOrderHash>.

=head2 Methods

=over 4

=cut

*/

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC* box_integer(PARROT_INTERP, INTVAL val)
        __attribute__nonnull__(1);

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC* box_number(PARROT_INTERP, FLOATVAL val)
        __attribute__nonnull__(1);

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC* box_string(PARROT_INTERP, ARGIN(STRING *str))
        __attribute__nonnull__(1)
        __attribute__nonnull__(2);

static void find_bounds(PARROT_INTERP,
    ARGIN(PMC *pmc_hash),
    ARGMOD(PMC **first),
    ARGMOD(PMC **last))
        __attribute__nonnull__(1)
        __attribute__nonnull__(2)
        __attribute__nonnull__(3)
        __attribute__nonnull__(4)
        FUNC_MODIFIES(*first)
        FUNC_MODIFIES(*last);

PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC* get_list_item(PARROT_INTERP, ARGIN(PMC *self), INTVAL idx)
        __attribute__nonnull__(1)
        __attribute__nonnull__(2);

#define ASSERT_ARGS_box_integer __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
       PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_box_number __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
       PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_box_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
       PARROT_ASSERT_ARG(interp) \
    , PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_find_bounds __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
       PARROT_ASSERT_ARG(interp) \
    , PARROT_ASSERT_ARG(pmc_hash) \
    , PARROT_ASSERT_ARG(first) \
    , PARROT_ASSERT_ARG(last))
#define ASSERT_ARGS_get_list_item __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
       PARROT_ASSERT_ARG(interp) \
    , PARROT_ASSERT_ARG(self))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */

/* Get list_item by index */
/* XXX Can this actually return NULL or not? */
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC*
get_list_item(PARROT_INTERP, ARGIN(PMC *self), INTVAL idx)
{
    ASSERT_ARGS(get_list_item)

    const Parrot_OrderedHash_attributes * const attrs = PARROT_ORDEREDHASH(self);
    const INTVAL n = VTABLE_elements(interp, attrs->hash);
    INTVAL pos;
    PMC *list_entry = attrs->first;

    if (idx < -n)
        idx = -idx - n - 1;
    else if (idx < 0)
        idx += n;

    /* Iterate over linked list to get list_item */
    for (pos = 0; pos < idx; ++pos) {
        list_entry = VTABLE_get_pmc_keyed_int(interp, list_entry, ORDERED_HASH_ITEM_NEXT);
    }
    return list_entry;
}

/* Find first/last in cloned/thawed OrderedHash */
/* Parameter C<pmc_hash> is Hash, not OrderedHash */
static void
find_bounds(PARROT_INTERP, ARGIN(PMC *pmc_hash), ARGMOD(PMC **first), ARGMOD(PMC **last))
{
    ASSERT_ARGS(find_bounds)

    PMC * const iter = VTABLE_get_iter(interp, pmc_hash);

    while (VTABLE_get_bool(interp, iter)) {
        PMC * const item = VTABLE_shift_pmc(interp, iter);
        PMC * const entry = VTABLE_get_pmc_keyed(interp, pmc_hash, item);
        /* First entry doesn't have prev */
        PMC *tmp = VTABLE_get_pmc_keyed_int(interp, entry, ORDERED_HASH_ITEM_PREV);
        if (PMC_IS_NULL(tmp))
            *first = entry;
        /* Last entry doesn't have next */
        tmp = VTABLE_get_pmc_keyed_int(interp, entry, ORDERED_HASH_ITEM_NEXT);
        if (PMC_IS_NULL(tmp))
            *last = entry;
    }
}

/* Helpers for boxing values */
PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC*
box_string(PARROT_INTERP, ARGIN(STRING *str))
{
    ASSERT_ARGS(box_string)

    PMC * const ret = Parrot_pmc_new(interp, Parrot_get_ctx_HLL_type(interp,
                enum_class_String));
    VTABLE_set_string_native(interp, ret, str);
    return ret;
}

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC*
box_integer(PARROT_INTERP, INTVAL val)
{
    ASSERT_ARGS(box_integer)

    PMC * const ret = Parrot_pmc_new(interp, Parrot_get_ctx_HLL_type(interp,
                enum_class_Integer));
    VTABLE_set_integer_native(interp, ret, val);
    return ret;
}

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC*
box_number(PARROT_INTERP, FLOATVAL val)
{
    ASSERT_ARGS(box_number)

    PMC * const ret = Parrot_pmc_new(interp, Parrot_get_ctx_HLL_type(interp,
                enum_class_Float));
    VTABLE_set_number_native(interp, ret, val);
    return ret;
}


pmclass OrderedHash need_ext provides array provides hash auto_attrs {
    ATTR PMC *hash; /* key to item tuple */
    ATTR PMC *first; /* Pointer to first inserted value */
    ATTR PMC *last; /* Pointer to last inserted value */

/*

=item C<void init()>

Create new instance of OrderedHash.

=cut

*/

    VTABLE void init() {
        Parrot_OrderedHash_attributes * const attrs =
                (Parrot_OrderedHash_attributes*) PMC_data(SELF);

        attrs->hash = Parrot_pmc_new(INTERP, enum_class_Hash);
        attrs->first = PMCNULL;
        attrs->last = PMCNULL;

        PObj_custom_mark_destroy_SETALL(SELF);
    }

/*

=item C<void mark()>

Marks the OrderedHash as live.

=cut

*/

    VTABLE void mark() {
        Parrot_OrderedHash_attributes * const attrs =
                PARROT_ORDEREDHASH(SELF);

        if (attrs->hash)
            Parrot_gc_mark_PObj_alive(INTERP, (PObj *)attrs->hash);

        /* Don't mark C<first> and C<last>. They are in lookup hash anyway */
    }

/*

=item C<PMC *get_iter()>

Return a new iterator

=cut

*/

    VTABLE PMC *get_iter() {
        return Parrot_pmc_new_init(INTERP, enum_class_OrderedHashIterator, SELF);
    }

/*

=item C<INTVAL elements()>

=item C<INTVAL get_integer()>

=item C<FLOATVAL get_number()>

Returns the size of the hash.

=cut

*/

    VTABLE INTVAL get_integer() {
        return STATICSELF.elements();
    }

    VTABLE FLOATVAL get_number() {
        return SELF.get_integer();
    }

    VTABLE INTVAL elements() {
        return VTABLE_elements(INTERP, PARROT_ORDEREDHASH(SELF)->hash);
    }

/*

=item C<set_pmc_keyed(PMC *key, PMC *value)>

Main set function.

=cut

*/
    VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
        Parrot_OrderedHash_attributes * const attrs =
                PARROT_ORDEREDHASH(SELF);

        /* Check for old entry */
        PMC *list_entry = VTABLE_get_pmc_keyed(INTERP, attrs->hash, key);
        if (!PMC_IS_NULL(list_entry)) {
            /* We have old entry. Just update value */
            PMC * const nextkey = key_next(INTERP, key);
            if (nextkey) {
                /* XXX old_value is unused. Should we be storing this at all? */
                PMC * const old_value = VTABLE_get_pmc_keyed_int(INTERP, list_entry,
                        ORDERED_HASH_ITEM_VALUE);
                VTABLE_set_pmc_keyed(INTERP, value, nextkey, value);
            }
            else
                VTABLE_set_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_VALUE, value);
            return;
        }

        /* Create new entry */
        list_entry = Parrot_pmc_new_init_int(INTERP,
                enum_class_FixedPMCArray, ORDERED_HASH_ITEM_MAX);
        VTABLE_set_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_VALUE, value);
        VTABLE_set_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_KEY, key);

        /* .. and link it */
        if (!PMC_IS_NULL(attrs->last)) {
            VTABLE_set_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_PREV, attrs->last);
            VTABLE_set_pmc_keyed_int(INTERP, attrs->last, ORDERED_HASH_ITEM_NEXT, list_entry);
        }
        attrs->last = list_entry;
        if (PMC_IS_NULL(attrs->first))
            attrs->first = list_entry;

        /* .. and store it */
        VTABLE_set_pmc_keyed(INTERP, attrs->hash, key, list_entry);
    }
/*

=item C<void set_integer_keyed(INTVAL key, INTVAL value)>

=item C<void set_number_keyed(INTVAL key, FLOATVAL value)>

=item C<void set_string_keyed(INTVAL key, STRING *value)>

Sets the PMC value of the element at index C<key> to C<val>.

=cut

*/

    VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
        PMC * const v = box_integer(INTERP, value);
        SELF.set_pmc_keyed(key, v);
    }

    VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
        PMC * const v = box_number(INTERP, value);
        SELF.set_pmc_keyed(key, v);
    }

    VTABLE void set_string_keyed(PMC *key, STRING *value) {
        PMC * const v = box_string(INTERP, value);
        SELF.set_pmc_keyed(key, v);
    }

/*

=item C<void set_pmc_keyed_str(STRING *key, PMC *val)>

=item C<void set_integer_keyed_str(STRING *key, INTVAL value)>

=item C<void set_number_keyed_str(STRING *key, FLOATVAL value)>

=item C<void set_string_keyed_str(STRING *key, STRING *value)>

Sets the PMC value of the element at index C<key> to C<val>.

=cut

*/

    VTABLE void set_pmc_keyed_str(STRING *key, PMC *value) {
        /* Wallpapering problem with HLL Strings as keys */
        /* Apparently HLL registry in Parrot uses OrderedHash */
        /* Now we have chicken and egg problem during freeze/thaw */
        /* When we try to thaw OrderedHash which stores HLL mapping */
        /* Reported by François Perrad */
        PMC * const pkey = Parrot_pmc_new(INTERP, enum_class_String);
        VTABLE_set_string_native(INTERP, pkey, key);
        VTABLE_set_pmc_keyed(INTERP, SELF, pkey, value);
    }

    VTABLE void set_string_keyed_str(STRING *key, STRING *value) {
        PMC * const v = box_string(INTERP, value);
        STATICSELF.set_pmc_keyed_str(key, v);
    }

    VTABLE void set_integer_keyed_str(STRING *key, INTVAL value) {
        PMC * const v = box_integer(INTERP, value);
        STATICSELF.set_pmc_keyed_str(key, v);
    }

    VTABLE void set_number_keyed_str(STRING *key, FLOATVAL value) {
        PMC * const v = box_number(INTERP, value);
        STATICSELF.set_pmc_keyed_str(key, v);
    }

/*

=item C<PMC *get_pmc_keyed(PMC *key)>

=item C<PMC *get_pmc_keyed_int(INTVAL key)>

=item C<PMC *get_pmc_keyed_str(STRING *key)>

=cut

*/

    VTABLE PMC *get_pmc_keyed_int(INTVAL idx) {
        PMC * const list_entry = get_list_item(INTERP, SELF, idx);

        if (PMC_IS_NULL(list_entry))
            return PMCNULL;

        return VTABLE_get_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_VALUE);
    }

    VTABLE PMC *get_pmc_keyed(PMC *key) {
        PMC *item;

        /* Access by integer index */
        if ((PObj_get_FLAGS(key) & KEY_type_FLAGS) == KEY_integer_FLAG)
            return SELF.get_pmc_keyed_int(VTABLE_get_integer(INTERP, key));

        item = VTABLE_get_pmc_keyed(INTERP, PARROT_ORDEREDHASH(SELF)->hash, key);
        if (PMC_IS_NULL(item))
            return PMCNULL;
        return VTABLE_get_pmc_keyed_int(INTERP, item, ORDERED_HASH_ITEM_VALUE);
    }

    VTABLE PMC *get_pmc_keyed_str(STRING *key) {
        PMC *item = VTABLE_get_pmc_keyed_str(INTERP,
            PARROT_ORDEREDHASH(SELF)->hash, key);

        if (PMC_IS_NULL(item))
            return PMCNULL;

        return VTABLE_get_pmc_keyed_int(INTERP, item, ORDERED_HASH_ITEM_VALUE);
    }
/*

=item C<STRING *get_string_keyed(PMC *key)>

=item C<STRING *get_string_keyed_int(INTVAL key)>

=item C<STRING *get_string_keyed_str(STRING *key)>

=cut

*/

    VTABLE STRING *get_string_keyed_int(INTVAL idx) {
        PMC * const item = VTABLE_get_pmc_keyed_int(INTERP, SELF, idx);
        return VTABLE_get_string(INTERP, item);
    }

    VTABLE STRING *get_string_keyed(PMC *key) {
        PMC * const item = VTABLE_get_pmc_keyed(INTERP, SELF, key);
        return VTABLE_get_string(INTERP, item);
    }
/*

=item C<INTVAL get_integer_keyed(PMC *key)>

=item C<INTVAL get_integer_keyed_str(STRING *key)>

=item C<INTVAL get_integer_keyed_int(INTVAL key)>

Returns the integer value associated with C<key>.

=cut

*/

    VTABLE INTVAL get_integer_keyed_int(INTVAL idx) {
        PMC * const item = VTABLE_get_pmc_keyed_int(INTERP, SELF, idx);
        return VTABLE_get_integer(INTERP, item);
    }

    VTABLE INTVAL get_integer_keyed(PMC *key) {
        PMC * const item = VTABLE_get_pmc_keyed(INTERP, SELF, key);
        return VTABLE_get_integer(INTERP, item);
    }

/*

=item C<FLOATVAL get_number_keyed(PMC *key)>

=item C<FLOATVAL get_number_keyed_int(INTVAL key)>

=item C<FLOATVAL get_number_keyed_str(STRING *key)>

Returns the floating-point value for the element at C<key>.

=cut

*/

    VTABLE FLOATVAL get_number_keyed_int(INTVAL idx) {
        PMC * const item = VTABLE_get_pmc_keyed_int(INTERP, SELF, idx);
        return VTABLE_get_number(INTERP, item);
    }

    VTABLE FLOATVAL get_number_keyed(PMC *key) {
        PMC * const item = VTABLE_get_pmc_keyed(INTERP, SELF, key);
        return VTABLE_get_number(INTERP, item);
    }

/*

=item C<void set_pmc_keyed_int(INTVAL idx, PMC *val)>

=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>

=item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>

=item C<void set_string_keyed_int(INTVAL key, STRING *value)>

Sets the PMC value of the element at index C<key> to C<val>.
The created key = "\1idx".

=cut

*/

    VTABLE void set_pmc_keyed_int(INTVAL idx, PMC *val) {
        const INTVAL n = STATICSELF.elements();

        if (idx < -n)
            idx = -idx - n - 1;
        else if (idx < 0)
            idx += n;

        if (idx >= n) {
            /* TODO warn or fill if there are holes */
            STRING * const fmt = CONST_STRING(INTERP, "\1%d");
            STRING * const key = Parrot_sprintf_s(INTERP, fmt, idx);
            SELF.set_pmc_keyed_str(key, val);
        }
        else {
            PMC * const list_entry = get_list_item(INTERP, SELF, idx);
            PARROT_ASSERT(!PMC_IS_NULL(list_entry));
            VTABLE_set_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_VALUE, val);
        }
    }

    VTABLE void set_integer_keyed_int(INTVAL idx, INTVAL value) {
        PMC * const v = Parrot_pmc_new(INTERP, Parrot_get_ctx_HLL_type(INTERP,
                    enum_class_Integer));
        VTABLE_set_integer_native(INTERP, v, value);
        SELF.set_pmc_keyed_int(idx, v);
    }

    VTABLE void set_number_keyed_int(INTVAL idx, FLOATVAL value) {
        PMC * const v = Parrot_pmc_new(INTERP, Parrot_get_ctx_HLL_type(INTERP,
                    enum_class_Float));
        VTABLE_set_number_native(INTERP, v, value);
        SELF.set_pmc_keyed_int(idx, v);
    }

    VTABLE void set_string_keyed_int(INTVAL idx, STRING *value) {
        PMC * const v = Parrot_pmc_new(INTERP, Parrot_get_ctx_HLL_type(INTERP,
                    enum_class_String));
        VTABLE_set_string_native(INTERP, v, value);
        SELF.set_pmc_keyed_int(idx, v);
    }
/*

=item C<void push_float(FLOATVAL value)>

=item C<void push_integer(INTVAL value)>

=item C<void push_pmc(PMC *value)>

=item C<void push_string(STRING *value)>

=cut

*/

    VTABLE void push_pmc(PMC *value) {
        const INTVAL n = SELF.elements();
        SELF.set_pmc_keyed_int(n, value);
    }

    VTABLE void push_float(FLOATVAL value) {
        const INTVAL n = SELF.elements();
        SELF.set_number_keyed_int(n, value);
    }

    VTABLE void push_integer(INTVAL value) {
        const INTVAL n = SELF.elements();
        SELF.set_integer_keyed_int(n, value);
    }

    VTABLE void push_string(STRING *value) {
        const INTVAL n = SELF.elements();
        SELF.set_string_keyed_int(n, value);
    }

/*

=item C<INTVAL exists_keyed(PMC *key)>

=item C<INTVAL exists_keyed_str(STRING *key)>

=item C<INTVAL exists_keyed_int(INTVAL key)>

=cut

*/

    VTABLE INTVAL exists_keyed_int(INTVAL idx) {
        return (idx >= 0) && (idx < STATICSELF.elements());
    }

    VTABLE INTVAL exists_keyed(PMC *key) {
        if ((PObj_get_FLAGS(key) & KEY_type_FLAGS) == KEY_integer_FLAG) {
            /* Don't fetch item prematurely. It's costy */
            const INTVAL intval = VTABLE_get_integer(INTERP, key);
            PMC * const next = VTABLE_shift_pmc(INTERP, key);

            if (!next)
                return STATICSELF.exists_keyed_int(intval);

            return VTABLE_exists_keyed(INTERP, STATICSELF.get_pmc_keyed_int(intval), next);
        }

        return VTABLE_exists_keyed_str(INTERP, SELF, VTABLE_get_string(INTERP, key));
    }

    VTABLE INTVAL exists_keyed_str(STRING *key) {
        return VTABLE_exists_keyed_str(INTERP, PARROT_ORDEREDHASH(SELF)->hash, key);
    }

/*

=item C<INTVAL defined_keyed(PMC *key)>

=item C<INTVAL defined_keyed_str(STRING *key)>

=item C<INTVAL defined_keyed_int(INTVAL key)>

=cut

*/

    VTABLE INTVAL defined_keyed(PMC *key) {
        /* We store list_item (which is defined). So fetch original PMC and check it */
        PMC * const item = STATICSELF.get_pmc_keyed(key);
        if (PMC_IS_NULL(item))
            return 0;
        return VTABLE_defined(INTERP, item);
    }

    VTABLE INTVAL defined_keyed_str(STRING *key) {
        PMC * const item = STATICSELF.get_pmc_keyed_str(key);
        if (PMC_IS_NULL(item))
            return 0;
        return VTABLE_defined(INTERP, item);
    }

    VTABLE INTVAL defined_keyed_int(INTVAL idx) {
        PMC * item;
        /* If item doesn't exists it's undefined */
        if (!STATICSELF.exists_keyed_int(idx))
            return 0;

        /* Null is undefined */
        item = STATICSELF.get_pmc_keyed_int(idx);
        if (PMC_IS_NULL(item))
            return 0;

        return VTABLE_defined(INTERP, item);
    }

/*

=item C<void delete_keyed(PMC *key)>

=item C<void delete_keyed_str(STRING *key)>

=item C<void delete_keyed_int(INTVAL key)>

Deletes the key C<*key> from the hash.

=cut

*/

    VTABLE void delete_keyed(PMC *key) {
        Parrot_OrderedHash_attributes *attrs =
                PARROT_ORDEREDHASH(SELF);
        PMC *list_entry, *prev, *next;
        if ((PObj_get_FLAGS(key) & KEY_type_FLAGS) == KEY_integer_FLAG) {
            const INTVAL intval = VTABLE_get_integer(INTERP, key);
            PMC * const next = VTABLE_shift_pmc(INTERP, key);

            if (next)
                VTABLE_delete_keyed(INTERP, STATICSELF.get_pmc_keyed_int(intval), next);
            else
                STATICSELF.delete_keyed_int(intval);

            return;
        }

        list_entry = VTABLE_get_pmc_keyed(INTERP, attrs->hash, key);
        if (PMC_IS_NULL(list_entry))
            return;

        /* Unlink entry */
        next = VTABLE_get_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_NEXT);
        prev = VTABLE_get_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_PREV);

        if (attrs->first == list_entry)
            attrs->first = next;

        if (attrs->last == list_entry)
            attrs->last = prev;

        /* C<prev> and C<next> are list entries (FPA) */
        if (!PMC_IS_NULL(prev))
            VTABLE_set_pmc_keyed_int(INTERP, prev, ORDERED_HASH_ITEM_NEXT, next);
        if (!PMC_IS_NULL(next))
            VTABLE_set_pmc_keyed_int(INTERP, next, ORDERED_HASH_ITEM_PREV, prev);

        /* And finally delete it */
        VTABLE_delete_keyed(INTERP, PARROT_ORDEREDHASH(SELF)->hash, key);
    }

    VTABLE void delete_keyed_str(STRING *key) {
        STATICSELF.delete_keyed(box_string(INTERP, key));
    }

    VTABLE void delete_keyed_int(INTVAL idx) {
        if (STATICSELF.exists_keyed_int(idx)) {
            PMC * const list_entry = get_list_item(INTERP, SELF, idx);
            STATICSELF.delete_keyed(
                    VTABLE_get_pmc_keyed_int(INTERP, list_entry, ORDERED_HASH_ITEM_KEY));
        }
        return;
    }

/*

=item C<PMC *clone()>

Create a clone of the OrderedHash. Non-existent keys are compacted. Accessing
the clone via integers has different indices, if items were deleted.

=cut

*/

    VTABLE PMC *clone() {
        PMC * const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
        Parrot_OrderedHash_attributes *clone_attrs =
                PARROT_ORDEREDHASH(dest);

        clone_attrs->hash = VTABLE_clone(INTERP, PARROT_ORDEREDHASH(SELF)->hash);
        find_bounds(INTERP, clone_attrs->hash, &clone_attrs->first, &clone_attrs->last);

        return dest;
    }
/*

=item C<void visit(PMC *info)>

Used during archiving to visit the elements in the hash.

=item C<void freeze(PMC *info)>

Used to archive the hash.

=item C<void thaw(PMC *info)>

Used to unarchive the hash.

=cut

*/

    VTABLE void visit(PMC *info) {
        VISIT_PMC_ATTR(INTERP, info, SELF, OrderedHash, hash);
        SUPER(info);
    }

    VTABLE void thawfinish(PMC *info) {
        Parrot_OrderedHash_attributes *attrs = PARROT_ORDEREDHASH(SELF);
        find_bounds(INTERP, attrs->hash, &attrs->first, &attrs->last);
        SUPER(info);
    }

/*

=item C<get_pmc()>

Get underlying regular Hash. Used in UnManagedStruct.

=cut

*/

    VTABLE PMC* get_pmc() {
        PMC *hash;
        GET_ATTR_hash(INTERP, SELF, hash);
        return hash;
    }
}

/*

=back

=head1 SEE ALSO

F<docs/pdds/pdd08_keys.pod>.

=cut

*/

/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
Something went wrong with that request. Please try again.