0

My record structure is as below:- (Name,Option,StartNum,EndNum,Vacancy)

Name,Option,StartNum,EndNum form the composite keys of the sturcture/table. So for a given combination of these, there would be only one Vacancy record.

Sample Records

ABC,X,2,14,1
ADE,X,3,8,0
AEF,Y,1,12,2
ERF,X,12,13,17

There could be:

  1. 250-300 Names

  2. For each Name 20-30 Options

  3. For each Option 1-45 StartNum

  4. For each StartNum 1-14 EndNum

  5. For each combination of above entries, there would be only one entry for Vacancy.

So there could be maximum of 5,670,000 (300*30*45*14) entries

Fast operations to support

  1. Search on the composite key i.e. (Name+Option+StartNum+EndNum) and fetch it's Vacancy record value
  2. For a given Name,Option and a Number, search and delete records having the given Name,Option and StartNum<=Number<=EndNum

Can anyone please suggest the appropriate datastructure for my above requirement. The data structure building operation can be slow as its done offline, but the above mentioned two operations should be very very fast.

Thanks,
Harish

svick
  • 236,525
  • 50
  • 385
  • 514
Harish
  • 7,589
  • 10
  • 36
  • 47

2 Answers2

2

I would create a hash-map based on the tuple (Name, Option). For each of the combinations of those two, there could be a simple list, ordered by (StartNum, EndNum). Inserting into that list would be O(N) in its size, but you know that size is quite small. Another option would be some balanced binary search tree or a skip list.

If you had a good hash (which shouldn't be hard, the standard ones should work), the time complexity for retrieval would be O(1 + log(|StartNum| * |EndNum|)).

When searching as per 2., you check all values that have StartNum <= Number whether they have matching EndNum.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Hash map is what I would recommend as well – jyore Oct 11 '11 at 00:14
  • Thanks. I will try out this solution and do the benchmarking. Again for #2, I just came across interval tree. I can have maximum of 45*15=693 entries for each (name,option) combination. Do you suggest using any interval tree for this? Or any other DS for the start num & end num. The thing is the DS should be efficient for both #1 and #2 operations – Harish Oct 11 '11 at 03:25
0

A hash-map should serve your purposes pretty well. You just have to figure out how to create a good hash for the composite key. If you already have hashes for the individual items, you can combine these using a simple XOR.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • I haven't yet have hashes for individual items.. I will try to figure that out.. But how having a hashmap will help me for Operation #2 to be fast, as I don't have the exact key for #2 above scenario? – Harish Oct 10 '11 at 13:37
  • @Harish: Sorry, didn't read that right. No, a simple hash-table will not help you here. – Björn Pollex Oct 10 '11 at 13:39