Which data structures to use when storing multiple entities with multiple query criteria?

rd22 :

There is a storage unit, with has a capacity for N items. Initially this unit is empty. The space is arranged in a linear manner, i.e. one beside the other in a line. Each storage space has a number, increasing till N.

When someone drops their package, it is assigned the first available space. The packages could also be picked up, in this case the space becomes vacant. Example: If the total capacity was 4. and 1 and 2 are full the third person to come in will be assigned the space 3. If 1, 2 and 3 were full and the 2nd space becomes vacant, the next person to come will be assigned the space 2.

The packages they drop have 2 unique properties, assigned for immediate identification. First they are color coded based on their content and second they are assigned a unique identification number(UIN).

What we want is to query the system:

  1. When the input is color, show all the UIN associated with this color.
  2. When the input is color, show all the numbers where these packages are placed(storage space number).
  3. Show where an item with a given UIN is placed, i.e. storage space number.

I would like to know how which Data Structures to use for this case, so that the system works as efficiently as possible? And I am not given which of these operations os most frequent, which means I will have to optimise for all the cases.

Please take a note, even though the query process is not directly asking for storage space number, but when an item is removed from the store it is removed by querying from the storage space number.

rd22 :

For getting the storage space number I used a min heap approach, PriorityQueue. This works in O(log n) time, removal and insertion both.

I used 2 BiMaps, self-created data structures, for storing the mapping between UIN, color and storage space number. These BiMaps used internally a HashMap and an array of size N.

In first BiMap(BiMap1), a HashMap<color, Set<StorageSpace>> stores the mapping of color to the list of storage spaces's. And a String array String[] colorSpace which stores the color at the storage space index.

In the Second BiMap(BiMap2), a HashMap<UIN, storageSpace> stores the mapping between UIN and storageSpace. And a string arrayString[] uinSpace` stores the UIN at the storage space index.

Querying is straight forward with this approach:

  1. When the input is color, show all the UIN associated with this color. Get the List of storage spaces from BiMap1, for these spaces use the array in BiMap2 to get the corresponding UIN's.
  2. When the input is color, show all the numbers where these packages are placed(storage space number). Use BiMap1's HashMap to get the list.
  3. Show where an item with a given UIN is placed, i.e. storage space number. Use BiMap2 to get the values from the HashMap.

Now when we are given a storage space to remove, both the BiMaps have to be updated. In BiMap1 get the entry from the array, get the corersponding Set, and remove the space number from this set. From BiMap2 get the UIN from the array, remove it and also remove it from the HashMap.

For both the BiMaps the removal and the insert operations are O(1). And the Min heap works in O(Log n), hence the total time complexity is O(Log N)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=455126&siteId=1