Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <thread>
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <random>
- #ifdef _WIN32
- #include <Windows.h>
- #else
- #include <unistd.h>
- #endif
- #include "LinkedList.h"
- static std::shared_ptr<Node> head = nullptr;
- static std::shared_ptr<Node> tail = nullptr;
- static bool run_loop = true;
- std::shared_ptr<Node> RemoveNode(std::shared_ptr<Node> remNode) {
- remNode->data->ShowName();
- // The one behind me should link to my next value
- if (remNode->prev) {
- remNode->prev->next = remNode->next;
- }
- else { // Handle head behaviour if front of list
- if (head == remNode) {
- if (remNode->next) {
- head = remNode->next;
- }
- else {
- head = nullptr;
- }
- }
- }
- // My next value should link to the value behind me
- if (remNode->next) {
- remNode->next->prev = remNode->prev;
- }
- else { // Handle tail behaviour if end of list
- if (tail == remNode) {
- if (remNode->prev) {
- tail = remNode->prev;
- }
- else {
- tail = nullptr;
- }
- }
- }
- // Store next step;
- std::shared_ptr<Node> tmp = remNode->next;
- // Clear up my values;
- remNode->next = nullptr;
- remNode->prev = nullptr;
- remNode->data = nullptr;
- // No one should reference me,
- // and I don't reference anyone now
- // so replace to finish cleanup
- return tmp;
- }
- void loop() {
- while (run_loop) {
- // Start Fixed Actions
- // Dynamic Actions
- std::shared_ptr<Node> loop_ptr = head;
- while (loop_ptr) {
- if (loop_ptr->remove) {
- loop_ptr = RemoveNode(loop_ptr);
- continue;
- }
- loop_ptr->data->Act();
- if (loop_ptr->temporary) {
- loop_ptr = RemoveNode(loop_ptr);
- continue;
- }
- loop_ptr = loop_ptr->next;
- }
- // End Fixed Actions
- // std::cout << ".\n";
- Sleep(250);
- }
- }
- void AddNode(std::shared_ptr<Node> newNode) {
- // Initial Node
- if (head == tail && tail == nullptr) {
- head = tail = newNode;
- return;
- }
- // Invalid State
- if ((head == nullptr && tail != nullptr) || (head != nullptr && tail == nullptr)) {
- throw ERROR_INVALID_STATE;
- }
- // Add to Tail
- newNode->prev = tail; // Before me is current tail
- tail->next = newNode; // Current tail leads to me
- tail = newNode; // Making me new tail
- return;
- }
- static std::set<int> temp_keys;
- static std::set<int> assigned_keys;
- static std::map<int, std::shared_ptr<Node>> active_nodes;
- int main(int argc, char* argv) {
- std::thread thread_obj(loop);
- while (true) {
- for (auto const& N : active_nodes) {
- std::cerr << "( " << N.first << " : " << N.second << " )\t";
- }
- if (active_nodes.size() > 0) {
- std::cerr << std::endl;
- }
- int choice = rand() % 4;
- if (choice < 2) {
- // Get next available key
- int key = 0;
- while ((assigned_keys.find(key) != assigned_keys.end()) || (temp_keys.find(key) != temp_keys.end())) {
- key++;
- }
- int temp = rand() % 3;
- if (temp == 1) {
- temp = 0;
- }
- std::shared_ptr<Node> tmpNode = std::make_shared<Node>();
- tmpNode->data = std::make_shared<PrintMessage>((temp ? "T\t" : "C\t") + std::to_string(key));
- tmpNode->temporary = temp;
- AddNode(tmpNode);
- if (temp) {
- temp_keys.insert(key);
- }
- else {
- assigned_keys.insert(key);
- active_nodes.insert(std::pair<int, std::shared_ptr<Node>>(key, tmpNode));
- }
- fprintf(stderr, "Add\t%i\n", key);
- }
- else if (choice < 3) {
- if (assigned_keys.size() == 0) continue;
- int max_val = *assigned_keys.rbegin();
- int key = std::rand() % (max_val+1);
- if (assigned_keys.find(key) != assigned_keys.end()) {
- active_nodes.at(key)->remove = true;
- assigned_keys.erase(key);
- active_nodes.erase(key);
- fprintf(stderr, "Rem\t%i\n", key);
- }
- }
- }
- /*
- // Constant Node (No IO)
- std::shared_ptr<Node> Constant = std::make_shared<Node>();
- Constant->data = std::make_shared<PrintMessage>("Constant");
- AddNode(Constant);
- Sleep(25);
- // Constant Node (IO)
- std::shared_ptr<int> MyCounter = std::make_shared<int>(0);
- std::shared_ptr<Node> IONode = std::make_shared<Node>();
- IONode->data = std::make_shared<CountLoops>(MyCounter);
- AddNode(IONode);
- Sleep(50);
- // Temporary Node (No IO)
- std::shared_ptr<Node> TmpNode = std::make_shared<Node>();
- TmpNode->temporary = true;
- TmpNode->data = std::make_shared<PrintMessage>("OnceOff!");
- AddNode(TmpNode);
- Sleep(500);
- Constant->remove = true;
- Sleep(500);
- */
- run_loop = false;
- thread_obj.join();
- // std::cout << *MyCounter << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement