~ubuntu-branches/ubuntu/raring/parrot/raring-proposed

« back to all changes in this revision

Viewing changes to src/pmc/hash.pmc

  • Committer: Bazaar Package Importer
  • Author(s): Allison Randal
  • Date: 2011-07-30 18:45:03 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110730184503-34d4mprtfx6pt5h3
Tags: 3.6.0-1
* New upstream release
* debian/watch:
  - Modified regular expression to capture numbered directory name
    (patch from Dominique Dumont).
* debian/rules:
  - Split build-arch and build-indep, resolving lintian warning.
  - Update path to pbc_disassemble for manpage generation (patch
    from Dominique Dumont).
* debian/patches:
  - Added patch 02_fix_perl_interpreter_path.patch, resolving
    lintian warnings.
* debian/control:
  - Added DM-Upload-Allowed field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
C<set_value_type> may be used to switch key and values type. For C<PMC> keys
34
34
hash value is calculated using VTABLE C<get_hashvalue> function.
35
35
 
36
 
These are the vtable functions for the Hash PMC.
37
 
 
38
 
=head2 Functions
39
 
 
40
 
=over 4
41
 
 
42
36
=cut
43
37
 
44
38
*/
79
73
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
80
74
/* HEADERIZER END: static */
81
75
 
82
 
/*
83
 
 
84
 
=item C<static PMC* get_next_hash(PARROT_INTERP, const Hash *hash, const void
85
 
*key)>
86
 
 
87
 
Get the next hash for multipart keys. Autovivify a hash if it doesn't exist.
88
 
 
89
 
=cut
90
 
 
91
 
*/
92
 
 
93
 
PARROT_CANNOT_RETURN_NULL
94
 
PARROT_WARN_UNUSED_RESULT
95
 
static PMC*
96
 
get_next_hash(PARROT_INTERP, ARGIN(const Hash *hash), ARGIN(const void *key))
97
 
{
98
 
    ASSERT_ARGS(get_next_hash)
99
 
    HashBucket *bucket;
100
 
 
101
 
    if (hash->entry_type != enum_type_PMC)
102
 
        entry_type_must_be_pmc(interp);
103
 
 
104
 
    bucket = Parrot_hash_get_bucket(interp, hash, key);
105
 
 
106
 
    if (bucket)
107
 
        return (PMC *)bucket->value;
108
 
    else
109
 
        return PMCNULL;
110
 
}
111
 
 
112
76
pmclass Hash provides hash auto_attrs {
113
77
    ATTR Hash *hash;
114
78
 
115
79
/*
116
80
 
 
81
=head2 Vtable functions
 
82
 
 
83
=over 4
 
84
 
117
85
=item C<void init()>
118
86
 
119
87
Initializes the instance.
264
232
            Parrot_hash_destroy(INTERP, old_hash);
265
233
    }
266
234
 
267
 
    METHOD set_key_type(INTVAL type) {
268
 
        SELF.set_integer_native(type);
269
 
    }
270
 
 
271
 
/*
272
 
 
273
 
=item C<METHOD get_key_type()>
274
 
 
275
 
Return type of keys in Hash.
276
 
 
277
 
=cut
278
 
 
279
 
*/
280
 
    METHOD get_key_type() {
281
 
        const INTVAL ret = ((Hash *)SELF.get_pointer())->key_type;
282
 
        RETURN(INTVAL ret);
283
 
    }
284
 
 
285
 
/*
286
 
 
287
 
=item C<METHOD set_value_type(INTVAL type)>
288
 
 
289
 
Reset Hash to use different value-type for stored items. If there is no
290
 
previous _hash was set defaults to STRING* keys.
291
 
 
292
 
NB: this method will destroy all old data!
293
 
 
294
 
=cut
295
 
*/
296
 
    METHOD set_value_type(INTVAL type) {
297
 
        Hash * const old_hash = (Hash *)SELF.get_pointer();
298
 
        Hash *new_hash;
299
 
 
300
 
        /*
301
 
        If someone called Hash.set_pointer with NULL pointer...
302
 
        It will create STRING* keys hash.
303
 
        */
304
 
 
305
 
        if (old_hash && old_hash->entry_type == type)
306
 
            return;
307
 
 
308
 
        switch (type) {
309
 
          case enum_type_INTVAL:
310
 
          case enum_type_STRING:
311
 
          case enum_type_PMC:
312
 
            new_hash = Parrot_hash_create(INTERP,
313
 
                        (PARROT_DATA_TYPE)type,
314
 
                        old_hash ? old_hash->key_type : Hash_key_type_STRING);
315
 
            break;
316
 
          default:
317
 
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
318
 
                        "Hash: unsupported entry_type %d", type);
319
 
        }
320
 
 
321
 
        PARROT_HASH(SELF)->hash = new_hash;
322
 
 
323
 
        if (old_hash)
324
 
            Parrot_hash_destroy(INTERP, old_hash);
325
 
    }
326
 
 
327
 
    METHOD get_value_type() {
328
 
        const INTVAL ret = ((Hash *)SELF.get_pointer())->entry_type;
329
 
        RETURN(INTVAL ret);
330
 
    }
331
 
 
332
235
/*
333
236
 
334
237
=item C<void *get_pointer()>
1204
1107
        /* Thawed Hash was created from pmc_new_noinit */
1205
1108
        PObj_custom_mark_destroy_SETALL(SELF);
1206
1109
    }
 
1110
 
 
1111
/*
 
1112
 
 
1113
=back
 
1114
 
 
1115
=head1 Methods
 
1116
 
 
1117
=over 4
 
1118
 
 
1119
=item C<METHOD set_key_type(INTVAL)>
 
1120
 
 
1121
Set the type of key in the hash.
 
1122
 
 
1123
=item C<METHOD get_key_type()>
 
1124
 
 
1125
Return type of keys in Hash.
 
1126
 
 
1127
=item C<METHOD set_value_type(INTVAL type)>
 
1128
 
 
1129
Reset Hash to use different value-type for stored items. If there is no
 
1130
previous _hash was set defaults to STRING* keys.
 
1131
 
 
1132
NB: this method will destroy all old data!
 
1133
 
 
1134
=item C<METHOD get_value_type()>
 
1135
 
 
1136
Return type of values in Hash.
 
1137
 
 
1138
=cut
 
1139
 
 
1140
*/
 
1141
 
 
1142
    METHOD set_key_type(INTVAL type) {
 
1143
        SELF.set_integer_native(type);
 
1144
    }
 
1145
 
 
1146
    METHOD get_key_type() {
 
1147
        const INTVAL ret = ((Hash *)SELF.get_pointer())->key_type;
 
1148
        RETURN(INTVAL ret);
 
1149
    }
 
1150
 
 
1151
    METHOD set_value_type(INTVAL type) {
 
1152
        Hash * const old_hash = (Hash *)SELF.get_pointer();
 
1153
        Hash *new_hash;
 
1154
 
 
1155
        /*
 
1156
        If someone called Hash.set_pointer with NULL pointer...
 
1157
        It will create STRING* keys hash.
 
1158
        */
 
1159
 
 
1160
        if (old_hash && old_hash->entry_type == (PARROT_DATA_TYPE)type)
 
1161
            return;
 
1162
 
 
1163
        switch (type) {
 
1164
          case enum_type_INTVAL:
 
1165
          case enum_type_STRING:
 
1166
          case enum_type_PMC:
 
1167
            new_hash = Parrot_hash_create(INTERP,
 
1168
                        (PARROT_DATA_TYPE)type,
 
1169
                        old_hash ? old_hash->key_type : Hash_key_type_STRING);
 
1170
            break;
 
1171
          default:
 
1172
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNIMPLEMENTED,
 
1173
                        "Hash: unsupported entry_type %d", type);
 
1174
        }
 
1175
 
 
1176
        PARROT_HASH(SELF)->hash = new_hash;
 
1177
 
 
1178
        if (old_hash)
 
1179
            Parrot_hash_destroy(INTERP, old_hash);
 
1180
    }
 
1181
 
 
1182
    METHOD get_value_type() {
 
1183
        const INTVAL ret = ((Hash *)SELF.get_pointer())->entry_type;
 
1184
        RETURN(INTVAL ret);
 
1185
    }
 
1186
 
1207
1187
}
1208
1188
 
1209
1189
/*
1214
1194
 
1215
1195
=over 4
1216
1196
 
 
1197
=item C<static PMC* get_next_hash(PARROT_INTERP, const Hash *hash, const void
 
1198
*key)>
 
1199
 
 
1200
Get the next hash for multipart keys. Autovivify a hash if it doesn't exist.
 
1201
 
 
1202
=cut
 
1203
 
 
1204
*/
 
1205
 
 
1206
PARROT_CANNOT_RETURN_NULL
 
1207
PARROT_WARN_UNUSED_RESULT
 
1208
static PMC*
 
1209
get_next_hash(PARROT_INTERP, ARGIN(const Hash *hash), ARGIN(const void *key))
 
1210
{
 
1211
    ASSERT_ARGS(get_next_hash)
 
1212
    HashBucket *bucket;
 
1213
 
 
1214
    if (hash->entry_type != enum_type_PMC)
 
1215
        entry_type_must_be_pmc(interp);
 
1216
 
 
1217
    bucket = Parrot_hash_get_bucket(interp, hash, key);
 
1218
 
 
1219
    if (bucket)
 
1220
        return (PMC *)bucket->value;
 
1221
    else
 
1222
        return PMCNULL;
 
1223
}
 
1224
 
 
1225
/*
 
1226
 
1217
1227
=item C<static void entry_type_must_be_pmc(PARROT_INTERP)>
1218
1228
 
1219
1229
=item C<static void cannot_autovivify_nested(PARROT_INTERP)>
1220
1230
 
 
1231
Throw the appropiate exception.
 
1232
 
 
1233
=cut
 
1234
 
1221
1235
*/
1222
1236
 
1223
1237
PARROT_DOES_NOT_RETURN