Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure for string indices?

I'm looking for a data structure for string(UTF-8) indices that is highly optimized for range queries and space usage. Thanks!

Elaboration: I have list of arbitrary length utf-8 strings that i need to index. I will be use only range queries.

Example: I have strings - apple, ape, black, cool, dark.

Query will be something like this - "get from 2 to 3 element in desc order" or "get strings that start by 'ap'"

like image 897
Artem Avatar asked Sep 06 '26 15:09

Artem


2 Answers

Since you mentioned "relatively static", a simple sorted array would do everything you want and is highly optimized both in terms of space and time.

"get from 2 to 3 element in desc order" is simply a lookup of the corresponding array indices.

"get strings that start by 'ap'" can be done with a binary search. The search will stop at or just before the first string that starts with 'ap', and from there on, you just scan through until you find all such strings.

like image 184
casablanca Avatar answered Sep 08 '26 04:09

casablanca


Did you check Tries?

The structure should fit what you need - both the range and the 'start with' should be easy, plus memory consumption is also good.

like image 33
Unreason Avatar answered Sep 08 '26 04:09

Unreason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!