Debian Codegraph @

include/ruby/internal/intern/hash.h

raw
1 #ifndef RBIMPL_INTERN_HASH_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_HASH_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to ::rb_cHash.
22 */
23 #include "ruby/internal/attr/nonnull.h"
24 #include "ruby/internal/dllexport.h"
25 #include "ruby/internal/value.h"
26 #include "ruby/st.h"
27
28 RBIMPL_SYMBOL_EXPORT_BEGIN()
29
30 /* hash.c */
31
32 RBIMPL_ATTR_NONNULL(())
33 /**
34 * Identical to rb_st_foreach(), except it raises exceptions when the callback
35 * function tampers the table during iterating over it.
36 *
37 * @param[in] st Table to iterate over.
38 * @param[in] func Callback function to apply.
39 * @param[in] arg Passed as-is to `func`.
40 * @exception rb_eRuntimeError `st` was tampered during iterating.
41 *
42 * @internal
43 *
44 * This is declared here because exceptions are Ruby level concept.
45 *
46 * This is in fact a very thin wrapper of rb_st_foreach_check().
47 */
48 void rb_st_foreach_safe(struct st_table *st, st_foreach_callback_func *func, st_data_t arg);
49
50 /** @alias{rb_st_foreach_safe} */
51 #define st_foreach_safe rb_st_foreach_safe
52
53 /**
54 * Try converting an object to its hash representation using its `to_hash`
55 * method, if any. If there is no such thing, returns ::RUBY_Qnil.
56 *
57 * @param[in] obj Arbitrary ruby object to convert.
58 * @exception rb_eTypeError `obj.to_hash` returned something non-Hash.
59 * @retval RUBY_Qnil No conversion from `obj` to hash defined.
60 * @retval otherwise Converted hash representation of `obj`.
61 * @see rb_io_check_io
62 * @see rb_check_array_type
63 * @see rb_check_string_type
64 *
65 * @internal
66 *
67 * There is no rb_hash_to_hash() that analogous to rb_str_to_str().
68 * Intentional or ...?
69 */
70 VALUE rb_check_hash_type(VALUE obj);
71
72 RBIMPL_ATTR_NONNULL(())
73 /**
74 * Iterates over a hash. This basically does the same thing as
75 * rb_st_foreach(). But because the passed hash is a Ruby object, its keys and
76 * values are both Ruby objects.
77 *
78 * @param[in] hash An instance of ::rb_cHash to iterate over.
79 * @param[in] func Callback function to yield.
80 * @param[in] arg Passed as-is to `func`.
81 * @exception rb_eRuntimeError `hash` was tampered during iterating.
82 */
83 void rb_hash_foreach(VALUE hash, int (*func)(VALUE key, VALUE val, VALUE arg), VALUE arg);
84
85 /**
86 * Calculates a message authentication code of the passed object. The return
87 * value is a very small integer used as an index of a key of a table. In
88 * order to calculate the value this function calls `#hash` method of the
89 * passed object. Ruby provides you a default implementation. But if you
90 * implement your class in C, that default implementation cannot know the
91 * underlying data structure. You must implement your own `#hash` method then,
92 * which must return an integer of uniform distribution in a sufficiently
93 * instant manner.
94 *
95 * @param[in] obj Arbitrary Ruby object.
96 * @exception rb_eTypeError `obj.hash` returned something non-Integer.
97 * @return A small integer.
98 * @note `#hash` can return very big integers, but they get truncated.
99 */
100 VALUE rb_hash(VALUE obj);
101
102 /**
103 * Creates a new, empty hash object.
104 *
105 * @return An allocated new instance of ::rb_cHash.
106 */
107 VALUE rb_hash_new(void);
108
109 /**
110 * Identical to rb_hash_new(), except it additionally specifies how many keys
111 * it is expected to contain. This way you can create a hash that is large enough
112 * for your need. For large hashes it means it won't need to be reallocated and
113 * rehashed as much, improving performance.
114 *
115 * @param[in] capa Designed capacity of the hash.
116 * @return An empty Hash, whose capacity is `capa`.
117 */
118 VALUE rb_hash_new_capa(long capa);
119
120 /**
121 * Duplicates a hash.
122 *
123 * @param[in] hash An instance of ::rb_cHash.
124 * @return An allocated new instance of ::rb_cHash, whose contents are
125 * a verbatim copy of from `hash`.
126 */
127 VALUE rb_hash_dup(VALUE hash);
128
129 /** @alias{rb_obj_freeze} */
130 VALUE rb_hash_freeze(VALUE obj);
131
132 /**
133 * Queries the given key in the given hash table. If there is the key in the
134 * hash, returns the value associated with the key. Otherwise it returns the
135 * "default" value (defined per hash table).
136 *
137 * @param[in] hash Hash table to look into.
138 * @param[in] key Hash key to look for.
139 * @return Either the value associated with the key, or the default one if
140 * absent.
141 */
142 VALUE rb_hash_aref(VALUE hash, VALUE key);
143
144 /**
145 * Identical to rb_hash_aref(), except it always returns ::RUBY_Qnil for
146 * misshits.
147 *
148 * @param[in] hash Hash table to look into.
149 * @param[in] key Hash key to look for.
150 * @return Either the value associated with the key, or ::RUBY_Qnil if
151 * absent.
152 * @note A hash can store ::RUBY_Qnil as an ordinary value. You cannot
153 * distinguish whether the key is missing, or just its associated
154 * value happens to be ::RUBY_Qnil, as far as you use this API.
155 */
156 VALUE rb_hash_lookup(VALUE hash, VALUE key);
157
158 /**
159 * Identical to rb_hash_lookup(), except you can specify what to return on
160 * misshits. This is much like 2-arguments version of `Hash#fetch`.
161 *
162 * ```CXX
163 * VALUE hash;
164 * VALUE key;
165 * VALUE tmp = rb_obj_alloc(rb_cObject);
166 * VALUE val = rb_hash_lookup2(hash, key, tmp);
167 * if (val == tmp) {
168 * printf("misshit");
169 * }
170 * else {
171 * printf("hit");
172 * }
173 * ```
174 *
175 * @param[in] hash Hash table to look into.
176 * @param[in] key Hash key to look for.
177 * @param[in] def Default value.
178 * @retval def `hash` does not have `key`.
179 * @retval otherwise The value associated with `key`.
180 */
181 VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def);
182
183 /**
184 * Identical to rb_hash_lookup(), except it yields the (implicitly) passed
185 * block instead of returning ::RUBY_Qnil.
186 *
187 * @param[in] hash Hash table to look into.
188 * @param[in] key Hash key to look for.
189 * @exception rb_eKeyError No block given.
190 * @return Either the value associated with the key, or what the block
191 * evaluates to if absent.
192 */
193 VALUE rb_hash_fetch(VALUE hash, VALUE key);
194
195 /**
196 * Inserts or replaces ("upsert"s) the objects into the given hash table. This
197 * basically associates the given value with the given key. On duplicate key
198 * this function updates its associated value with the given one. Otherwise it
199 * inserts the association at the end of the table.
200 *
201 * @param[out] hash Target hash table to modify.
202 * @param[in] key Arbitrary Ruby object.
203 * @param[in] val A value to be associated with `key`.
204 * @exception rb_eFrozenError `hash` is frozen.
205 * @return The passed `val`
206 * @post `val` is associated with `key` in `hash`.
207 */
208 VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val);
209
210 /**
211 * Swipes everything out of the passed hash table.
212 *
213 * @param[out] hash Target to clear.
214 * @exception rb_eFrozenError `hash`is frozen.
215 * @return The passed `hash`
216 * @post `hash` has no contents.
217 */
218 VALUE rb_hash_clear(VALUE hash);
219
220 /**
221 * Deletes each entry for which the block returns a truthy value. If there is
222 * no block given, it returns an enumerator that does the thing.
223 *
224 * @param[out] hash Target hash to modify.
225 * @exception rb_eFrozenError `hash` is frozen.
226 * @retval hash The hash is modified.
227 * @retval otherwise An instance of ::rb_cEnumerator that does it.
228 */
229 VALUE rb_hash_delete_if(VALUE hash);
230
231 /**
232 * Deletes the passed key from the passed hash table, if any.
233 *
234 * @param[out] hash Target hash to modify.
235 * @param[in] key Key to delete.
236 * @retval RUBY_Qnil `hash` has no such key as `key`.
237 * @retval otherwise What was associated with `key`.
238 * @post `hash` has no such key as `key`.
239 */
240 VALUE rb_hash_delete(VALUE hash, VALUE key);
241
242 /**
243 * Inserts a list of key-value pairs into a hash table at once. It is
244 * semantically identical to repeatedly calling rb_hash_aset(), but can be
245 * faster than that.
246 *
247 * @param[in] argc Length of `argv`, must be even.
248 * @param[in] argv A list of key, value, key, value, ...
249 * @param[out] hash Target hash table to modify.
250 * @post `hash` has contents from `argv`.
251 * @note `argv` is allowed to be NULL as long as `argc` is zero.
252 *
253 * @internal
254 *
255 * What happens for duplicated keys? Well it silently discards older ones to
256 * accept the newest (rightmost) one. This behaviour also mimics repeated call
257 * of rb_hash_aset().
258 */
259 void rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash);
260
261 /**
262 * Type of callback functions to pass to rb_hash_update_by().
263 *
264 * @param[in] newkey A key of the table.
265 * @param[in] oldkey Value associated with `key` in hash1.
266 * @param[in] value Value associated with `key` in hash2.
267 * @return Either one of the passed values to take.
268 */
269 typedef VALUE rb_hash_update_func(VALUE newkey, VALUE oldkey, VALUE value);
270
271 /**
272 * Destructively merges two hash tables into one. It resolves key conflicts by
273 * calling the passed function and take its return value.
274 *
275 * @param[out] hash1 Target hash to be modified.
276 * @param[in] hash2 A hash to merge into `hash1`.
277 * @param[in] func Conflict reconciler.
278 * @exception rb_eFrozenError `hash1` is frozen.
279 * @exception rb_eRuntimeError `hash2` is updated instead.
280 * @return The passed `hash1`.
281 * @post Contents of `hash2` is merged into `hash1`.
282 * @note You can pass zero to `func`. This means values from `hash2`
283 * are always taken.
284 */
285 VALUE rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func);
286
287 /* file.c */
288
289 /**
290 * This function is mysterious. What it does is not immediately obvious. Also
291 * what it does seems platform dependent.
292 *
293 * @param[in] path A local path.
294 * @retval 0 The "check" succeeded.
295 * @retval otherwise The "check" failed.
296 */
297 int rb_path_check(const char *path);
298
299 /* hash.c */
300
301 /**
302 * Destructively removes every environment variables of the running process.
303 *
304 * @return The `ENV` object.
305 * @post The process has no environment variables.
306 */
307 VALUE rb_env_clear(void);
308
309 /**
310 * Identical to #RHASH_SIZE(), except it returns the size in Ruby's integer
311 * instead of C's.
312 *
313 * @param[in] hash A hash object.
314 * @return The size of the hash.
315 */
316 VALUE rb_hash_size(VALUE hash);
317
318 RBIMPL_SYMBOL_EXPORT_END()
319
320 #endif /* RBIMPL_INTERN_HASH_H */