Author: chromatic
Date: Fri Apr 27 12:49:28 2007
New Revision: 18346
Modified:
trunk/src/pmc/orderedhash.pmc
Log:
[PMC] Minor coding style cleanups to OrderedHash before File of the Week review.
Modified: trunk/src/pmc/orderedhash.pmc
==============================================================================
--- trunk/src/pmc/orderedhash.pmc (original)
+++ trunk/src/pmc/orderedhash.pmc Fri Apr 27 12:49:28 2007
@@ -1,5 +1,5 @@
/*
-Copyright (C) 2001-2003, The Perl Foundation.
+Copyright (C) 2001-2007, The Perl Foundation.
$Id$
=head1 NAME
@@ -8,23 +8,22 @@
=head1 DESCRIPTION
-C<OrderedHash> extends C<Hash> to provide the interfaces of
-C<array> and C<hash>. To achieve the functionality of an ordered
-hash there are a few restrictions though: C<delete_keyed> never removes
-items, they are just nulled.
-
-Please note that if values are set via integer idx, these indices
-have to be in strict order. Using C<push_xx> simplifies this task.
-This creates a key "\1idx" for C<idx> and is therefore not fully transparent.
+C<OrderedHash> extends C<Hash> to provide the interfaces of C<array> and
+C<hash>. To achieve ordering, hash there are a few restrictions:
+C<delete_keyed> never removes items; they are just nulled.
+
+Please note that if values are set via integer idx, these indices have to be in
+strict order. Using C<push_xx> simplifies this task. This creates a key
+"\1idx" for C<idx> and is therefore not fully transparent.
-There are 2 iterator interfaces:
+There are two iterator interfaces:
=over 4
=item * retrieve values (in creation order)
Please note that after a C<delete_keyed> operation, iterating over
-values doesn't work any more, you'll get an error 'No such key'.
+values doesn't work any more. You'll get an error 'No such key'.
=item * retrieve keys (in creation order)
@@ -43,7 +42,6 @@
#include "parrot/parrot.h"
#include "pmc_hash.h"
-
pmclass OrderedHash extends Hash need_ext does array does hash {
/*
@@ -59,36 +57,45 @@
*/
PMC* get_pmc_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: index out of bounds!");
- }
+
b = h->bs + idx;
+
if (b->key)
- return (PMC*) b->value;
+ return (PMC *)b->value;
+
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: No such key");
+
return PMCNULL;
}
- PMC* get_pmc_keyed(PMC* key) {
+ PMC* get_pmc_keyed(PMC *key) {
PMC *item, *next;
switch (PObj_get_FLAGS(key) & KEY_type_FLAGS) {
case KEY_hash_iterator_FLAGS:
return SUPER(key);
+
case KEY_integer_FLAG:
item = SELF.get_pmc_keyed_int(key_integer(INTERP, key));
next = key_next(INTERP, key);
+
if (!next)
return item;
+
return VTABLE_get_pmc_keyed(INTERP, item, next);
+
default:
return SUPER(key);
}
@@ -107,26 +114,31 @@
*/
STRING* get_string_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: index out of bounds!");
- }
+
b = h->bs + idx;
+
if (b->key)
- return VTABLE_get_string(INTERP, (PMC*) b->value);
+ return VTABLE_get_string(INTERP, (PMC *)b->value);
+
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: No such key");
+
return NULL;
}
- STRING* get_string_keyed(PMC* key) {
+ STRING* get_string_keyed(PMC *key) {
PMC *item, *next;
switch (PObj_get_FLAGS(key) & KEY_type_FLAGS) {
case KEY_hash_iterator_FLAGS:
@@ -155,38 +167,47 @@
*/
-
INTVAL get_integer_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
real_exception(INTERP, NULL, OUT_OF_BOUNDS,
"OrderedHash: index out of bounds!");
- }
+
b = h->bs + idx;
+
if (b->key)
- return VTABLE_get_integer(INTERP, (PMC*) b->value);
+ return VTABLE_get_integer(INTERP, (PMC *)b->value);
+
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: No such key");
+
return 0;
}
- INTVAL get_integer_keyed(PMC* key) {
+ INTVAL get_integer_keyed(PMC *key) {
PMC *item, *next;
+
switch (PObj_get_FLAGS(key) & KEY_type_FLAGS) {
case KEY_hash_iterator_FLAGS:
return SUPER(key);
+
case KEY_integer_FLAG:
item = SELF.get_pmc_keyed_int(key_integer(INTERP, key));
next = key_next(INTERP, key);
+
if (!next)
return VTABLE_get_integer(INTERP, item);
+
return VTABLE_get_integer_keyed(INTERP, item, next);
+
default:
return SUPER(key);
}
@@ -207,36 +228,46 @@
*/
FLOATVAL get_number_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
real_exception(INTERP, NULL, OUT_OF_BOUNDS,
"OrderedHash: index out of bounds!");
- }
+
b = h->bs + idx;
+
if (b->key)
- return VTABLE_get_number(INTERP, (PMC*) b->value);
+ return VTABLE_get_number(INTERP, (PMC *)b->value);
+
real_exception(INTERP, NULL, KEY_NOT_FOUND,
"OrderedHash: No such key");
+
return 0.0;
}
- FLOATVAL get_number_keyed(PMC* key) {
+ FLOATVAL get_number_keyed(PMC *key) {
PMC *item, *next;
+
switch (PObj_get_FLAGS(key) & KEY_type_FLAGS) {
case KEY_hash_iterator_FLAGS:
return SUPER(key);
+
case KEY_integer_FLAG:
item = SELF.get_pmc_keyed_int(key_integer(INTERP, key));
next = key_next(INTERP, key);
+
if (!next)
return VTABLE_get_number(INTERP, item);
+
return VTABLE_get_number_keyed(INTERP, item, next);
+
default:
return SUPER(key);
}
@@ -259,18 +290,21 @@
*/
- void set_pmc_keyed_int(INTVAL idx, PMC* val) {
- STRING *key, *fmt;
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ void set_pmc_keyed_int(INTVAL idx, PMC *val) {
+ STRING *key, *fmt;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < -n)
idx = -idx - n - 1;
else if (idx < 0)
idx += n;
+
fmt = CONST_STRING(INTERP, "\1%d");
+
if (idx >= n) {
/* TODO warn or fill if there are holes */
key = Parrot_sprintf_s(INTERP, fmt, idx);
@@ -278,9 +312,10 @@
}
else {
b = h->bs + idx;
- if (!b->key) {
+
+ if (!b->key)
b->key = Parrot_sprintf_s(INTERP, fmt, idx);
- }
+
b->value = val;
}
}
@@ -311,7 +346,7 @@
=item C<void push_integer(INTVAL value)>
-=item C<void push_pmc(PMC* value)>
+=item C<void push_pmc(PMC *value)>
=item C<void push_string(STRING* value)>
@@ -319,7 +354,7 @@
*/
- void push_pmc(PMC* value) {
+ void push_pmc(PMC *value) {
INTVAL n = DYNSELF.elements();
SELF.set_pmc_keyed_int(n, value);
}
@@ -334,7 +369,7 @@
SELF.set_integer_keyed_int(n, value);
}
- void push_string(STRING* value) {
+ void push_string(STRING *value) {
INTVAL n = DYNSELF.elements();
SELF.set_string_keyed_int(n, value);
}
@@ -352,43 +387,54 @@
*/
INTVAL exists_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
return 0;
- }
+
b = h->bs + idx;
+
if (b->key)
return 1;
+
return 0;
}
- INTVAL exists_keyed(PMC* key) {
+ INTVAL exists_keyed(PMC *key) {
PMC *item, *next;
+
if (PObj_get_FLAGS(key) & KEY_integer_FLAG) {
Hash *h = PMC_struct_val(SELF);
INTVAL n, idx;
HashBucket *b;
idx = key_integer(INTERP, key);
- n = h->entries;
+ n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
return 0;
- }
+
b = h->bs + idx;
+
if (!b->key)
return 0;
+
item = b->value;
next = key_next(INTERP, key);
+
if (!next)
return 1;
+
return VTABLE_exists_keyed(INTERP, item, next);
}
else {
@@ -397,11 +443,12 @@
}
INTVAL exists_keyed_str(STRING* key) {
- Hash *h = PMC_struct_val(SELF);
- HashBucket *b;
- b = parrot_hash_get_bucket(INTERP, h, key);
+ Hash *h = PMC_struct_val(SELF);
+ HashBucket *b = parrot_hash_get_bucket(INTERP, h, key);
+
if (b && b->key)
return 1;
+
return 0;
}
@@ -417,48 +464,60 @@
*/
-
INTVAL defined_keyed(PMC *key) {
PMC *item, *next;
+
if (PObj_get_FLAGS(key) & KEY_integer_FLAG) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n, idx;
+ INTVAL n, idx;
+ Hash *h;
HashBucket *b;
+ h = PMC_struct_val(SELF);
idx = key_integer(INTERP, key);
- n = h->entries;
+ n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
- /* XXX non-existant is undefined - is this correct */
+
+ /* XXX non-existant is undefined - is this correct */
+ if (idx < 0 || idx >= n)
return 0;
- }
+
b = h->bs + idx;
+
if (!b->key)
return 0;
+
item = b->value;
next = key_next(INTERP, key);
+
if (!next)
return VTABLE_defined(INTERP, item);
+
return VTABLE_defined_keyed(INTERP, item, next);
}
+
return SUPER(key);
}
INTVAL defined_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
return 0;
- }
+
b = h->bs + idx;
+
if (b->key)
- return VTABLE_defined(INTERP, (PMC*)b->value);
+ return VTABLE_defined(INTERP, (PMC *)b->value);
+
return 0;
}
@@ -476,15 +535,17 @@
*/
- void delete_keyed(PMC* key) {
- PMC *item, *next;
- next = key_next(INTERP, key);
+ void delete_keyed(PMC *key) {
+ PMC *item;
+ PMC *next = key_next(INTERP, key);
+
if (PObj_get_FLAGS(key) & KEY_integer_FLAG) {
if (next) {
item = SELF.get_pmc_keyed_int(key_integer(INTERP, key));
VTABLE_delete_keyed(INTERP, item, next);
return;
}
+
SELF.delete_keyed_int(key_integer(INTERP, key));
}
else {
@@ -498,25 +559,28 @@
}
void delete_keyed_int(INTVAL idx) {
- Hash *h = PMC_struct_val(SELF);
- INTVAL n;
+ INTVAL n;
+ Hash *h = PMC_struct_val(SELF);
HashBucket *b;
n = h->entries;
+
if (idx < 0)
idx += n;
- if (idx < 0 || idx >= n) {
+
+ if (idx < 0 || idx >= n)
return;
- }
+
b = h->bs + idx;
+
if (b)
b->key = NULL;
}
- void delete_keyed_str(STRING* key) {
- Hash *h = PMC_struct_val(SELF);
- HashBucket *b;
- b = parrot_hash_get_bucket(INTERP, h, key);
+ void delete_keyed_str(STRING *key) {
+ Hash *h = PMC_struct_val(SELF);
+ HashBucket *b = parrot_hash_get_bucket(INTERP, h, key);
+
if (b)
b->key = NULL;
}
@@ -525,35 +589,39 @@
=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
+Create a clone of the OrderedHash. Non-existent keys are compacted. Accessing
+the clone via integers has different indices, if items were deleted.
=cut
*/
PMC* clone() {
- PMC *dest;
- Hash *hash = PMC_struct_val(SELF), *h_dest;
-
- UINTVAL i;
- HashBucket *b;
- void *key;
- void *valtmp;
+ PMC *dest;
+ Hash *hash = PMC_struct_val(SELF);
+ Hash *h_dest;
+ HashBucket *b;
+ UINTVAL i;
+ void *key;
+ void *valtmp;
dest = pmc_new_noinit(INTERP, SELF->vtable->base_type);
parrot_new_pmc_hash_x(INTERP, dest, hash->entry_type,
hash->key_type, hash->compare, hash->hash_val);
+
h_dest = PMC_struct_val(dest);
+
for (i = 0; i <= hash->mask; i++) {
- b = hash->bs + i;
+ b = hash->bs + i;
key = b->key;
+
if (!key)
continue;
- valtmp = VTABLE_clone(INTERP, (PMC*)b->value);
+
+ valtmp = VTABLE_clone(INTERP, (PMC *)b->value);
parrot_hash_put(INTERP, h_dest, key, valtmp);
}
+
return dest;
}
/*
@@ -570,33 +638,37 @@
Used to unarchive the hash.
-Freeze/thaw are inherited from hash, onyl thaw.visit is special, as
-we have to preserver key/value order.
+Freeze/thaw are inherited from hash. Only thaw.visit is special, as we have to
+preserve key/value order.
=cut
*/
void visit(visit_info *info) {
- UINTVAL i;
+ UINTVAL i;
HashBucket *b;
- void *key;
- IMAGE_IO *io = info->image_io;
- Hash *hash = (Hash *)PMC_struct_val(SELF);
+ void *key;
+ IMAGE_IO *io = info->image_io;
+ Hash *hash = (Hash *)PMC_struct_val(SELF);
+
+ info->container = SELF;
- info->container = SELF;
switch (info->what) {
case VISIT_THAW_NORMAL:
case VISIT_THAW_CONSTANTS:
SUPER(info);
break;
+
case VISIT_FREEZE_NORMAL:
case VISIT_FREEZE_AT_DESTRUCT:
for (i = 0; i <= hash->mask; i++) {
- b = hash->bs + i;
+ b = hash->bs + i;
key = b->key;
+
if (!key)
continue;
+
io->vtable->push_string(interp, io, key);
(info->visit_pmc_now)(interp, b->value, info);
}
@@ -607,7 +679,6 @@
info->what);
}
}
-
}
/*
@@ -626,7 +697,6 @@
*/
-
/*
* Local variables:
* c-file-style: "parrot"