Skip to content

Instantly share code, notes, and snippets.

@takei-yuya
Created April 28, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takei-yuya/a44462b37418eab59d74995878fa3e6c to your computer and use it in GitHub Desktop.
Save takei-yuya/a44462b37418eab59d74995878fa3e6c to your computer and use it in GitHub Desktop.
template <typename T>
class CardinalityQueue {
public:
CardinalityQueue() : cardinality_(0) {}
void Push(const T& value) {
queue_.push(value);
if (map_[value]++ == 0) ++cardinality_;
}
void Pop() {
if (--map_[queue_.front()] == 0) --cardinality_;
queue_.pop();
}
// number of non-zero keys
size_t Cardinality() const { return cardinality_; }
private:
std::queue<T> queue_;
std::map<T, size_t> map_;
size_t cardinality_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment