Need algorithm for fast storage and retrieval (search) of sets and subsets

前端 未结 5 795
遇见更好的自我
遇见更好的自我 2021-02-06 11:57

I need a way of storing sets of arbitrary size for fast query later on. I\'ll be needing to query the resulting data structure for subsets or sets that are already stored.

5条回答
  •  后悔当初
    2021-02-06 12:34

    How about having an inverse index built of hashes?

    Suppose you have your values int A, char B, bool C of different types. With std::hash (or any other hash function) you can create numeric hash values size_t Ah, Bh, Ch.

    Then you define a map that maps an index to a vector of pointers to the tuples

    std::map > mymap;
    

    or, if you can use global indices, just

    std::map > mymap;
    

    For retrieval by queries X and Y, you need to

    1. get hash value of the queries Xh and Yh
    2. get the corresponding "sets" out of mymap
    3. intersect the sets mymap[Xh] and mymap[Yh]

提交回复
热议问题