greg7mdp / parallel-hashmap

A family of header-only, very fast and memory-friendly hashmap and btree containers.
https://greg7mdp.github.io/parallel-hashmap/
Apache License 2.0
2.47k stars 234 forks source link

emplace does not work for non-moveable / non-copyable objects. #244

Closed StavrosMar closed 2 months ago

StavrosMar commented 2 months ago

the following snippet works with std::unordered_map. however when we switch the parallel-hashmap it won't compile because emplace method does not seem to support objects with deleted move/copy ctors and assignment operations.

Please add this feauture .

include

include

include

include

class OHLCPublisher() { } ;

class ohlccache { public: ohlccache(const std::string& id,const std::string& priceformulastr, const std::string& intervalstr,std::shared_ptr ohlcpub) { std::cout << " Called default A ctor\n";
}

ohlccache(const ohlccache&) = delete;
ohlccache& operator=(const ohlccache&) = delete;
ohlccache(ohlccache&&) = delete;
ohlccache& operator=(ohlccache&&) = delete;

};

int main() { std::unordered_map<std::string, ohlccache> m;

auto ptr = std::make_shared<OHLCPublisher>();
// uses pair's piecewise constructor
m.emplace(std::piecewise_construct,
          std::forward_as_tuple("c"),
          std::forward_as_tuple( ,ptr));
// an alternative is: m.try_emplace("c", 10, 'c');

for (const auto& p : m)
    std::cout << p.first << "\n";

}

greg7mdp commented 2 months ago

Yes, the flat maps require the keys and values to be movable (they need to be moved when the hash map resizes). However, the node hash maps will work fine with non-movable values.

StavrosMar commented 2 months ago

can you add support for the flat map?

greg7mdp commented 2 months ago

can you add support for the flat map?

Unfortunately no, it is not possible, because the values have to be moved in memory when the table is resized.