eina_hash_02.c
//Compile with:
//gcc -g eina_hash_02.c -o eina_hash_02 `pkg-config --cflags --libs eina`
#include <stdio.h>
#include <string.h>
#include <Eina.h>
/*
* Eina Hash - Two more types of hash
*
* This example demonstrate two other types of hash in action - using
* eina_hash_stringshared_new and eina_hash_new.
*
* It indexes the phone numbers by Contact Full Name, so it's a hash with string
* keys, exactly the same as the other example.
*/
struct _Phone_Entry {
const char *name; // Full name.
const char *number; // Phone number.
};
typedef struct _Phone_Entry Phone_Entry;
static Phone_Entry _start_entries[] = {
{ "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
{ "Ludwig van Beethoven", "+12 34 567-89101" },
{ "Richard Georg Strauss", "+23 45 678-91012" },
{ "Heitor Villa-Lobos", "+34 56 789-10123" },
{ NULL, NULL }
};
static void
_phone_entry_free_cb(void *data)
{
free(data);
}
static void
_phone_book_stringshared_free_cb(void *data)
{
Phone_Entry *e = data;
free(e);
}
static Eina_Bool
_phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book,
const void *key, void *data, void *fdata)
{
Phone_Entry *e = data;
const char *name = e->name; // e->name == key
const char *number = e->number;
printf("%s: %s\n", name, number);
return EINA_TRUE;
}
static void
example_hash_stringshared(void)
{
Eina_Hash *phone_book = NULL;
int i;
// Create the hash as before
phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb);
// Add initial entries to our hash, using direct_add
for (i = 0; _start_entries[i].name != NULL; i++)
{
Phone_Entry *e = malloc(sizeof(Phone_Entry));
e->name = eina_stringshare_add(_start_entries[i].name);
e->number = eina_stringshare_add(_start_entries[i].number);
// Since we are storing the key (name) in our struct, we can use
// eina_hash_direct_add. It could be used in the previous example
// too, since each key is already stored in the _start_entries
// static array, but we started it with the default add function.
eina_hash_direct_add(phone_book, e->name, e);
}
// Iterate over the elements
printf("List of phones:\n");
eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL);
printf("\n");
eina_hash_free(phone_book);
}
static unsigned int
_phone_book_string_key_length(const char *key)
{
if (!key)
return 0;
return (int)strlen(key) + 1;
}
static int
_phone_book_string_key_cmp(const char *key1, int key1_length,
const char *key2, int key2_length)
{
return strcmp(key1, key2);
}
static void
example_hash_big(void)
{
Eina_Hash *phone_book = NULL;
int i;
const char *phone;
// Create the same hash as used in eina_hash_01.c, but
// use 1024 (2 ^ 10) buckets.
phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length),
EINA_KEY_CMP(_phone_book_string_key_cmp),
EINA_KEY_HASH(eina_hash_superfast),
_phone_entry_free_cb,
10);
for (i = 0; _start_entries[i].name != NULL; i++)
{
eina_hash_add(phone_book, _start_entries[i].name,
strdup(_start_entries[i].number));
}
// Look for a specific entry and get its phone number
phone = eina_hash_find(phone_book, "Heitor Villa-Lobos");
if (phone)
{
printf("Printing entry.\n");
printf("Name: Heitor Villa-Lobos\n");
printf("Number: %s\n\n", phone);
}
eina_hash_free(phone_book);
}
int
main(int argc, const char *argv[])
{
example_hash_stringshared();
example_hash_big();
}
Eina Utility library.
#define EINA_KEY_CMP(Function)
Definition eina_hash.h:310
struct _Eina_Hash Eina_Hash
Type for a generic hash table.
Definition eina_hash.h:288
void eina_hash_foreach(const Eina_Hash *hash, Eina_Hash_Foreach func, const void *fdata)
Call a function on every member stored in the hash table.
Definition eina_hash.c:1207
Eina_Bool eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
Add an entry to the given hash table without duplicating the string key.
Definition eina_hash.c:931
Eina_Hash * eina_hash_new(Eina_Key_Length key_length_cb, Eina_Key_Cmp key_cmp_cb, Eina_Key_Hash key_hash_cb, Eina_Free_Cb data_free_cb, int buckets_power_size)
Create a new hash table.
Definition eina_hash.c:723
Eina_Hash * eina_hash_stringshared_new(Eina_Free_Cb data_free_cb)
Create a new hash table optimized for stringshared values.
Definition eina_hash.c:830
#define EINA_KEY_LENGTH(Function)
Definition eina_hash.h:304
void eina_hash_free(Eina_Hash *hash)
Free the given hash table resources.
Definition eina_hash.c:850
void * eina_hash_find(const Eina_Hash *hash, const void *key)
Retrieve a specific entry in the given hash table.
Definition eina_hash.c:1053
#define EINA_KEY_HASH(Function)
Definition eina_hash.h:316
Eina_Bool eina_hash_add(Eina_Hash *hash, const void *key, const void *data)
Add an entry to the given hash table.
Definition eina_hash.c:913
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieve an instance of a string for use in a program.
Definition eina_stringshare.c:635
void eina_stringshare_del(Eina_Stringshare *str)
Note that the given string has lost an instance.
Definition eina_stringshare.c:577
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:299
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:287