summaryrefslogtreecommitdiff
path: root/wm/table.c
blob: b6d5c9e8a981233782bc386fe49ad06d7b2e7869 (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
#include <stdlib.h>
#include "xcb_wm.h"

typedef struct node node;
struct node {
	node *next;
	uint32_t key;
	void *value;
};

struct table_t {
	node *head;
};

table_t *alloc_table()
{
	return calloc(1, sizeof(table_t));
}

void free_table(table_t *table)
{
	free(table);
}

int table_put(table_t *table, uint32_t key, void *value)
{
	node *record = malloc(sizeof(node));
	if(!record)
		return 0;
	record->next = table->head;
	record->key = key;
	record->value = value;
	table->head = record;
	return 1;
}

void *table_get(table_t *table, uint32_t key)
{
	node *cur;
	for(cur = table->head; cur; cur = cur->next)
		if(cur->key == key)
			return cur->value;
	return 0;
}

void *table_remove(table_t *table, uint32_t key)
{
	node **cur;
	for(cur = &table->head; *cur; cur = &(*cur)->next)
		if((*cur)->key == key)
		{
			node *tmp = *cur;
			void *ret = tmp->value;
			*cur = (*cur)->next;
			free(tmp);
			return ret;
		}
	return 0;
}