diff options
Diffstat (limited to 'src/sord.c')
-rw-r--r-- | src/sord.c | 28 |
1 files changed, 13 insertions, 15 deletions
@@ -664,11 +664,8 @@ index_search(SordModel* sord, GSequence* db, const SordQuad search_key) } static inline GSequenceIter* -index_lower_bound(SordModel* sord, GSequence* db, const SordQuad search_key) +index_lower_bound_iter(SordModel* sord, GSequenceIter* i, const SordQuad search_key) { - GSequenceIter* i = g_sequence_search( - db, (void*)search_key, sord_quad_compare, sord); - /* i is now at the position where search_key would be inserted, but this is not necessarily a match, and we need the leftmost... */ @@ -708,6 +705,14 @@ index_lower_bound(SordModel* sord, GSequence* db, const SordQuad search_key) return i; } +static inline GSequenceIter* +index_lower_bound(SordModel* sord, GSequence* db, const SordQuad search_key) +{ + GSequenceIter* i = g_sequence_search( + db, (void*)search_key, sord_quad_compare, sord); + return index_lower_bound_iter(sord, i, search_key); +} + SordIter* sord_find(SordModel* sord, const SordQuad pat) { @@ -968,9 +973,10 @@ sord_add_to_index(SordModel* sord, const SordQuad tup, SordOrder order) const SordQuad key = { tup[ordering[0]], tup[ordering[1]], tup[ordering[2]], tup[ordering[3]] }; - GSequenceIter* const cur = index_search(sord, sord->indices[order], key); - if (!g_sequence_iter_is_end(cur) - && !sord_quad_compare(g_sequence_get(cur), key, sord)) { + GSequenceIter* const cur = index_search(sord, sord->indices[order], key); + GSequenceIter* const match = index_lower_bound_iter(sord, cur, key); + if (!g_sequence_iter_is_end(match) + && !sord_quad_compare(g_sequence_get(match), key, sord)) { return false; // Quad already stored in this index } @@ -991,14 +997,6 @@ sord_add(SordModel* sord, const SordQuad tup) return false; } - // FIXME: Remove double search - SordQuad pat = { tup[0], tup[1], tup[2], tup[3] }; - SordIter* existing = sord_find(sord, pat); - if (existing) { - sord_iter_free(existing); - return false; - } - for (unsigned i = 0; i < NUM_ORDERS; ++i) { if (sord->indices[i]) { if (!sord_add_to_index(sord, tup, i)) { |