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

BLI_edgehash.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26eb8ab7665f2d61cf0cc575b00e5213485cc8ae (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
253
254
255
/*
 * 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.
 */

#pragma once

/** \file
 * \ingroup bli
 */

#include "BLI_compiler_attrs.h"
#include "BLI_utildefines.h"

#ifdef __cplusplus
extern "C" {
#endif

struct EdgeHash;
typedef struct EdgeHash EdgeHash;

struct _EdgeHash_Edge {
  uint v_low, v_high;
};

struct _EdgeHash_Entry {
  struct _EdgeHash_Edge edge;
  void *value;
};

typedef struct EdgeHashIterator {
  struct _EdgeHash_Entry *entries;
  uint length;
  uint index;
} EdgeHashIterator;

typedef void (*EdgeHashFreeFP)(void *key);

enum {
  /**
   * Only checked for in debug mode.
   */
  EDGEHASH_FLAG_ALLOW_DUPES = (1 << 0),
};

EdgeHash *BLI_edgehash_new_ex(const char *info, unsigned int nentries_reserve);
EdgeHash *BLI_edgehash_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP free_value);
void BLI_edgehash_print(EdgeHash *eh);
/**
 * Insert edge (\a v0, \a v1) into hash with given value, does
 * not check for duplicates.
 */
void BLI_edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val);
/**
 * Assign a new value to a key that may already be in edgehash.
 */
bool BLI_edgehash_reinsert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val);
/**
 * Return value for given edge (\a v0, \a v1), or NULL if
 * if key does not exist in hash. (If need exists
 * to differentiate between key-value being NULL and
 * lack of key then see #BLI_edgehash_lookup_p().
 */
void *BLI_edgehash_lookup(const EdgeHash *eh,
                          unsigned int v0,
                          unsigned int v1) ATTR_WARN_UNUSED_RESULT;
/**
 * A version of #BLI_edgehash_lookup which accepts a fallback argument.
 */
void *BLI_edgehash_lookup_default(const EdgeHash *eh,
                                  unsigned int v0,
                                  unsigned int v1,
                                  void *default_value) ATTR_WARN_UNUSED_RESULT;
/**
 * Return pointer to value for given edge (\a v0, \a v1),
 * or NULL if key does not exist in hash.
 */
void **BLI_edgehash_lookup_p(EdgeHash *eh,
                             unsigned int v0,
                             unsigned int v1) ATTR_WARN_UNUSED_RESULT;
/**
 * Ensure \a (v0, v1) is exists in \a eh.
 *
 * This handles the common situation where the caller needs ensure a key is added to \a eh,
 * constructing a new value in the case the key isn't found.
 * Otherwise use the existing value.
 *
 * Such situations typically incur multiple lookups, however this function
 * avoids them by ensuring the key is added,
 * returning a pointer to the value so it can be used or initialized by the caller.
 *
 * \return true when the value didn't need to be added.
 * (when false, the caller _must_ initialize the value).
 */
bool BLI_edgehash_ensure_p(EdgeHash *eh, unsigned int v0, unsigned int v1, void ***r_val)
    ATTR_WARN_UNUSED_RESULT;
/**
 * Remove \a key (v0, v1) from \a eh, or return false if the key wasn't found.
 *
 * \param v0, v1: The key to remove.
 * \param free_value: Optional callback to free the value.
 * \return true if \a key was removed from \a eh.
 */
bool BLI_edgehash_remove(EdgeHash *eh,
                         unsigned int v0,
                         unsigned int v1,
                         EdgeHashFreeFP free_value);

/**
 * Remove \a key (v0, v1) from \a eh, returning the value or NULL if the key wasn't found.
 *
 * \param v0, v1: The key to remove.
 * \return the value of \a key int \a eh or NULL.
 */
void *BLI_edgehash_popkey(EdgeHash *eh, unsigned int v0, unsigned int v1) ATTR_WARN_UNUSED_RESULT;
/**
 * Return boolean true/false if edge (v0,v1) in hash.
 */
bool BLI_edgehash_haskey(const EdgeHash *eh,
                         unsigned int v0,
                         unsigned int v1) ATTR_WARN_UNUSED_RESULT;
/**
 * Return number of keys in hash.
 */
int BLI_edgehash_len(const EdgeHash *eh) ATTR_WARN_UNUSED_RESULT;
/**
 * Remove all edges from hash.
 */
void BLI_edgehash_clear_ex(EdgeHash *eh, EdgeHashFreeFP free_value, uint reserve);
/**
 * Wraps #BLI_edgehash_clear_ex with zero entries reserved.
 */
void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP free_value);

/**
 * Create a new #EdgeHashIterator. The hash table must not be mutated
 * while the iterator is in use, and the iterator will step exactly
 * #BLI_edgehash_len(eh) times before becoming done.
 */
EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
/**
 * Initialize an already allocated #EdgeHashIterator. The hash table must not
 * be mutated while the iterator is in use, and the iterator will
 * step exactly BLI_edgehash_len(eh) times before becoming done.
 *
 * \param ehi: The #EdgeHashIterator to initialize.
 * \param eh: The #EdgeHash to iterate over.
 */
void BLI_edgehashIterator_init(EdgeHashIterator *ehi, EdgeHash *eh);
/**
 * Free an #EdgeHashIterator.
 */
void BLI_edgehashIterator_free(EdgeHashIterator *ehi);

BLI_INLINE void BLI_edgehashIterator_step(EdgeHashIterator *ehi)
{
  ehi->index++;
}
BLI_INLINE bool BLI_edgehashIterator_isDone(const EdgeHashIterator *ehi)
{
  return ehi->index >= ehi->length;
}
BLI_INLINE void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi,
                                            unsigned int *r_v0,
                                            unsigned int *r_v1)
{
  struct _EdgeHash_Edge edge = ehi->entries[ehi->index].edge;
  *r_v0 = edge.v_low;
  *r_v1 = edge.v_high;
}
BLI_INLINE void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi)
{
  return ehi->entries[ehi->index].value;
}
BLI_INLINE void **BLI_edgehashIterator_getValue_p(EdgeHashIterator *ehi)
{
  return &ehi->entries[ehi->index].value;
}
BLI_INLINE void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val)
{
  ehi->entries[ehi->index].value = val;
}

#define BLI_EDGEHASH_SIZE_GUESS_FROM_LOOPS(totloop) ((totloop) / 2)
#define BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(totpoly) ((totpoly)*2)

/* *** EdgeSet *** */

struct EdgeSet;
typedef struct EdgeSet EdgeSet;

typedef struct EdgeSetIterator {
  struct _EdgeHash_Edge *edges;
  uint length;
  uint index;
} EdgeSetIterator;

EdgeSet *BLI_edgeset_new_ex(const char *info,
                            unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
EdgeSet *BLI_edgeset_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
int BLI_edgeset_len(const EdgeSet *es) ATTR_WARN_UNUSED_RESULT;
/**
 * A version of BLI_edgeset_insert which checks first if the key is in the set.
 * \returns true if a new key has been added.
 *
 * \note #EdgeHash has no equivalent to this because typically the value would be different.
 */
bool BLI_edgeset_add(EdgeSet *es, unsigned int v0, unsigned int v1);
/**
 * Adds the key to the set (no checks for unique keys!).
 * Matching #BLI_edgehash_insert
 */
void BLI_edgeset_insert(EdgeSet *es, unsigned int v0, unsigned int v1);
bool BLI_edgeset_haskey(const EdgeSet *es,
                        unsigned int v0,
                        unsigned int v1) ATTR_WARN_UNUSED_RESULT;
void BLI_edgeset_free(EdgeSet *es);

/* rely on inline api for now */

EdgeSetIterator *BLI_edgesetIterator_new(EdgeSet *es);
void BLI_edgesetIterator_free(EdgeSetIterator *esi);

BLI_INLINE void BLI_edgesetIterator_getKey(EdgeSetIterator *esi,
                                           unsigned int *r_v0,
                                           unsigned int *r_v1)
{
  struct _EdgeHash_Edge edge = esi->edges[esi->index];
  *r_v0 = edge.v_low;
  *r_v1 = edge.v_high;
}
BLI_INLINE void BLI_edgesetIterator_step(EdgeSetIterator *esi)
{
  esi->index++;
}
BLI_INLINE bool BLI_edgesetIterator_isDone(const EdgeSetIterator *esi)
{
  return esi->index >= esi->length;
}

#ifdef __cplusplus
}
#endif