| 1 |
/** |
| 2 |
** @file mruby/hash.h - Hash class |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef MRUBY_HASH_H |
| 8 |
#define MRUBY_HASH_H |
| 9 |
|
| 10 |
#include "common.h" |
| 11 |
|
| 12 |
/** |
| 13 |
* Hash class |
| 14 |
*/ |
| 15 |
MRB_BEGIN_DECL |
| 16 |
|
| 17 |
/* offset of `iv` must be 3 words */ |
| 18 |
struct RHash { |
| 19 |
MRB_OBJECT_HEADER; |
| 20 |
#ifdef MRB_64BIT |
| 21 |
uint32_t size; |
| 22 |
struct iv_tbl *iv; |
| 23 |
uint32_t ea_capa; |
| 24 |
uint32_t ea_n_used; |
| 25 |
#else |
| 26 |
struct iv_tbl *iv; |
| 27 |
uint32_t size; |
| 28 |
#endif |
| 29 |
union { |
| 30 |
struct hash_entry *ea; |
| 31 |
struct hash_table *ht; |
| 32 |
} hsh; |
| 33 |
}; |
| 34 |
|
| 35 |
#define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v))) |
| 36 |
#define mrb_hash_value(p) mrb_obj_value((void*)(p)) |
| 37 |
|
| 38 |
MRB_API mrb_value mrb_hash_new_capa(mrb_state *mrb, mrb_int capa); |
| 39 |
|
| 40 |
/* |
| 41 |
* Initializes a new hash. |
| 42 |
* |
| 43 |
* Equivalent to: |
| 44 |
* |
| 45 |
* Hash.new |
| 46 |
* |
| 47 |
* @param mrb The mruby state reference. |
| 48 |
* @return The initialized hash. |
| 49 |
*/ |
| 50 |
MRB_API mrb_value mrb_hash_new(mrb_state *mrb); |
| 51 |
|
| 52 |
/* |
| 53 |
* Sets a keys and values to hashes. |
| 54 |
* |
| 55 |
* Equivalent to: |
| 56 |
* |
| 57 |
* hash[key] = val |
| 58 |
* |
| 59 |
* @param mrb The mruby state reference. |
| 60 |
* @param hash The target hash. |
| 61 |
* @param key The key to set. |
| 62 |
* @param val The value to set. |
| 63 |
* @return The value. |
| 64 |
*/ |
| 65 |
MRB_API void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val); |
| 66 |
|
| 67 |
/* |
| 68 |
* Gets a value from a key. If the key is not found, the default of the |
| 69 |
* hash is used. |
| 70 |
* |
| 71 |
* Equivalent to: |
| 72 |
* |
| 73 |
* hash[key] |
| 74 |
* |
| 75 |
* @param mrb The mruby state reference. |
| 76 |
* @param hash The target hash. |
| 77 |
* @param key The key to get. |
| 78 |
* @return The found value. |
| 79 |
*/ |
| 80 |
MRB_API mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key); |
| 81 |
|
| 82 |
/* |
| 83 |
* Gets a value from a key. If the key is not found, the default parameter is |
| 84 |
* used. |
| 85 |
* |
| 86 |
* Equivalent to: |
| 87 |
* |
| 88 |
* hash.key?(key) ? hash[key] : def |
| 89 |
* |
| 90 |
* @param mrb The mruby state reference. |
| 91 |
* @param hash The target hash. |
| 92 |
* @param key The key to get. |
| 93 |
* @param def The default value. |
| 94 |
* @return The found value. |
| 95 |
*/ |
| 96 |
MRB_API mrb_value mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def); |
| 97 |
|
| 98 |
/* |
| 99 |
* Deletes hash key and value pair. |
| 100 |
* |
| 101 |
* Equivalent to: |
| 102 |
* |
| 103 |
* hash.delete(key) |
| 104 |
* |
| 105 |
* @param mrb The mruby state reference. |
| 106 |
* @param hash The target hash. |
| 107 |
* @param key The key to delete. |
| 108 |
* @return The deleted value. This value is not protected from GC. Use `mrb_gc_protect()` if necessary. |
| 109 |
*/ |
| 110 |
MRB_API mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key); |
| 111 |
|
| 112 |
/* |
| 113 |
* Gets an array of keys. |
| 114 |
* |
| 115 |
* Equivalent to: |
| 116 |
* |
| 117 |
* hash.keys |
| 118 |
* |
| 119 |
* @param mrb The mruby state reference. |
| 120 |
* @param hash The target hash. |
| 121 |
* @return An array with the keys of the hash. |
| 122 |
*/ |
| 123 |
MRB_API mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash); |
| 124 |
/* |
| 125 |
* Check if the hash has the key. |
| 126 |
* |
| 127 |
* Equivalent to: |
| 128 |
* |
| 129 |
* hash.key?(key) |
| 130 |
* |
| 131 |
* @param mrb The mruby state reference. |
| 132 |
* @param hash The target hash. |
| 133 |
* @param key The key to check existence. |
| 134 |
* @return True if the hash has the key |
| 135 |
*/ |
| 136 |
MRB_API mrb_bool mrb_hash_key_p(mrb_state *mrb, mrb_value hash, mrb_value key); |
| 137 |
|
| 138 |
/* |
| 139 |
* Check if the hash is empty |
| 140 |
* |
| 141 |
* Equivalent to: |
| 142 |
* |
| 143 |
* hash.empty? |
| 144 |
* |
| 145 |
* @param mrb The mruby state reference. |
| 146 |
* @param self The target hash. |
| 147 |
* @return True if the hash is empty, false otherwise. |
| 148 |
*/ |
| 149 |
MRB_API mrb_bool mrb_hash_empty_p(mrb_state *mrb, mrb_value self); |
| 150 |
|
| 151 |
/* |
| 152 |
* Gets an array of values. |
| 153 |
* |
| 154 |
* Equivalent to: |
| 155 |
* |
| 156 |
* hash.values |
| 157 |
* |
| 158 |
* @param mrb The mruby state reference. |
| 159 |
* @param hash The target hash. |
| 160 |
* @return An array with the values of the hash. |
| 161 |
*/ |
| 162 |
MRB_API mrb_value mrb_hash_values(mrb_state *mrb, mrb_value hash); |
| 163 |
|
| 164 |
/* |
| 165 |
* Clears the hash. |
| 166 |
* |
| 167 |
* Equivalent to: |
| 168 |
* |
| 169 |
* hash.clear |
| 170 |
* |
| 171 |
* @param mrb The mruby state reference. |
| 172 |
* @param hash The target hash. |
| 173 |
* @return The hash |
| 174 |
*/ |
| 175 |
MRB_API mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash); |
| 176 |
|
| 177 |
/* |
| 178 |
* Get hash size. |
| 179 |
* |
| 180 |
* Equivalent to: |
| 181 |
* |
| 182 |
* hash.size |
| 183 |
* |
| 184 |
* @param mrb The mruby state reference. |
| 185 |
* @param hash The target hash. |
| 186 |
* @return The hash size. |
| 187 |
*/ |
| 188 |
MRB_API mrb_int mrb_hash_size(mrb_state *mrb, mrb_value hash); |
| 189 |
|
| 190 |
/* |
| 191 |
* Copies the hash. This function does NOT copy the instance variables |
| 192 |
* (except for the default value). Use mrb_obj_dup() to copy the instance |
| 193 |
* variables as well. |
| 194 |
* |
| 195 |
* @param mrb The mruby state reference. |
| 196 |
* @param hash The target hash. |
| 197 |
* @return The copy of the hash |
| 198 |
*/ |
| 199 |
MRB_API mrb_value mrb_hash_dup(mrb_state *mrb, mrb_value hash); |
| 200 |
|
| 201 |
/* |
| 202 |
* Merges two hashes. The first hash will be modified by the |
| 203 |
* second hash. |
| 204 |
* |
| 205 |
* @param mrb The mruby state reference. |
| 206 |
* @param hash1 The target hash. |
| 207 |
* @param hash2 Updating hash |
| 208 |
*/ |
| 209 |
MRB_API void mrb_hash_merge(mrb_state *mrb, mrb_value hash1, mrb_value hash2); |
| 210 |
|
| 211 |
#define RHASH(hash) ((struct RHash*)(mrb_ptr(hash))) |
| 212 |
|
| 213 |
#define MRB_HASH_IB_BIT_BIT 5 |
| 214 |
#define MRB_HASH_AR_EA_CAPA_BIT 5 |
| 215 |
#define MRB_HASH_IB_BIT_SHIFT 0 |
| 216 |
#define MRB_HASH_AR_EA_CAPA_SHIFT 0 |
| 217 |
#define MRB_HASH_AR_EA_N_USED_SHIFT MRB_HASH_AR_EA_CAPA_BIT |
| 218 |
#define MRB_HASH_SIZE_FLAGS_SHIFT (MRB_HASH_AR_EA_CAPA_BIT * 2) |
| 219 |
#define MRB_HASH_IB_BIT_MASK ((1 << MRB_HASH_IB_BIT_BIT) - 1) |
| 220 |
#define MRB_HASH_AR_EA_CAPA_MASK ((1 << MRB_HASH_AR_EA_CAPA_BIT) - 1) |
| 221 |
#define MRB_HASH_AR_EA_N_USED_MASK (MRB_HASH_AR_EA_CAPA_MASK << MRB_HASH_AR_EA_N_USED_SHIFT) |
| 222 |
#define MRB_HASH_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 0)) |
| 223 |
#define MRB_HASH_PROC_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 1)) |
| 224 |
#define MRB_HASH_HT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 2)) |
| 225 |
#define MRB_RHASH_DEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_DEFAULT) |
| 226 |
#define MRB_RHASH_PROCDEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_PROC_DEFAULT) |
| 227 |
|
| 228 |
/* return non zero to break the loop */ |
| 229 |
typedef int (mrb_hash_foreach_func)(mrb_state *mrb, mrb_value key, mrb_value val, void *data); |
| 230 |
MRB_API void mrb_hash_foreach(mrb_state *mrb, struct RHash *hash, mrb_hash_foreach_func *func, void *p); |
| 231 |
|
| 232 |
MRB_END_DECL |
| 233 |
|
| 234 |
#endif /* MRUBY_HASH_H */ |