What is the data structure for O (1) random insert / delete and O (1) random access?

I do not know what data structure to use for this problem. I want the structure to have:

  • Insert or delete a time constant.
  • Search by constant time by identifier.

Actual system:

I have a bunch of objects with a unique identifier. My program will have to receive requests for an identifier and return the corresponding object.

Whenever he receives a request, I want him to: look for a structure to see if it is there. If so, return it. If this is not the case, load it from the disk into memory (put it in the structure so that the next time it is requested, it does not need to use the disk), and then return it.

I use C.

Here's a similar question , but I'm not sure how appropriate it is.

+3
source share
4 answers

A Hash table can be a pretty good solution in your case - even if it is not in O (1) when there is a collision: this is a pretty efficient solution.

+11
source

The only structure that suits her is ... C array.  SomeObject arr[MAX_NUM_OBJECTS]  handles it in a fast and efficient way.

+2
source

, mmap ?

+1

, . , (, ..). - . , - .

, , . O (1) ( ) . O (1) , , , ( , ).

To give you the best answer, we really need to know what you are trying to achieve. Any additional information about what you use for the data structure would help

0
source

Source: https://habr.com/ru/post/1727364/


All Articles