Skip to content

Instantly share code, notes, and snippets.

@takei-yuya

takei-yuya/CardinalityQueue.cpp Secret

Created Apr 28, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.