89 lines
1.9 KiB
C++
89 lines
1.9 KiB
C++
#ifndef PYLIST_H
|
|
#define PYLIST_H
|
|
|
|
#include <cstddef>
|
|
#include <iostream>
|
|
#include <ostream>
|
|
#include <vector>
|
|
#include <unordered_set>
|
|
#include <memory>
|
|
#include <queue>
|
|
class pylist;
|
|
std::queue<std::shared_ptr<std::vector<pylist *> > > bucket_keeper;
|
|
std::queue<std::shared_ptr<pylist> > pylist_keeper;
|
|
class pylist {
|
|
private:
|
|
|
|
bool is_list;
|
|
int int_val;
|
|
std::vector<pylist *> *children_ptr;
|
|
static void Print(
|
|
std::ostream &os,
|
|
const pylist &ls,
|
|
std::unordered_set<std::vector<pylist *> *>st) {
|
|
os << '[';
|
|
if (st.find(ls.children_ptr) != st.end()) {
|
|
os << "...]";
|
|
return;
|
|
}
|
|
st.insert(ls.children_ptr);
|
|
for (int i = 0; i < ls.children_ptr->size(); i++) {
|
|
if (i > 0) os << ", ";
|
|
if (((*ls.children_ptr)[i])->is_list)
|
|
Print(os, *((*ls.children_ptr)[i]), st);
|
|
else os << ((*ls.children_ptr)[i])->int_val;
|
|
}
|
|
os << ']';
|
|
}
|
|
|
|
public:
|
|
|
|
// ~pylist() {}
|
|
|
|
pylist() {
|
|
is_list = true;
|
|
std::vector<pylist *> *ptr = new std::vector<pylist *>();
|
|
children_ptr = ptr;
|
|
bucket_keeper.emplace(ptr);
|
|
}
|
|
|
|
pylist(int v) {
|
|
is_list = false;
|
|
int_val = v;
|
|
}
|
|
|
|
// pylist(const pylist &x) {}
|
|
|
|
// pylist& operator=(const pylist &x) {
|
|
// return *this;
|
|
// }
|
|
|
|
void append(const pylist &x) {
|
|
pylist *ptr = new pylist(x);
|
|
children_ptr->emplace_back(ptr);
|
|
pylist_keeper.emplace(ptr);
|
|
}
|
|
|
|
pylist pop() {
|
|
pylist tmp = *((*(children_ptr))[children_ptr->size() - 1]);
|
|
children_ptr->pop_back();
|
|
return tmp;
|
|
}
|
|
|
|
pylist& operator[](size_t i) {
|
|
return *((*(children_ptr))[i]);
|
|
}
|
|
|
|
friend std::ostream& operator<<(std::ostream &os, const pylist &ls) {
|
|
std::unordered_set<std::vector<pylist *> *>
|
|
st;
|
|
if (ls.is_list)
|
|
Print(os, ls, st);
|
|
else os << ls.int_val;
|
|
return os;
|
|
}
|
|
|
|
operator int() const { return int_val; }
|
|
};
|
|
#endif // PYLIST_H
|