/src/wrappers/glib/partially-implemented/g_hash_table.e

http://github.com/tybor/Liberty · Specman e · 517 lines · 169 code · 109 blank · 239 comment · 5 complexity · 2188b1c9988478f8eaf1abaf909c8be6 MD5 · raw file

  1. indexing
  2. description: "Hash Tables associations between keys and values so that given a key the value can be found quickly."
  3. copyright: "(C) 2005 Paolo Redaelli "
  4. license: "LGPL v2 or later"
  5. date: "$Date:$"
  6. revision: "$REvision:$"
  7. class G_HASH_TABLE [VALUE->SHARED_C_STRUCT, KEY->COMPARABLE_SHARED_C_STRUCT]
  8. -- A G_HASH_TABLE provides associations between keys and values
  9. -- which is optimized so that given a key, the associated value can
  10. -- be found very quickly.
  11. -- TODO: provide non-fast features, i.e. features that compare
  12. -- keys by their content, not by their address.
  13. -- TODO: Eiffellize the following documentation.
  14. -- Note: the following note, valid for the manual memory management
  15. -- of the C world is not true anymore in an Eiffel program, if a
  16. -- caching wrapper factory is used... "Note that neither keys nor
  17. -- values are copied when inserted into the G_HASH_TABLE, so they
  18. -- must exist for the lifetime of the G_HASH_TABLE. This means that
  19. -- the use of static strings is OK, but temporary strings
  20. -- (i.e. those created in buffers and those returned by GTK+
  21. -- widgets) should be copied with g_strdup() before being
  22. -- inserted".
  23. -- If keys or values are dynamically allocated, you must be careful
  24. -- to ensure that they are freed when they are removed from the
  25. -- G_HASH_TABLE, and also when they are overwritten by new
  26. -- insertions into the G_HASH_TABLE. It is also not advisable to
  27. -- mix static strings and dynamically-allocated strings in a
  28. -- GHashTable, because it then becomes difficult to determine
  29. -- whether the string should be freed.
  30. -- To create a GHashTable, use g_hash_table_new().
  31. -- To insert a key and value into a GHashTable, use
  32. -- g_hash_table_insert().
  33. -- To lookup a value corresponding to a given key, use
  34. -- g_hash_table_lookup() and g_hash_table_lookup_extended().
  35. -- To remove a key and value, use g_hash_table_remove().
  36. -- To call a function for each key and value pair use
  37. -- g_hash_table_foreach().
  38. -- To destroy a GHashTable use g_hash_table_destroy().
  39. inherit
  40. WRAPPER_DICTIONARY [VALUE, KEY]
  41. redefine
  42. dispose
  43. end
  44. insert
  45. G_HASH_TABLE_EXTERNALS
  46. creation from_external
  47. feature {} -- Creation
  48. from_external (a_pointer: POINTER; a_factory: WRAPPER_FACTORY[VALUE]) is
  49. require factory_not_void: a_factory/=Void
  50. do
  51. factory := a_factory
  52. from_external_pointer(a_pointer)
  53. end
  54. with_factory (a_factory: WRAPPER_FACTORY[VALUE]) is
  55. require factory_not_void: a_factory/=Void
  56. do
  57. factory := a_factory
  58. from_external_pointer
  59. (g_hash_table_new
  60. (default_pointer, -- Using g_direct_hash as hash function;
  61. default_pointer, -- Direct comparison of address, like using g_direct_equal as key equal function but with no overhead
  62. ),a_factory)
  63. -- g_hash_table_new creates a new GHashTable.
  64. -- hash_func : a function to create a hash value from a
  65. -- key. Hash values are used to determine where keys are
  66. -- stored within the GHashTable data structure. The
  67. -- g_direct_hash(), g_int_hash() and g_str_hash() functions
  68. -- are provided for some common types of keys. If hash_func
  69. -- is NULL, g_direct_hash() is used.
  70. -- key_equal_func : a function to check two keys for
  71. -- equality. This is used when looking up keys in the
  72. -- GHashTable. The g_direct_equal(), g_int_equal() and
  73. -- g_str_equal() functions are provided for the most common
  74. -- types of keys. If key_equal_func is NULL, keys are
  75. -- compared directly in a similar fashion to
  76. -- g_direct_equal(), but without the overhead of a function
  77. -- call.
  78. end
  79. -- TODO: GHashTable* g_hash_table_new_full (GHashFunc hash_func,
  80. -- GEqualFunc key_equal_func, GDestroyNotify key_destroy_func,
  81. -- GDestroyNotify value_destroy_func);
  82. -- Creates a new GHashTable like g_hash_table_new() and allows to
  83. -- specify functions to free the memory allocated for the key and
  84. -- value that get called when removing the entry from the
  85. -- GHashTable.
  86. -- hash_func : a function to create a hash value from a key.
  87. -- key_equal_func : a function to check two keys for equality.
  88. -- key_destroy_func : a function to free the memory allocated for the key used when removing the entry from the GHashTable or NULL if you don't want to supply such a function.
  89. -- value_destroy_func : a function to free the memory allocated for the value used when removing the entry from the GHashTable or NULL if you don't want to supply such a function.
  90. -- Returns : a new GHashTable.
  91. -- TODO: feature {} -- Implement manifest generic creation:
  92. -- manifest_make (needed_capacity: INTEGER) is
  93. -- -- Manifest creation of a dictionary.
  94. -- do
  95. -- allocate
  96. -- end
  97. feature {ANY} -- Basic access:
  98. has (a_key: KEY): BOOLEAN is
  99. local orig_key_ptr, value_ptr: POINTER
  100. do
  101. Result:=(g_hash_table_lookup_extended
  102. (handle, a_key.handle, orig_key_ptr, value_ptr)).to_boolean
  103. end
  104. at (a_key: KEY): VALUE is
  105. -- Looks up `a_key' in a GHashTable.
  106. local ptr: POINTER
  107. do
  108. ptr := g_hash_table_lookup (handle, a_key.handle)
  109. if ptr.is_not_null then
  110. Result:=factory.wrapper(ptr)
  111. end
  112. end
  113. reference_at (a_key: KEY): VALUE is
  114. -- The value associated to `a_key'. Void if there is no value
  115. -- for `a_key'.
  116. local ptr: POINTER
  117. do
  118. ptr := g_hash_table_lookup (handle, a_key.handle)
  119. if ptr.is_not_null then
  120. Result:=factory.wrapper(ptr)
  121. end
  122. -- Looks up a key in the GHashTable, returning the original
  123. -- key and the associated value and a gboolean which is TRUE
  124. -- if the key was found. This is useful if you need to free
  125. -- the memory allocated for the original key, for example
  126. -- before calling `remove'.
  127. -- `a_key': the key to look up.
  128. -- orig_key : returns the original key.
  129. -- value : returns the value associated with the key.
  130. -- Returns : TRUE if the key was found in the GHashTable.
  131. -- gboolean g_hash_table_lookup_extended (GHashTable
  132. -- *hash_table, gconstpointer lookup_key, gpointer *orig_key,
  133. -- gpointer *value);
  134. end
  135. fast_has (k: KEY): BOOLEAN is
  136. do
  137. debug
  138. print_no_fast_notice
  139. print(fast_fallback_notice)
  140. end
  141. Result:=has(k)
  142. end
  143. fast_at (k: KEY): VALUE is
  144. do
  145. debug
  146. print_no_fast_notice
  147. print(fast_fallback_notice)
  148. end
  149. Result:=at(k)
  150. end
  151. fast_reference_at (k: KEY): VALUE is
  152. do
  153. debug
  154. print_no_fast_notice
  155. print(fast_fallback_notice)
  156. end
  157. Result:=reference_at(k)
  158. end
  159. feature {ANY}
  160. put (a_value: VALUE; a_key: KEY) is
  161. -- Inserts a new key and value into a GHashTable.
  162. -- If the key already exists in the GHashTable its current
  163. -- value is replaced with the new value. If you supplied a
  164. -- value_destroy_func when creating the GHashTable, the old
  165. -- value is freed using that function. If you supplied a
  166. -- key_destroy_func when creating the GHashTable, the passed
  167. -- key is freed using that function.
  168. -- hash_table : a GHashTable.
  169. -- key : a key to insert.
  170. -- value : the value to associate with the key.
  171. require else value_not_void: a_value /= Void
  172. do
  173. g_hash_table_insert (handle, a_key.handle, a_value.handle)
  174. end
  175. fast_put (v: VALUE; k: KEY) is
  176. require else value_not_void: v /= Void
  177. do
  178. debug
  179. print_no_fast_notice
  180. print(fast_fallback_notice)
  181. end
  182. put(v,k)
  183. end
  184. add (v: VALUE; k: KEY) is
  185. require else value_not_void: v /= Void
  186. do
  187. put(v,k)
  188. end
  189. feature {ANY} -- Removing:
  190. remove (a_key: KEY) is
  191. -- Removes a key and its associated value from a GHashTable.
  192. -- `is_successful' will be True if the key was found and
  193. -- removed from the GHashTable.
  194. do
  195. -- TODO: Translate/implement this C-ism: If the GHashTable
  196. -- was created using `make_full', the key and value are freed
  197. -- using the supplied destroy functions, otherwise you have
  198. -- to make sure that any dynamically allocated values are
  199. -- freed yourself.
  200. is_successful := (g_hash_table_remove (handle, a_key.handle)).to_boolean
  201. end
  202. fast_remove (k: KEY) is
  203. do
  204. debug
  205. print_no_fast_notice
  206. print(fast_fallback_notice)
  207. end
  208. remove(k)
  209. end
  210. clear_count is
  211. do
  212. g_hash_table_remove_all(handle)
  213. end
  214. clear_count_and_capacity is
  215. do
  216. dispose
  217. make
  218. end
  219. capacity: INTEGER is
  220. do
  221. Result:=g_hash_table_size(handle)
  222. end
  223. feature {ANY} -- To provide iterating facilities:
  224. item (index: INTEGER): VALUE is
  225. do
  226. ensure then implemented: False
  227. end
  228. key (index: INTEGER): KEY is
  229. do
  230. ensure then implemented: False
  231. end
  232. get_new_iterator_on_keys: ITERATOR[KEY] is
  233. do
  234. ensure then implemented: False
  235. end
  236. feature {ANY} -- Other features:
  237. internal_key (k: KEY): KEY is
  238. do
  239. ensure then implemented: False
  240. end
  241. -- GHashFunc ()
  242. -- guint (*GHashFunc) (gconstpointer key);
  243. -- Specifies the type of the hash function which is passed to
  244. -- g_hash_table_new() when a GHashTable is created.
  245. -- The function is passed a key and should return a guint hash
  246. -- value. The functions g_direct_hash(), g_int_hash() and
  247. -- g_str_hash() provide hash functions which can be used when the
  248. -- key is a gpointer, gint, and gchar* respectively.
  249. -- FIXME: Need more here. The hash values should be evenly
  250. -- distributed over a fairly large range? The modulus is taken with
  251. -- the hash table size (a prime number) to find the 'bucket' to
  252. -- place each key into. The function should also be very fast,
  253. -- since it is called for each key lookup. key : a key. Returns :
  254. -- the hash value corresponding to the key. GEqualFunc ()
  255. -- gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b);
  256. -- Specifies the type of a function used to test two values for
  257. -- equality. The function should return TRUE if both values are
  258. -- equal and FALSE otherwise.
  259. -- a : a value.
  260. -- b : a value to compare with.
  261. -- Returns : TRUE if a = b; FALSE otherwise.
  262. -- g_hash_table_insert ()
  263. feature
  264. count: INTEGER is
  265. -- the number of elements (key/value pairs) contained in the
  266. -- GHashTable.
  267. do
  268. Result := g_hash_table_size (handle)
  269. end
  270. -- g_hash_table_foreach ()
  271. -- void g_hash_table_foreach (GHashTable *hash_table, GHFunc func,
  272. -- gpointer user_data);
  273. -- Calls the given function for each of the key/value pairs in the
  274. -- GHashTable. The function is passed the key and value of each
  275. -- pair, and the given user_data parameter. The hash table may not
  276. -- be modified while iterating over it (you can't add/remove
  277. -- items). To remove all items matching a predicate, use
  278. -- g_hash_table_foreach_remove().
  279. -- hash_table : a GHashTable.
  280. -- func : the function to call for each key/value pair.
  281. -- user_data : user data to pass to the function.
  282. -- g_hash_table_find ()
  283. -- gpointer g_hash_table_find (GHashTable *hash_table, GHRFunc
  284. -- predicate, gpointer user_data);
  285. -- Calls the given function for key/value pairs in the GHashTable
  286. -- until predicate returns TRUE. The function is passed the key and
  287. -- value of each pair, and the given user_data parameter. The hash
  288. -- table may not be modified while iterating over it (you can't
  289. -- add/remove items).
  290. -- hash_table : a GHashTable.
  291. -- predicate : function to test the key/value pairs for a certain property.
  292. -- user_data : user data to pass to the function.
  293. -- Returns : The value of the first key/value pair is returned, for which func evaluates to TRUE. If no pair with the requested property is found, NULL is returned.
  294. -- GHFunc ()
  295. -- void (*GHFunc) (gpointer key, gpointer value, gpointer
  296. -- user_data);
  297. -- Specifies the type of the function passed to g_hash_table_foreach(). It is called with each key/value pair, together with the user_data parameter which is passed to g_hash_table_foreach().
  298. -- key : a key.
  299. -- value : the value corresponding to the key.
  300. -- user_data : user data passed to g_hash_table_foreach().
  301. is_successful: BOOLEAN
  302. -- g_hash_table_foreach_remove ()
  303. -- guint g_hash_table_foreach_remove (GHashTable *hash_table,
  304. -- GHRFunc func,
  305. -- gpointer user_data);
  306. -- Calls the given function for each key/value pair in the GHashTable. If the function returns TRUE, then the key/value pair is removed from the GHashTable. If you supplied key or value destroy functions when creating the GHashTable, they are used to free the memory allocated for the removed keys and values.
  307. -- hash_table : a GHashTable.
  308. -- func : the function to call for each key/value pair.
  309. -- user_data : user data to pass to the function.
  310. -- Returns : the number of key/value pairs removed.
  311. -- g_hash_table_foreach_steal ()
  312. -- guint g_hash_table_foreach_steal (GHashTable *hash_table,
  313. -- GHRFunc func,
  314. -- gpointer user_data);
  315. -- Calls the given function for each key/value pair in the GHashTable. If the function returns TRUE, then the key/value pair is removed from the GHashTable, but no key or value destroy functions are called.
  316. -- hash_table : a GHashTable.
  317. -- func : the function to call for each key/value pair.
  318. -- user_data : user data to pass to the function.
  319. -- Returns : the number of key/value pairs removed.
  320. -- GHRFunc ()
  321. -- gboolean (*GHRFunc) (gpointer key,
  322. -- gpointer value,
  323. -- gpointer user_data);
  324. -- Specifies the type of the function passed to g_hash_table_foreach_remove(). It is called with each key/value pair, together with the user_data parameter passed to g_hash_table_foreach_remove(). It should return TRUE if the key/value pair should be removed from the GHashTable.
  325. -- key : a key.
  326. -- value : the value associated with the key.
  327. -- user_data : user data passed to g_hash_table_remove().
  328. -- Returns : TRUE if the key/value pair should be removed from the GHashTable.
  329. dispose is
  330. -- Destroys the GHashTable. If keys and/or values are
  331. -- dynamically allocated, you should either free them first
  332. -- or create the GHashTable using g_hash_table_new_full(). In
  333. -- the latter case the destroy functions you supplied will be
  334. -- called on all keys and values before destroying the
  335. -- GHashTable.
  336. do
  337. g_hash_table_destroy (handle)
  338. end
  339. feature {}
  340. -- g_direct_equal ()
  341. -- Compares two gpointer arguments and returns TRUE if they
  342. -- are equal. It can be passed to `g_hash_table_new' as the
  343. -- key_equal_func parameter, when using pointers as keys in a
  344. -- GHashTable.
  345. -- g_direct_hash ()
  346. -- guint g_direct_hash (gconstpointer v);
  347. -- Converts a gpointer to a hash value. It can be passed to g_hash_table_new() as the hash_func parameter, when using pointers as keys in a GHashTable.
  348. -- v : a gpointer key
  349. -- Returns : a hash value corresponding to the key.
  350. -- g_int_equal ()
  351. -- gboolean g_int_equal (gconstpointer v1,
  352. -- gconstpointer v2);
  353. -- Compares the two gint values being pointed to and returns TRUE if they are equal. It can be passed to g_hash_table_new() as the key_equal_func parameter, when using pointers to integers as keys in a GHashTable.
  354. -- v1 : a pointer to a gint key.
  355. -- v2 : a pointer to a gint key to compare with v1.
  356. -- Returns : TRUE if the two keys match.
  357. -- g_int_hash ()
  358. -- guint g_int_hash (gconstpointer v);
  359. -- Converts a pointer to a gint to a hash value. It can be passed to g_hash_table_new() as the hash_func parameter, when using pointers to integers values as keys in a GHashTable.
  360. -- v : a pointer to a gint key
  361. -- Returns : a hash value corresponding to the key.
  362. -- g_str_equal ()
  363. -- gboolean g_str_equal (gconstpointer v1,
  364. -- gconstpointer v2);
  365. -- Compares two strings and returns TRUE if they are equal. It can be passed to g_hash_table_new() as the key_equal_func parameter, when using strings as keys in a GHashTable.
  366. -- v1 : a key.
  367. -- v2 : a key to compare with v1.
  368. -- Returns : TRUE if the two keys match.
  369. -- g_str_hash ()
  370. -- guint g_str_hash (gconstpointer v);
  371. -- Converts a string to a hash value. It can be passed to g_hash_table_new() as the hash_func parameter, when using strings as keys in a GHashTable.
  372. -- v : a string key.
  373. -- Returns : a hash value corresponding to the key.
  374. feature -- size
  375. struct_size: INTEGER is
  376. external "C inline use <glib.h>"
  377. alias "sizeof(GHashTable)"
  378. end
  379. feature {} -- Low level implementation
  380. print_no_fast_notice is
  381. once
  382. print(no_fast_notice)
  383. end
  384. no_fast_notice: STRING is
  385. "Original C GHashTable implementation does not offer functions equivalent to `fast_has,' `fast_at', `fast_reference_at', `fast_put' and `fast_remove'. An eventual implementation of those features would require to manipulate directly GHashTable data-structure, skipping the Glib abstraction. Paolo 2007-07-15%N"
  386. fast_fallback_notice: STRING is
  387. "Fast_[has|at|reference_at|put|remove] feature not available. Falling back to non-fast features.%N"
  388. -- steal (a_key: KEY): VALUE is
  389. -- Removes a key and its associated value from a GHashTable
  390. -- without calling the key and value destroy functions.
  391. -- `is_successful' is True if the key was found and removed
  392. -- from the GHashTable.
  393. -- require key_not_void: a_key /= Void
  394. -- do is_successful := (g_hash_table_steal (handle,
  395. -- a_key.handle)).to_boolean end
  396. -- replace (a_key: KEY; a_value: VALUE) is Inserts a new key and
  397. -- value into a GHashTable similar to g_hash_table_insert(). The
  398. -- difference is that if the key already exists in the
  399. -- GHashTable, it gets replaced by the new key. If you supplied
  400. -- a value_destroy_func when creating the GHashTable, the old
  401. -- value is freed using that function. If you supplied a
  402. -- key_destroy_func when creating the GHashTable, the old key is
  403. -- freed using that function. hash_table : a GHashTable. key :
  404. -- a key to insert. value : the value to associate with the
  405. -- key. require key_not_void: a_key /= Void value_not_void:
  406. -- a_value /= Void do g_hash_table_replace (handle,
  407. -- a_key.handle, a_value.handle) end
  408. end