aboutsummaryrefslogtreecommitdiffstats
path: root/src/manager/lib/dict.c
blob: a5fdd52d514acf1c361c3e0af942f77a038ea4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * @file dict.c
 *
 * @brief Implementation of dict_t.
 *
 */

/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "dict.h"

#include <utils/linked_list.h>


typedef struct private_dict_t private_dict_t;

/**
 * private data of dict
 */
struct private_dict_t {

	/**
	 * public functions
	 */
	dict_t public;
	
	/**
	 * baaah, we really should have a hashtable for this
	 */
	linked_list_t *list;
	
	/**
	 * key comparator function
	 */
	bool(*key_comparator)(void*,void*);
	
	/**
	 * destructor function for key
	 */
	 void(*key_destructor)(void*);
	 
	 /**
	  * destructor function for value
	  */
	 void(*value_destructor)(void*);
};

/**
 * key value pair to store entries
 */
typedef struct {
	void *key;
	void *value;
} key_value_t;

/**
 * Implementation of dict_t.get.
 */
static void* get(private_dict_t *this, void *key)
{
	key_value_t *kv;
	iterator_t *iterator;
	void *value = NULL;
	
	iterator = this->list->create_iterator(this->list, TRUE);
	while (iterator->iterate(iterator, (void**)&kv))
	{
		if (this->key_comparator(kv->key, key))
		{
			value = kv->value;
			break;
		}
	}
	iterator->destroy(iterator);
	return value;
}
/**
 * Implementation of dict_t.set.
 */
static void set(private_dict_t *this, void *key, void *value)
{
	/* we don't overwrite, just prepend */
	key_value_t *kv = malloc_thing(key_value_t);
	kv->key = key;
	kv->value = value;
	this->list->insert_first(this->list, kv);
}


/**
 * comparator for strings
 */
bool dict_streq(void *a, void *b)
{
	return streq(a, b);
}

/**
 * Implementation of dict_t.destroy
 */
static void destroy(private_dict_t *this)
{
	key_value_t *kv;

	while (this->list->remove_last(this->list, (void**)&kv) == SUCCESS)
	{
		if (this->key_destructor)
		{
			this->key_destructor(kv->key);
		}
		if (this->value_destructor)
		{
			this->value_destructor(kv->value);
		}
		free(kv);
	}
	this->list->destroy(this->list);
	free(this);
}

/*
 * see header file
 */
dict_t *dict_create(bool(*key_comparator)(void*,void*),
					void(*key_destructor)(void*),
					void(*value_destructor)(void*))
{
	private_dict_t *this = malloc_thing(private_dict_t);
	
	this->public.set = (void(*)(dict_t*, void *key, void *value))set;
	this->public.get = (void*(*)(dict_t*, void *key))get;
	this->public.destroy = (void(*)(dict_t*))destroy;
	
	this->list = linked_list_create();
	this->key_comparator = key_comparator;
	this->key_destructor = key_destructor;
	this->value_destructor = value_destructor;
	
	return &this->public;
}