@@ -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
217
+ self ._ensure_mapping_populated()
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()
@@ -266,30 +262,32 @@ cdef class IndexEngine:
266
262
cdef _check_type(self , object val):
267
263
hash (val)
268
264
265
+ property is_mapping_populated :
266
+
267
+ def __get__ (self ):
268
+ return self .mapping is not None
269
+
269
270
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:
275
- self .initialize()
276
-
277
- cdef initialize(self ):
278
- values = self ._get_index_values()
271
+ # this populates the mapping
272
+ # if its not already populated
273
+ # also satisfies the need_unique_check
279
274
280
- self .mapping = self ._make_hash_table(len (values))
281
- self .mapping.map_locations(values)
275
+ if not self .is_mapping_populated:
282
276
283
- if len (self .mapping) == len (values):
284
- self .unique = 1
277
+ values = self ._get_index_values()
278
+
279
+ self .mapping = self ._make_hash_table(len (values))
280
+ self .mapping.map_locations(values)
281
+
282
+ if len (self .mapping) == len (values):
283
+ self .unique = 1
285
284
286
- self .initialized = 1
285
+ self .need_unique_check = 0
287
286
288
287
def clear_mapping (self ):
289
288
self .mapping = None
290
- self .initialized = 0
291
- self .monotonic_check = 0
292
- self .unique_check = 0
289
+ self .need_monotonic_check = 1
290
+ self .need_unique_check = 1
293
291
294
292
self .unique = 0
295
293
self .monotonic_inc = 0
0 commit comments