Skip to content

Commit

Permalink
rearrange a bit and fix pod in Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
NotFound committed Jun 25, 2011
1 parent 2410627 commit cd9fe1b
Showing 1 changed file with 110 additions and 101 deletions.
211 changes: 110 additions & 101 deletions src/pmc/hash.pmc
Expand Up @@ -33,12 +33,6 @@ By default Hash uses string keys and PMC values. Methods C<set_key_type> and
C<set_value_type> may be used to switch key and values type. For C<PMC> keys
hash value is calculated using VTABLE C<get_hashvalue> function.

These are the vtable functions for the Hash PMC.

=head2 Functions

=over 4

=cut

*/
Expand Down Expand Up @@ -79,41 +73,15 @@ static PMC* get_next_hash(PARROT_INTERP,
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */

/*

=item C<static PMC* get_next_hash(PARROT_INTERP, const Hash *hash, const void
*key)>

Get the next hash for multipart keys. Autovivify a hash if it doesn't exist.

=cut

*/

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC*
get_next_hash(PARROT_INTERP, ARGIN(const Hash *hash), ARGIN(const void *key))
{
ASSERT_ARGS(get_next_hash)
HashBucket *bucket;

if (hash->entry_type != enum_type_PMC)
entry_type_must_be_pmc(interp);

bucket = Parrot_hash_get_bucket(interp, hash, key);

if (bucket)
return (PMC *)bucket->value;
else
return PMCNULL;
}

pmclass Hash provides hash auto_attrs {
ATTR Hash *hash;

/*

=head2 Vtable functions

=over 4

=item C<void init()>

Initializes the instance.
Expand Down Expand Up @@ -264,71 +232,6 @@ NB: this method will destroy all old data!
Parrot_hash_destroy(INTERP, old_hash);
}

METHOD set_key_type(INTVAL type) {
SELF.set_integer_native(type);
}

/*

=item C<METHOD get_key_type()>

Return type of keys in Hash.

=cut

*/
METHOD get_key_type() {
const INTVAL ret = ((Hash *)SELF.get_pointer())->key_type;
RETURN(INTVAL ret);
}

/*

=item C<METHOD set_value_type(INTVAL type)>

Reset Hash to use different value-type for stored items. If there is no
previous _hash was set defaults to STRING* keys.

NB: this method will destroy all old data!

=cut
*/
METHOD set_value_type(INTVAL type) {
Hash * const old_hash = (Hash *)SELF.get_pointer();
Hash *new_hash;

/*
If someone called Hash.set_pointer with NULL pointer...
It will create STRING* keys hash.
*/

if (old_hash && old_hash->entry_type == (Hash_key_type)type)
return;

switch (type) {
case enum_type_INTVAL:
case enum_type_STRING:
case enum_type_PMC:
new_hash = Parrot_hash_create(INTERP,
(PARROT_DATA_TYPE)type,
old_hash ? old_hash->key_type : Hash_key_type_STRING);
break;
default:
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
"Hash: unsupported entry_type %d", type);
}

PARROT_HASH(SELF)->hash = new_hash;

if (old_hash)
Parrot_hash_destroy(INTERP, old_hash);
}

METHOD get_value_type() {
const INTVAL ret = ((Hash *)SELF.get_pointer())->entry_type;
RETURN(INTVAL ret);
}

/*

=item C<void *get_pointer()>
Expand Down Expand Up @@ -1204,6 +1107,78 @@ Used to unarchive the hash.
/* Thawed Hash was created from pmc_new_noinit */
PObj_custom_mark_destroy_SETALL(SELF);
}
METHOD set_key_type(INTVAL type) {
SELF.set_integer_native(type);
}

/*

=back

=head1 Methods

=over 4

=item C<METHOD get_key_type()>

Return type of keys in Hash.

=item C<METHOD set_value_type(INTVAL type)>

Reset Hash to use different value-type for stored items. If there is no
previous _hash was set defaults to STRING* keys.

NB: this method will destroy all old data!

=item C<METHOD get_value_type()>

Return type of values in Hash.

=cut

*/

METHOD get_key_type() {
const INTVAL ret = ((Hash *)SELF.get_pointer())->key_type;
RETURN(INTVAL ret);
}

METHOD set_value_type(INTVAL type) {
Hash * const old_hash = (Hash *)SELF.get_pointer();
Hash *new_hash;

/*
If someone called Hash.set_pointer with NULL pointer...
It will create STRING* keys hash.
*/

if (old_hash && old_hash->entry_type == (Hash_key_type)type)
return;

switch (type) {
case enum_type_INTVAL:
case enum_type_STRING:
case enum_type_PMC:
new_hash = Parrot_hash_create(INTERP,
(PARROT_DATA_TYPE)type,
old_hash ? old_hash->key_type : Hash_key_type_STRING);
break;
default:
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
"Hash: unsupported entry_type %d", type);
}

PARROT_HASH(SELF)->hash = new_hash;

if (old_hash)
Parrot_hash_destroy(INTERP, old_hash);
}

METHOD get_value_type() {
const INTVAL ret = ((Hash *)SELF.get_pointer())->entry_type;
RETURN(INTVAL ret);
}

}

/*
Expand All @@ -1214,10 +1189,44 @@ Used to unarchive the hash.

=over 4

=item C<static PMC* get_next_hash(PARROT_INTERP, const Hash *hash, const void
*key)>

Get the next hash for multipart keys. Autovivify a hash if it doesn't exist.

=cut

*/

PARROT_CANNOT_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
static PMC*
get_next_hash(PARROT_INTERP, ARGIN(const Hash *hash), ARGIN(const void *key))
{
ASSERT_ARGS(get_next_hash)
HashBucket *bucket;

if (hash->entry_type != enum_type_PMC)
entry_type_must_be_pmc(interp);

bucket = Parrot_hash_get_bucket(interp, hash, key);

if (bucket)
return (PMC *)bucket->value;
else
return PMCNULL;
}

/*

=item C<static void entry_type_must_be_pmc(PARROT_INTERP)>

=item C<static void cannot_autovivify_nested(PARROT_INTERP)>

Throw the appropiate exception.

=cut

*/

PARROT_DOES_NOT_RETURN
Expand Down

0 comments on commit cd9fe1b

Please sign in to comment.