From d45a39ba5b98f6eef21a183fb735320c2ca20dac Mon Sep 17 00:00:00 2001 From: luben Date: Sun, 15 Aug 2010 01:38:18 +0300 Subject: [PATCH 4/7] get rid of parrot_hash_get_idx : very ugly crap replace with hash iteraton macros --- include/parrot/hash.h | 19 -------------- src/call/args.c | 22 ++++----------- src/hash.c | 67 ------------------------------------------------- src/pmc/null.pmc | 2 - 4 files changed, 6 insertions(+), 104 deletions(-) diff --git a/include/parrot/hash.h b/include/parrot/hash.h index c23afa6..e8eff3d 100644 --- a/include/parrot/hash.h +++ b/include/parrot/hash.h @@ -19,10 +19,6 @@ typedef enum { } HashEntryType; -/* A BucketIndex is an index into the pool of available buckets. */ -typedef UINTVAL BucketIndex; -#define INITBucketIndex ((BucketIndex)-2) - #define N_BUCKETS(n) ((n)) #define HASH_ALLOC_SIZE(n) (N_BUCKETS(n) * sizeof (HashBucket) + \ (n) * sizeof (HashBucket *)) @@ -191,17 +187,6 @@ HashBucket * parrot_hash_get_bucket(PARROT_INTERP, __attribute__nonnull__(2); PARROT_EXPORT -PARROT_WARN_UNUSED_RESULT -PARROT_CAN_RETURN_NULL -void * parrot_hash_get_idx(PARROT_INTERP, - ARGIN(const Hash *hash), - ARGMOD(PMC *key)) - __attribute__nonnull__(1) - __attribute__nonnull__(2) - __attribute__nonnull__(3) - FUNC_MODIFIES(*key); - -PARROT_EXPORT PARROT_IGNORABLE_RESULT PARROT_CANNOT_RETURN_NULL HashBucket* parrot_hash_put(PARROT_INTERP, @@ -469,10 +454,6 @@ int STRING_compare_distinct_cs_enc(PARROT_INTERP, #define ASSERT_ARGS_parrot_hash_get_bucket __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(interp) \ , PARROT_ASSERT_ARG(hash)) -#define ASSERT_ARGS_parrot_hash_get_idx __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ - PARROT_ASSERT_ARG(interp) \ - , PARROT_ASSERT_ARG(hash) \ - , PARROT_ASSERT_ARG(key)) #define ASSERT_ARGS_parrot_hash_put __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(interp) \ , PARROT_ASSERT_ARG(hash)) diff --git a/src/call/args.c b/src/call/args.c index d432caa..c87ea8f 100644 --- a/src/call/args.c +++ b/src/call/args.c @@ -488,22 +488,12 @@ dissect_aggregate_arg(PARROT_INTERP, ARGMOD(PMC *call_object), ARGIN(PMC *aggreg } } else if (VTABLE_does(interp, aggregate, CONST_STRING(interp, "hash"))) { - const INTVAL elements = VTABLE_elements(interp, aggregate); - INTVAL index; - PMC * const key = Parrot_pmc_new(interp, enum_class_Key); - VTABLE_set_integer_native(interp, key, 0); - SETATTR_Key_next_key(interp, key, (PMC *)INITBucketIndex); - - /* Low-level hash iteration. */ - for (index = 0; index < elements; ++index) { - if (!PMC_IS_NULL(key)) { - STRING * const name = (STRING *)parrot_hash_get_idx(interp, - (Hash *)VTABLE_get_pointer(interp, aggregate), key); - PARROT_ASSERT(name); - VTABLE_set_pmc_keyed_str(interp, call_object, name, - VTABLE_get_pmc_keyed_str(interp, aggregate, name)); - } - } + Hash * hash = (Hash *)VTABLE_get_pointer(interp, aggregate), key; + parrot_hash_iterate(hash, + PARROT_ASSERT(_bucket->key); + VTABLE_set_pmc_keyed_str(interp,call_object,_bucket->key, + VTABLE_get_pmc_keyed_str(interp, aggregate, _bucket->key)); + ); } else { Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION, diff --git a/src/hash.c b/src/hash.c index f5b73f5..2ff52c4 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1152,73 +1152,6 @@ parrot_hash_size(SHIM_INTERP, ARGIN(const Hash *hash)) return hash->entries; } - -/* - -=item C - -Finds the next index into the hash's internal storage for the given Key. Used -by iterators. Ugly. - -=cut - -*/ - -PARROT_EXPORT -PARROT_WARN_UNUSED_RESULT -PARROT_CAN_RETURN_NULL -void * -parrot_hash_get_idx(PARROT_INTERP, ARGIN(const Hash *hash), ARGMOD(PMC *key)) -{ - ASSERT_ARGS(parrot_hash_get_idx) - HashBucket *b; - void *res; - INTVAL i = VTABLE_get_integer(interp, key); - PMC *fake_bi; - BucketIndex bi; - - /* idx directly in the bucket store, which is at negative - * address from the data pointer */ - /* locate initial */ - const INTVAL size = (INTVAL)N_BUCKETS(hash->mask + 1); - - GETATTR_Key_next_key(interp, key, fake_bi); - bi = (BucketIndex)fake_bi; - - if (bi == INITBucketIndex) { - i = 0; - SETATTR_Key_next_key(interp, key, NULL); - } - else if (i >= size || i < 0) { - /* NOTE: These instances of SETATTR_Key_int_key can't be VTABLE - * functions because of the "special" way hash iterators work. */ - SETATTR_Key_int_key(interp, key, -1); - return NULL; - } - - res = NULL; - - for (b = hash->buckets + i; i < size ; ++i, ++b) { - /* XXX int keys may be zero - use different iterator */ - if (b->key) { - if (!res) - res = b->key; - - /* found next key - FIXME hash iter does auto next */ - else - break; - } - } - - if (i >= size) - i = -1; - - SETATTR_Key_int_key(interp, key, i); - - return res; -} - - /* =item C - - =cut */ -- 1.7.1