Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (97 sloc) 2.275 kb
7d7d3e8 Mark Rada Change ownership to The MacRuby Team and update copyrights
ferrous26 authored
1 /*
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
2 * MacRuby Hash.
3 *
7d7d3e8 Mark Rada Change ownership to The MacRuby Team and update copyrights
ferrous26 authored
4 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
9595725 update copyrights to 2011
Laurent Sansonetti authored
5 * Copyright (C) 2011, Apple Inc. All rights reserved.
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
6 */
7
8 #ifndef __HASH_H_
9 #define __HASH_H_
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
548a941 Watson fix the struct declarations
Watson1978 authored
15
16 typedef struct RHash {
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
17 struct RBasic basic;
18 st_table *tbl;
19 VALUE ifnone;
20 bool has_proc_default;
21 } rb_hash_t;
22
23 #define RHASH(x) ((rb_hash_t *)x)
24
25 static inline bool
26 rb_klass_is_rhash(VALUE klass)
27 {
28 do {
29 if (klass == rb_cRubyHash) {
30 return true;
31 }
32 if (klass == rb_cNSHash) {
33 return false;
34 }
35 klass = RCLASS_SUPER(klass);
36 }
37 while (klass != 0);
38 return false;
39 }
40
41 #define IS_RHASH(x) (rb_klass_is_rhash(*(VALUE *)x))
42
43 static inline void
44 rhash_modify(VALUE hash)
45 {
7280ac3 better rewrite r5186
Laurent Sansonetti authored
46 const long mask = RBASIC(hash)->flags;
47 if ((mask & FL_FREEZE) == FL_FREEZE) {
48 rb_raise(rb_eRuntimeError, "can't modify frozen/immutable hash");
49 }
f39c792 r{ary,str,hash}_modify: check for the unstrust flag properly
Laurent Sansonetti authored
50 if ((mask & FL_UNTRUSTED) != FL_UNTRUSTED) {
7280ac3 better rewrite r5186
Laurent Sansonetti authored
51 if (rb_safe_level() >= 4) {
52 rb_raise(rb_eSecurityError, "Insecure: can't modify hash");
53 }
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
54 }
55 }
56
57 static inline long
58 rhash_len(VALUE hash)
59 {
60 return RHASH(hash)->tbl->num_entries;
61 }
62
63 static inline void
64 rhash_foreach(VALUE hash, int (*func)(ANYARGS), VALUE farg)
65 {
66 st_foreach_safe(RHASH(hash)->tbl, func, (st_data_t)farg);
67 }
68
69 static inline VALUE
70 rhash_lookup(VALUE hash, VALUE key)
71 {
72 VALUE val;
73 if (st_lookup(RHASH(hash)->tbl, key, &val)) {
74 return val;
75 }
76 return Qundef;
77 }
78
79 static inline VALUE
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
80 rhash_store(VALUE hash, VALUE key, VALUE val)
81 {
82 rhash_modify(hash);
83 if (TYPE(key) == T_STRING) {
84 key = rb_str_dup(key);
85 OBJ_FREEZE(key);
86 }
87 st_insert(RHASH(hash)->tbl, key, val);
88 return val;
89 }
90
91 static inline VALUE
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
92 rhash_delete_key(VALUE hash, VALUE key)
93 {
94 VALUE val;
95 if (st_delete(RHASH(hash)->tbl, &key, &val)) {
96 return val;
97 }
98 return Qundef;
99 }
100
101 VALUE rhash_dup(VALUE rcv, SEL sel);
102 VALUE rhash_aref(VALUE hash, SEL sel, VALUE key);
103 VALUE rhash_aset(VALUE hash, SEL sel, VALUE key, VALUE val);
104 VALUE rhash_keys(VALUE hash, SEL sel);
105 VALUE rhash_has_key(VALUE hash, SEL sel, VALUE key);
106 VALUE rhash_set_default(VALUE hash, SEL sel, VALUE ifnone);
107
01ec3c8 implement a better RubyHash hashing function for arrays, as the CF one i...
Laurent Sansonetti authored
108 unsigned long rb_hash_code(VALUE obj);
109
ae05921 completed Array refactoring, cleaned out a few things
Laurent Sansonetti authored
110 #if defined(__cplusplus)
111 } // extern "C"
112 #endif
113
114 #endif // __HASH_H_
Something went wrong with that request. Please try again.