Home > Software design >  Map with multiple sorts and sets (like index into the map by not the [] kind)
Map with multiple sorts and sets (like index into the map by not the [] kind)

Time:10-17

I have a large map of records and I need to access subsets of the map in an order different from the keys (kind of like an index into a database). Say the map is lots of people and there is a list of all those people who are students (large subset). One of the fields is height in inches and the other weight in lbs. Given a student I want to find the next shortest and next heaviest. I currently have mini-maps mapping the key to the next and prev keys based on different fields. I'm trying to think of something better.

map<string, bigRecord> mainMap; vector students;

mainMap:

  • "Eric", 73, 174, student
  • "Greg", 71, 176, student
  • "Paul", 70, 173, notStudent
  • "Pete", 72, 175, student

students: "Eric", "Greg", Pete"

map<string, twoStringRecordPrevNext>

studentHeightMap:

  • "Eric", prev="Pete", next=""
  • "Greg", prev="", next="Pete"
  • "Pete", prev="Greg", next="Eric"

studentWieghtMap:

  • "Eric", prev="", next="Pete"
  • "Greg", prev="Pete", next=""
  • "Pete", prev="Eric", next="Greg"

Once the mainMap is set (updates invalidate lists) I create the student mini-maps and can then find the next/prev easily given a key. It's just a lot of maps to maintain.

Using a SQL database doesn't work well because there are thousands of sets (i.e. students is the set in this example) and millions of records in mainMap.

Any ideas on better approaches?

CodePudding user response:

After a break and coming back to it, I figured the "prev/next" thing was the bad part. I just need a studentHeightVector to go with the mini-map. Now, given "Greg" I know that's the first item in the vector and can find the next tallest by incrementing and the 2nd element is "Pete" which I can look up in the mainMap.

studentHeightMap:

  • "Eric", 3
  • "Greg", 1
  • "Pete", 2

studentHeightVector:

  1. "Greg"
  2. "Pete"
  3. "Eric"
  • Related