Last active
May 13, 2022 07:42
-
-
Save hjroh0315/dd426cdcf0f79d808cdfc96e16f6ed52 to your computer and use it in GitHub Desktop.
PS/CP를 위한 원형 큐 클래스. Range-based for를 통해 BFS를 할 수 있습니다. std::queue와 같은 함수 기능을 제공합니다. || Circular buffer class for Problem Solving/Competitive programming. Can be used with a range-based for statement in BFS. Provides the same functionality with std::queue.
This file contains hidden or 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
| #include<exception> | |
| using namespace std; | |
| //////// BEGIN STRUCT DEFINITION //////// | |
| template<class T, size_t sz> | |
| struct circular_buffer | |
| { | |
| struct It | |
| { | |
| circular_buffer<T,sz>∥ | |
| size_t abspos; | |
| bool is_end; | |
| It&& operator++(int) | |
| {It pre=*this;abspos++;par.pop();return pre;} | |
| It& operator++() | |
| {abspos++;par.pop();return *this;} | |
| T& operator*() | |
| {return par.data[abspos%sz];} | |
| bool operator==(It r) | |
| { | |
| if(&r.par!=&par)return false; | |
| else if(is_end) return r.abspos==par.ed; | |
| else if(r.is_end) return abspos==par.ed; | |
| else return r.abspos==abspos; | |
| } | |
| }; | |
| using iterator=It; | |
| using const_iterator=const It; | |
| T data[sz]; | |
| size_t bg,ed; | |
| circular_buffer():bg(0),ed(0){} | |
| T& front() | |
| {return data[bg%sz];} | |
| void push(T&& val) | |
| { | |
| if(ed==bg+sz)throw out_of_range("Circular Buffer Full"s); | |
| else data[ed++]=val; | |
| } | |
| void pop() | |
| { | |
| if(ed==bg)throw out_of_range("Circular Buffer Empty"s); | |
| else bg++; | |
| } | |
| It begin() | |
| {return It{*this,bg,false};} | |
| It end() | |
| {return It{*this,ed,true};} | |
| size_t size() | |
| {return ed-bg;} | |
| bool empty() | |
| {return bg==ed;} | |
| void swap(circular_buffer<T,sz>&other) | |
| { | |
| swap(data,other.data); | |
| } | |
| }; | |
| //////// END STRUCT DEFINITION //////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment