@@ -82,20 +82,13 @@ cdef class IndexEngine:
82
82
83
83
cdef:
84
84
bint unique, monotonic_inc, monotonic_dec
85
- bint initialized, monotonic_check, unique_check
85
+ bint need_monotonic_check, need_unique_check
86
86
87
87
def __init__ (self , vgetter , n ):
88
88
self .vgetter = vgetter
89
89
90
90
self .over_size_threshold = n >= _SIZE_CUTOFF
91
-
92
- self .initialized = 0
93
- self .monotonic_check = 0
94
- self .unique_check = 0
95
-
96
- self .unique = 0
97
- self .monotonic_inc = 0
98
- self .monotonic_dec = 0
91
+ self .clear_mapping()
99
92
100
93
def __contains__ (self , object val ):
101
94
self ._ensure_mapping_populated()
@@ -213,24 +206,28 @@ cdef class IndexEngine:
213
206
property is_unique :
214
207
215
208
def __get__ (self ):
216
- if not self .initialized :
217
- self .initialize ()
209
+ if self .need_unique_check :
210
+ self ._do_unique_check ()
218
211
219
- self .unique_check = 1
220
212
return self .unique == 1
221
213
214
+ cdef inline _do_unique_check(self ):
215
+
216
+ # this de-facto the same as initialization
217
+ self .initialize()
218
+
222
219
property is_monotonic_increasing :
223
220
224
221
def __get__ (self ):
225
- if not self .monotonic_check :
222
+ if self .need_monotonic_check :
226
223
self ._do_monotonic_check()
227
224
228
225
return self .monotonic_inc == 1
229
226
230
227
property is_monotonic_decreasing :
231
228
232
229
def __get__ (self ):
233
- if not self .monotonic_check :
230
+ if self .need_monotonic_check :
234
231
self ._do_monotonic_check()
235
232
236
233
return self .monotonic_dec == 1
@@ -246,13 +243,12 @@ cdef class IndexEngine:
246
243
self .monotonic_dec = 0
247
244
is_unique = 0
248
245
249
- self .monotonic_check = 1
246
+ self .need_monotonic_check = 0
250
247
251
248
# we can only be sure of uniqueness if is_unique=1
252
249
if is_unique:
253
- self .initialized = 1
254
250
self .unique = 1
255
- self .unique_check = 1
251
+ self .need_unique_check = 0
256
252
257
253
cdef _get_index_values(self ):
258
254
return self .vgetter()
@@ -267,29 +263,37 @@ cdef class IndexEngine:
267
263
hash (val)
268
264
269
265
cdef inline _ensure_mapping_populated(self ):
270
- # need to reset if we have previously
271
- # set the initialized from monotonic checks
272
- if self .unique_check:
273
- self .initialized = 0
274
- if not self .initialized:
266
+ # populate the mappings
267
+
268
+ if self .need_unique_check or not self .initialized:
275
269
self .initialize()
276
270
271
+ property initialized :
272
+
273
+ def __get__ (self ):
274
+ return self .mapping is not None
275
+
277
276
cdef initialize(self ):
278
- values = self ._get_index_values()
277
+ # this populates the mapping
278
+ # if its not already populated
279
+ # also satisfies the need_unique_check
279
280
280
- self .mapping = self ._make_hash_table(len (values))
281
- self .mapping.map_locations(values)
281
+ if not self .initialized:
282
282
283
- if len (self .mapping) == len (values):
284
- self .unique = 1
283
+ values = self ._get_index_values()
284
+
285
+ self .mapping = self ._make_hash_table(len (values))
286
+ self .mapping.map_locations(values)
287
+
288
+ if len (self .mapping) == len (values):
289
+ self .unique = 1
285
290
286
- self .initialized = 1
291
+ self .need_unique_check = 0
287
292
288
293
def clear_mapping (self ):
289
294
self .mapping = None
290
- self .initialized = 0
291
- self .monotonic_check = 0
292
- self .unique_check = 0
295
+ self .need_monotonic_check = 1
296
+ self .need_unique_check = 1
293
297
294
298
self .unique = 0
295
299
self .monotonic_inc = 0
0 commit comments