18template <
typename T>
class table {
23 std::shared_ptr<node> ptr_right;
24 std::shared_ptr<node> ptr_left;
25 explicit node() : ptr_right(
nullptr), ptr_left(
nullptr), _idx(0) {}
29 std::shared_ptr<node> root;
30 std::shared_ptr<node> tail;
31 std::shared_ptr<node> _itr;
34 explicit table() noexcept : root(
nullptr), tail(
nullptr), _itr(
nullptr), _size(0) {}
35 table(
const table<T>& t) noexcept : root(t.root), tail(t.tail), _size(t._size) {}
37 size_t size() {
return this->_size; }
39 template <
typename... Args>
void push_front(Args&&... keys);
41 template <
typename... Args>
void push_back(Args&&... keys);
49 std::vector<T> vectorize();
57 T& operator[](
const size_t& index) {
58 assert(index < _size && index >= 0);
60 return this->root->info;
63 if (index == _size - 1) {
64 return this->tail->info;
67 if (_itr->_idx < index) {
68 while (_itr->_idx != index) {
69 _itr = _itr->ptr_right;
72 while (_itr->_idx != index) {
73 _itr = _itr->ptr_left;
80 table<T>& operator=(
const table<T>& t) {
81 if (
this == &t) [[unlikely]] {
89 friend std::ostream& operator<<(std::ostream& out,
const table<T>& t) {
90 if (t.root ==
nullptr) [[unlikely]] {
93 std::shared_ptr<node> tmp = t.root;
94 while (tmp !=
nullptr) {
95 out << tmp->info <<
" ";
103 void _check_update() {
104 if (this->root ==
nullptr) {
107 std::shared_ptr<node> tmp = this->root;
109 while (tmp !=
nullptr) {
111 tmp = tmp->ptr_right;
116template <
typename T>
template <
typename... Args>
void table<T>::push_front(Args&&... keys) {
117 auto _insert = [&](
const T&& key) ->
void {
118 std::shared_ptr<node> nn = std::make_shared<node>();
120 if (this->root ==
nullptr) [[unlikely]] {
128 std::shared_ptr<node> tmp = root;
131 this->root->ptr_right = tmp;
132 tmp->ptr_left = this->root;
136 (std::invoke(_insert, std::forward<Args>(keys)), ...);
140template <
typename T>
template <
typename... Args>
void table<T>::push_back(Args&&... keys) {
141 auto _insert = [&](
const T&&
key) ->
void {
142 std::shared_ptr<node> nn = std::make_shared<node>();
144 if (this->root ==
nullptr) [[unlikely]] {
152 std::shared_ptr<node> tmp = tail;
153 nn->_idx = tail->_idx + 1;
155 tmp->ptr_right = tail;
156 this->tail->ptr_left = tmp;
160 (std::invoke(_insert, std::forward<Args>(keys)), ...);
163template <
typename T>
void table<T>::pop_back() {
164 if (this->tail ==
nullptr) [[unlikely]] {
167 this->tail = this->tail->ptr_left;
172template <
typename T>
void table<T>::pop_front() {
173 if (this->root ==
nullptr) [[unlikely]] {
176 this->root = this->root->ptr_right;
182template <
typename T>
bool table<T>::empty() {
183 return this->_size == 0;
186template <
typename T> std::vector<T> table<T>::vectorize() {
187 if (this->root ==
nullptr) {
191 for (
auto it = this->begin(); it != this->end(); it++) {
192 vec.push_back(*(it));
197template <
typename T>
class table<T>::Iterator {
199 std::shared_ptr<node> curr_root;
202 explicit Iterator(
const std::shared_ptr<node>& ptr) noexcept : curr_root(ptr) {}
204 Iterator& operator=(std::shared_ptr<node> current) {
205 this->curr_root = current;
209 Iterator& operator++() {
211 curr_root = curr_root->ptr_right;
216 Iterator operator++(
int) {
222 Iterator& operator--() {
224 curr_root = curr_root->ptr_left;
229 Iterator operator--(
int) {
235 bool operator!=(
const Iterator& it) {
return curr_root != it.curr_root; }
237 T operator*() {
return curr_root->info; }
@ key
the parser read a key of a value in an object
Definition json.hpp:11716