/*1* Copyright (c) 2002, 1997 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2010 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "baselocl.h"3637struct hashentry {38struct hashentry **prev;39struct hashentry *next;40heim_object_t key;41heim_object_t value;42};4344struct heim_dict_data {45size_t size;46struct hashentry **tab;47};4849static void50dict_dealloc(void *ptr)51{52heim_dict_t dict = ptr;53struct hashentry **h, *g, *i;5455for (h = dict->tab; h < &dict->tab[dict->size]; ++h) {56for (g = h[0]; g; g = i) {57i = g->next;58heim_release(g->key);59heim_release(g->value);60free(g);61}62}63free(dict->tab);64}6566struct heim_type_data dict_object = {67HEIM_TID_DICT,68"dict-object",69NULL,70dict_dealloc,71NULL,72NULL,73NULL74};7576static size_t77isprime(size_t p)78{79size_t q, i;8081for(i = 2 ; i < p; i++) {82q = p / i;8384if (i * q == p)85return 0;86if (i * i > p)87return 1;88}89return 1;90}9192static size_t93findprime(size_t p)94{95if (p % 2 == 0)96p++;9798while (isprime(p) == 0)99p += 2;100101return p;102}103104/**105* Allocate an array106*107* @return A new allocated array, free with heim_release()108*/109110heim_dict_t111heim_dict_create(size_t size)112{113heim_dict_t dict;114115dict = _heim_alloc_object(&dict_object, sizeof(*dict));116117dict->size = findprime(size);118if (dict->size == 0) {119heim_release(dict);120return NULL;121}122123dict->tab = calloc(dict->size, sizeof(dict->tab[0]));124if (dict->tab == NULL) {125dict->size = 0;126heim_release(dict);127return NULL;128}129130return dict;131}132133/**134* Get type id of an dict135*136* @return the type id137*/138139heim_tid_t140heim_dict_get_type_id(void)141{142return HEIM_TID_DICT;143}144145/* Intern search function */146147static struct hashentry *148_search(heim_dict_t dict, heim_object_t ptr)149{150unsigned long v = heim_get_hash(ptr);151struct hashentry *p;152153for (p = dict->tab[v % dict->size]; p != NULL; p = p->next)154if (heim_cmp(ptr, p->key) == 0)155return p;156157return NULL;158}159160/**161* Search for element in hash table162*163* @value dict the dict to search in164* @value key the key to search for165*166* @return a retained copy of the value for key or NULL if not found167*/168169heim_object_t170heim_dict_copy_value(heim_dict_t dict, heim_object_t key)171{172struct hashentry *p;173p = _search(dict, key);174if (p == NULL)175return NULL;176177return heim_retain(p->value);178}179180/**181* Add key and value to dict182*183* @value dict the dict to add too184* @value key the key to add185* @value value the value to add186*187* @return 0 if added, errno if not188*/189190int191heim_dict_add_value(heim_dict_t dict, heim_object_t key, heim_object_t value)192{193struct hashentry **tabptr, *h;194195h = _search(dict, key);196if (h) {197heim_release(h->value);198h->value = heim_retain(value);199} else {200unsigned long v;201202h = malloc(sizeof(*h));203if (h == NULL)204return ENOMEM;205206h->key = heim_retain(key);207h->value = heim_retain(value);208209v = heim_get_hash(key);210211tabptr = &dict->tab[v % dict->size];212h->next = *tabptr;213*tabptr = h;214h->prev = tabptr;215if (h->next)216h->next->prev = &h->next;217}218219return 0;220}221222/**223* Delete element with key key224*225* @value dict the dict to delete from226* @value key the key to delete227*/228229void230heim_dict_delete_key(heim_dict_t dict, heim_object_t key)231{232struct hashentry *h = _search(dict, key);233234if (h == NULL)235return;236237heim_release(h->key);238heim_release(h->value);239240if ((*(h->prev) = h->next) != NULL)241h->next->prev = h->prev;242243free(h);244}245246/**247* Do something for each element248*249* @value dict the dict to interate over250* @value func the function to search for251* @value arg argument to func252*/253254void255heim_dict_iterate_f(heim_dict_t dict, heim_dict_iterator_f_t func, void *arg)256{257struct hashentry **h, *g;258259for (h = dict->tab; h < &dict->tab[dict->size]; ++h)260for (g = *h; g; g = g->next)261func(g->key, g->value, arg);262}263264#ifdef __BLOCKS__265/**266* Do something for each element267*268* @value dict the dict to interate over269* @value func the function to search for270*/271272void273heim_dict_iterate(heim_dict_t dict, void (^func)(heim_object_t, heim_object_t))274{275struct hashentry **h, *g;276277for (h = dict->tab; h < &dict->tab[dict->size]; ++h)278for (g = *h; g; g = g->next)279func(g->key, g->value);280}281#endif282283284