I've been struggling with finding a suitable data structure that meets these requirements:
- The elements of this data structure are organized in blocks. The order of these blocks are somewhat irrelevant, but the elements within the block maintain a certain order.
- The number of insertions are not frequent.
- Searches are by far more frequent.
- Retrieving the index of the element within the data structure is critical.
- Once an element of the data structure has been searched for, the next or previous (neightbour) element in the data structure should be easily accessible.
With this in mind, I have these considerations:
- A linked or doubly-linked list might be optimal for requirements 1, 2, 4 and 5, but forces a linear search, which compromises req. 3.
- A hash table solves req. 3, but as far as I understand poses a problem in req. 5 because in using hashes one loses control on the position of the elements within the data structure.
Developing a hash function that matches the order I need can be tricky because the input keys can be partially random.
A possible solution that I was considering (in C) was to keep an array of pointers to the elements that maintain the insertion order (or the order I need) and then a second array of pointers that sort the elements using a hash function. While the first array can be used to quickly access the elements and finding the neighbours, the second array can be used to quickly search the elements. But somehow I have the impression of overly complicating things and I do not want to reinvent the wheel.
What do you think? Any suggestion would be more than appreciated.
Many thanks