-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage.c
76 lines (65 loc) · 1.85 KB
/
stage.c
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
#include "util.h"
#include "stage.h"
#include "key.h"
#define trie_index(__t) ((__t)->index)
/* Staging tags created with mkdir */
Stage *new_stage ()
{
Stage *res = g_try_malloc0(sizeof(Stage));
trie_index(res) = g_hash_table_new(g_direct_hash, g_direct_equal);
if (!res)
return NULL;
res->data = new_trie();
return res;
}
AbstractFile* stage_lookup (Stage *s, tagdb_key_t position, file_id_t id)
{
return trie_retrieve(s->data, position, TO_SP(id));
}
void stage_add (Stage *s, tagdb_key_t position, AbstractFile *item)
{
tagdb_key_t index_key = key_copy(position);
key_push_end(index_key, get_file_id(item));
KL(index_key, i)
{
gpointer k = TO_SP(key_ref(index_key, i));
GList *l = g_hash_table_lookup(trie_index(s), k);
l = g_list_prepend(l, key_copy(position));
g_hash_table_insert(trie_index(s), k, l);
}
key_destroy(index_key);
trie_insert(s->data, position, TO_SP(get_file_id(item)), item);
}
void trie_remove_by_bucket_key (Stage *t, AbstractFile *s)
{
gpointer id = TO_SP(get_file_id(s));
GList *l = g_hash_table_lookup(trie_index(t), id);
LL(l, it)
{
stage_remove(t, it->data, s);
}LL_END;
g_hash_table_remove(trie_index(t), id);
g_list_free_full(l, (GDestroyNotify)key_destroy);
}
void stage_remove (Stage *s, tagdb_key_t position, AbstractFile *f)
{
trie_remove(s->data, position, TO_SP(get_file_id(f)));
}
void stage_remove_tag (Stage *s, AbstractFile *t)
{
trie_remove_by_bucket_key(s, t);
}
GList *stage_list_position (Stage *s, tagdb_key_t position)
{
return trie_retrieve_bucket_l(s->data, position);
}
void stage_destroy (Stage *s)
{
HL(trie_index(s), it, k, v)
{
g_list_free_full(v, (GDestroyNotify) key_destroy);
} HL_END
g_hash_table_destroy(trie_index(s));
trie_destroy(s->data);
g_free(s);
}