Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- //######### as a word of caution to anyone who would attempt to use this, this code does contain memory leaks..... ###########
- using namespace std;
- class bitlist {
- public:
- unsigned long long size;
- unsigned long long bytecount;
- byte* masterlist;
- unsigned long long index;
- bitlist() :index{ 0 }, masterlist{ new byte[1]{0} }, size{ 0 }, bytecount{ 1 } {}
- ~bitlist() {
- if (masterlist) {
- delete[] masterlist;
- }
- }
- void Realloc(unsigned long long ull) {
- size_t newb = ull % 8 ? (ull >> 3) + 2 : (ull >> 3)+1;
- byte* m = new byte[newb]{};
- memcpy(m, masterlist, bytecount);
- delete[] masterlist;
- masterlist = m;
- bytecount = newb ;
- size = ull;
- }
- bitlist& operator[](unsigned long long ull) {
- if (ull > bytecount<<3) { Realloc(ull + 1); }
- else if (ull > size) { size = ull + 1; }
- index = ull;
- return *this;
- }
- bool operator==(bool b) {
- return check(masterlist[index >> 3]);
- }
- bool operator!=(bool b) {
- return !check(masterlist[index >> 3]);
- }
- void operator=(bool b) {
- set(masterlist[index>>3], b);
- }
- bool check(const byte& b) { // bitwise intensifies
- if (b & 1 << (index % 8)) { return true; }
- return false;
- }
- void set(byte& b, bool a) {
- if (a) {
- b |= 1 << (index % 8);
- }
- else if (check(b)){
- b -= 1 << (index % 8);
- }
- }
- void print() {
- for (auto i = 0; i < size; i++) {
- index = i;
- cout << check(masterlist[index>>3]) << " ";
- }
- }
- void printNumbers() {
- for (auto i = 0; i < size; i++) {
- index = i;
- if(check(masterlist[index >> 3]) )
- cout << index<< " ";
- }
- }
- };
- class node {
- public:
- node* prev;
- node* current;
- node* next;
- unsigned long long root;
- unsigned long long num;
- node(unsigned long long num = 0, unsigned long long root = 0, node* prev = nullptr, node* next = nullptr) : current{ nullptr },root { root }, prev{ prev }, next{ next }, num{ num }{}
- node(const node& n) :num{ n.num }, root{ n.root }, prev{ n.prev }, next{ n.next }, current{ n.current }{}
- void copy(const node& other) {
- num = other.num; root = other.root; prev = other.prev; next = other.next;
- }
- void operator=(const node& other) {
- copy(other);
- }
- };
- class nodeIT {
- public:
- node* n;
- nodeIT() :n{ new node } {
- }
- nodeIT(nodeIT& other) :n{ nullptr } {
- n = other.n;
- other.n = nullptr;
- }
- ~nodeIT() {
- if (n) {
- delete n;
- }
- }
- void operator=(nodeIT& other) {
- copy(other);
- }
- void operator=(const node& node) {
- copy(node);
- }
- void copy(nodeIT& other) {
- *this = other; //iffy
- }
- void copy(const node& node) {
- *n = node;
- }
- };
- unsigned long long xPlusOne(unsigned long long ull) {
- if (ull & 1) { return (ull * 3) + 1; } //0dd
- return ull >> 1; //bitwise operations... beastmode unlocked
- }
- class tree {
- public:
- nodeIT* t;
- size_t size;
- size_t capacity;
- tree() :t{ new nodeIT[10] }, size{ 0 }, capacity{ 0 } {}
- ~tree() {
- if (t) {
- //delete t; throwing
- }
- }
- void Realloc(size_t s) {
- nodeIT* n = new nodeIT[s];
- memcpy(n, t, capacity*sizeof(nodeIT));
- //delete t; //throwing
- t = n;
- capacity = s;
- }
- void push_back(node n) {
- if (size + 1 > capacity) {
- Realloc(capacity + 10);
- //cout << " re \n";
- }
- *t[size].n = n;
- size++;
- }
- node* back() {
- return t[size - 1].n;
- }
- size_t Size() {return size;}
- node* operator[](size_t index) {
- return t[index].n;
- }
- node* FindNodeByVal(unsigned long long ull) {
- for (auto i = 0; i < size; i++) {
- if (t[i].n->num == ull) {
- return t[i].n;
- }
- }
- return nullptr;
- }
- node* FindNodeRoot(unsigned long long ull) {
- for (auto i = 0; i < size; i++) {
- if (!t[i].n->prev && t[i].n->root == ull) {
- return t[i].n;
- }
- }
- return nullptr;
- }
- void printBranch(unsigned long long ull) {
- if (!ull) { return; }
- node* n = FindNodeRoot(ull);
- if (!n) { return; }
- node* next = n->next;
- cout << " root= " << n->root << " num = " << n->num << " prev = " << n->prev << " current = " << n << " next = " << n->next << "\n";
- while(next->num!=1){
- cout << " root= " << next->root << " num = " << next->num << " prev = " << next->prev << " current = " << next << " next = " << next->next << "\n";
- next = next->next;//...
- }
- n = next;
- cout << " root= " << n->root << " num = " << n->num << " prev = " << n->prev << " current = " << n << " next = " << n->next << "\n";
- }
- void printTree() {
- for (auto i = 0; i < branchCount()+1; i++) {
- printBranch(i);
- cout << "\n";
- }
- }
- unsigned long long branchCount() {
- return back()->root;
- }
- };
- int main() {
- unsigned long long in{};
- bitlist bl;
- tree t;
- for (unsigned long long i = 1; i < 20; i++) {
- in = i;
- t.push_back({ i,i });
- bl[in] = true;
- while (true) {
- in = xPlusOne(in); //plucks a number
- if (bl[in] == true) { //if the number is already in the tree it is disregarded
- t.back()->next = t.FindNodeByVal(in); // attaches last node in current branch to branch that has already found the end
- break;
- }
- t.push_back({ in,i,t.back()}); //new branch node is made
- t[t.Size() - 2]->next = t.back();// attach new node to the last
- bl[in] = true; // updates the list of which numbers have been touched already.
- }
- }
- t.printTree();
- }
Advertisement
Add Comment
Please, Sign In to add comment