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

outliner_treehash.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9497d389e7fd615f14beeed73dda615110d63e0 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bke
 *
 * Tree hash for the outliner space.
 */

#include <stdlib.h>
#include <string.h>

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

#include "DNA_outliner_types.h"

#include "BKE_outliner_treehash.h"

#include "MEM_guardedalloc.h"

typedef struct TseGroup {
  TreeStoreElem **elems;
  int lastused;
  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;
  tse_group->lastused = 0;
  return tse_group;
}

static void tse_group_add_element(TseGroup *tse_group, TreeStoreElem *elem)
{
  if (UNLIKELY(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_remove_element(TseGroup *tse_group, TreeStoreElem *elem)
{
  int min_allocated = MAX2(1, tse_group->allocated / 2);
  BLI_assert(tse_group->allocated == 1 || (tse_group->allocated % 2) == 0);

  tse_group->size--;
  BLI_assert(tse_group->size >= 0);
  for (int i = 0; i < tse_group->size; i++) {
    if (tse_group->elems[i] == elem) {
      memcpy(tse_group->elems[i],
             tse_group->elems[i + 1],
             (tse_group->size - (i + 1)) * sizeof(TreeStoreElem *));
      break;
    }
  }

  if (UNLIKELY(tse_group->size > 0 && tse_group->size <= min_allocated)) {
    tse_group->allocated = min_allocated;
    tse_group->elems = MEM_reallocN(tse_group->elems,
                                    sizeof(TreeStoreElem *) * tse_group->allocated);
  }
}

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;
  union {
    short h_pair[2];
    unsigned int u_int;
  } hash;

  BLI_assert((tse->type != TSE_SOME_ID) || !tse->nr);

  hash.h_pair[0] = tse->type;
  hash.h_pair[1] = tse->nr;

  hash.u_int ^= BLI_ghashutil_ptrhash(tse->id);

  return hash.u_int;
}

static bool 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);

  BLI_assert(treehash);

  while ((tselem = BLI_mempool_iterstep(&iter))) {
    BKE_outliner_treehash_add_element(treehash, tselem);
  }
}

void *BKE_outliner_treehash_create_from_treestore(BLI_mempool *treestore)
{
  GHash *treehash = BLI_ghash_new_ex(tse_hash, tse_cmp, "treehash", BLI_mempool_len(treestore));
  fill_treehash(treehash, treestore);
  return treehash;
}

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

void BKE_outliner_treehash_clear_used(void *treehash)
{
  GHashIterator gh_iter;

  GHASH_ITER (gh_iter, treehash) {
    TseGroup *group = BLI_ghashIterator_getValue(&gh_iter);
    group->lastused = 0;
  }
}

void *BKE_outliner_treehash_rebuild_from_treestore(void *treehash, BLI_mempool *treestore)
{
  BLI_assert(treehash);

  BLI_ghash_clear_ex(treehash, NULL, free_treehash_group, BLI_mempool_len(treestore));
  fill_treehash(treehash, treestore);
  return treehash;
}

void BKE_outliner_treehash_add_element(void *treehash, TreeStoreElem *elem)
{
  TseGroup *group;
  void **val_p;

  if (!BLI_ghash_ensure_p(treehash, elem, &val_p)) {
    *val_p = tse_group_create();
  }
  group = *val_p;
  group->lastused = 0;
  tse_group_add_element(group, elem);
}

void BKE_outliner_treehash_remove_element(void *treehash, TreeStoreElem *elem)
{
  TseGroup *group = BLI_ghash_lookup(treehash, elem);

  BLI_assert(group != NULL);
  if (group->size <= 1) {
    /* one element -> remove group completely */
    BLI_ghash_remove(treehash, elem, NULL, free_treehash_group);
  }
  else {
    tse_group_remove_element(group, elem);
  }
}

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

  BLI_assert(th);

  return BLI_ghash_lookup(th, &tse_template);
}

TreeStoreElem *BKE_outliner_treehash_lookup_unused(void *treehash,
                                                   short type,
                                                   short nr,
                                                   struct ID *id)
{
  TseGroup *group;

  BLI_assert(treehash);

  group = BKE_outliner_treehash_lookup_group(treehash, type, nr, id);
  if (group) {
    /* Find unused element, with optimization to start from previously
     * found element assuming we do repeated lookups. */
    int size = group->size;
    int offset = group->lastused;

    for (int i = 0; i < size; i++, offset++) {
      if (offset >= size) {
        offset = 0;
      }

      if (!group->elems[offset]->used) {
        group->lastused = offset;
        return group->elems[offset];
      }
    }
  }
  return NULL;
}

TreeStoreElem *BKE_outliner_treehash_lookup_any(void *treehash,
                                                short type,
                                                short nr,
                                                struct ID *id)
{
  TseGroup *group;

  BLI_assert(treehash);

  group = BKE_outliner_treehash_lookup_group(treehash, type, nr, id);
  return group ? group->elems[0] : NULL;
}

void BKE_outliner_treehash_free(void *treehash)
{
  BLI_assert(treehash);

  BLI_ghash_free(treehash, NULL, free_treehash_group);
}