Welcome to mirror list, hosted at ThFree Co, Russian Federation.

treehash.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d763b22e26e1d4f65e73ae9a427710be5833c6c (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
#include "BKE_treehash.h"

#include "BLI_ghash.h"
#include "BLI_mempool.h"
#include "BLI_utildefines.h"

#include "DNA_outliner_types.h"

#include "MEM_guardedalloc.h"

typedef struct TseGroup
{
	TreeStoreElem **elems;
	int size;
	int allocated;
} TseGroup;

/* Allocate structure for TreeStoreElements;
 * Most of elements in treestore have no duplicates,
 * so there is no need to preallocate memory for more than one pointer */
static TseGroup *tse_group_create(void)
{
	TseGroup *tse_group = MEM_mallocN(sizeof(TseGroup), "TseGroup");
	tse_group->elems = MEM_mallocN(sizeof(TreeStoreElem *), "TseGroupElems");
	tse_group->size = 0;
	tse_group->allocated = 1;
	return tse_group;
}

static void tse_group_add(TseGroup *tse_group, TreeStoreElem *elem)
{
	if (tse_group->size == tse_group->allocated) {
		tse_group->allocated *= 2;
		tse_group->elems = MEM_reallocN(tse_group->elems, sizeof(TreeStoreElem *) * tse_group->allocated);
	}
	tse_group->elems[tse_group->size] = elem;
	tse_group->size++;
}

static void tse_group_free(TseGroup *tse_group)
{
	MEM_freeN(tse_group->elems);
	MEM_freeN(tse_group);
}

static unsigned int tse_hash(const void *ptr)
{
	const TreeStoreElem *tse = ptr;
	unsigned int hash;
	BLI_assert(tse->type || !tse->nr);
	hash = BLI_ghashutil_inthash(SET_INT_IN_POINTER((tse->nr << 16) + tse->type));
	hash ^= BLI_ghashutil_inthash(tse->id);
	return hash;
}

static int tse_cmp(const void *a, const void *b)
{
	const TreeStoreElem *tse_a = a;
	const TreeStoreElem *tse_b = b;
	return tse_a->type != tse_b->type || tse_a->nr != tse_b->nr || tse_a->id != tse_b->id;
}

static void fill_treehash(void *treehash, BLI_mempool *treestore)
{
	TreeStoreElem *tselem;
	BLI_mempool_iter iter;
	BLI_mempool_iternew(treestore, &iter);
	while ((tselem = BLI_mempool_iterstep(&iter))) {
		BKE_treehash_add_element(treehash, tselem);
	}
}

void *BKE_treehash_create_from_treestore(BLI_mempool *treestore)
{
	GHash *treehash = BLI_ghash_new(tse_hash, tse_cmp, "treehash");
	fill_treehash(treehash, treestore);
	return treehash;
}

static void free_treehash_group(void *key) {
	tse_group_free(key);
}

void *BKE_treehash_rebuild_from_treestore(void *treehash, BLI_mempool *treestore)
{
	BLI_ghash_clear(treehash, NULL, free_treehash_group);
	fill_treehash(treehash, treestore);
	return treehash;
}

void BKE_treehash_add_element(void *treehash, TreeStoreElem *elem)
{
	TseGroup *group = BLI_ghash_lookup(treehash, elem);
	if (!group) {
		group = tse_group_create();
		BLI_ghash_insert(treehash, elem, group);
	}
	tse_group_add(group, elem);
}

static TseGroup *BKE_treehash_lookup_group(GHash *th, short type, short nr, struct ID *id)
{
	TreeStoreElem tse_template;
	tse_template.type = type;
	tse_template.nr = type ? nr : 0;  // we're picky! :)
	tse_template.id = id;
	return BLI_ghash_lookup(th, &tse_template);
}

TreeStoreElem *BKE_treehash_lookup_unused(void *treehash, short type, short nr, struct ID *id)
{
	TseGroup *group = BKE_treehash_lookup_group(treehash, type, nr, id);
	if (group) {
		int i;
		for (i = 0; i < group->size; i++) {
			if (!group->elems[i]->used) {
				return group->elems[i];
			}
		}
	}
	return NULL;
}

TreeStoreElem *BKE_treehash_lookup_any(void *treehash, short type, short nr, struct ID *id)
{
	TseGroup *group = BKE_treehash_lookup_group(treehash, type, nr, id);
	return group ? group->elems[0] : NULL;
}

void BKE_treehash_free(void *treehash)
{
	BLI_ghash_free(treehash, NULL, free_treehash_group);
}