-
-
Save takei-yuya/a44462b37418eab59d74995878fa3e6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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