Advertisement
AllWeather

Untitled

Apr 14th, 2021
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <memory>
  2. #include <thread>
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <random>
  8.  
  9. #ifdef _WIN32
  10. #include <Windows.h>
  11. #else
  12. #include <unistd.h>
  13. #endif
  14.  
  15. #include "LinkedList.h"
  16.  
  17.  
  18. static std::shared_ptr<Node> head = nullptr;
  19. static std::shared_ptr<Node> tail = nullptr;
  20. static bool run_loop = true;
  21.  
  22. std::shared_ptr<Node> RemoveNode(std::shared_ptr<Node> remNode) {
  23.     remNode->data->ShowName();
  24.  
  25.     // The one behind me should link to my next value
  26.     if (remNode->prev) {
  27.         remNode->prev->next = remNode->next;
  28.     }
  29.     else { // Handle head behaviour if front of list
  30.         if (head == remNode) {
  31.             if (remNode->next) {
  32.                 head = remNode->next;
  33.             }
  34.             else {
  35.                 head = nullptr;
  36.             }
  37.         }
  38.     }
  39.  
  40.     // My next value should link to the value behind me
  41.     if (remNode->next) {
  42.         remNode->next->prev = remNode->prev;
  43.     }
  44.     else { // Handle tail behaviour if end of list
  45.         if (tail == remNode) {
  46.             if (remNode->prev) {
  47.                 tail = remNode->prev;
  48.             }
  49.             else {
  50.                 tail = nullptr;
  51.             }
  52.         }
  53.     }
  54.  
  55.     // Store next step;
  56.     std::shared_ptr<Node> tmp = remNode->next;
  57.  
  58.     // Clear up my values;
  59.     remNode->next = nullptr;
  60.     remNode->prev = nullptr;
  61.     remNode->data = nullptr;
  62.  
  63.     // No one should reference me,
  64.     // and I don't reference anyone now
  65.     // so replace to finish cleanup
  66.     return tmp;
  67. }
  68. void loop() {
  69.     while (run_loop) {
  70.         // Start Fixed Actions
  71.  
  72.         // Dynamic Actions
  73.         std::shared_ptr<Node> loop_ptr = head;
  74.         while (loop_ptr) {
  75.             if (loop_ptr->remove) {
  76.                 loop_ptr = RemoveNode(loop_ptr);
  77.                 continue;
  78.             }
  79.  
  80.             loop_ptr->data->Act();
  81.            
  82.             if (loop_ptr->temporary) {
  83.                 loop_ptr = RemoveNode(loop_ptr);
  84.                 continue;
  85.             }
  86.  
  87.             loop_ptr = loop_ptr->next;
  88.         }
  89.  
  90.         // End Fixed Actions
  91.         // std::cout << ".\n";
  92.  
  93.         Sleep(250);
  94.     }
  95. }
  96.  
  97. void AddNode(std::shared_ptr<Node> newNode) {
  98.     // Initial Node
  99.     if (head == tail && tail == nullptr) {
  100.         head = tail = newNode;
  101.         return;
  102.     }
  103.  
  104.     // Invalid State
  105.     if ((head == nullptr && tail != nullptr) || (head != nullptr && tail == nullptr)) {
  106.         throw ERROR_INVALID_STATE;
  107.     }
  108.  
  109.     // Add to Tail
  110.     newNode->prev = tail;   // Before me is current tail
  111.     tail->next = newNode;   // Current tail leads to me
  112.     tail = newNode;         // Making me new tail
  113.     return;
  114. }
  115.  
  116. static std::set<int> temp_keys;
  117. static std::set<int> assigned_keys;
  118. static std::map<int, std::shared_ptr<Node>> active_nodes;
  119.  
  120. int main(int argc, char* argv) {
  121.     std::thread thread_obj(loop);
  122.  
  123.     while (true) {
  124.  
  125.         for (auto const& N : active_nodes) {
  126.             std::cerr << "( " << N.first << " : " << N.second << " )\t";
  127.         }
  128.         if (active_nodes.size() > 0) {
  129.             std::cerr << std::endl;
  130.         }
  131.        
  132.        
  133.         int choice = rand() % 4;
  134.  
  135.         if (choice < 2) {
  136.             // Get next available key
  137.             int key = 0;
  138.             while ((assigned_keys.find(key) != assigned_keys.end()) || (temp_keys.find(key) != temp_keys.end())) {
  139.                 key++;
  140.             }
  141.  
  142.             int temp = rand() % 3;
  143.  
  144.             if (temp == 1) {
  145.                 temp = 0;
  146.             }
  147.  
  148.             std::shared_ptr<Node> tmpNode = std::make_shared<Node>();
  149.             tmpNode->data = std::make_shared<PrintMessage>((temp ? "T\t" : "C\t") + std::to_string(key));
  150.             tmpNode->temporary = temp;
  151.             AddNode(tmpNode);
  152.  
  153.             if (temp) {
  154.                 temp_keys.insert(key);
  155.             }
  156.             else {
  157.                 assigned_keys.insert(key);
  158.                 active_nodes.insert(std::pair<int, std::shared_ptr<Node>>(key, tmpNode));
  159.             }
  160.  
  161.             fprintf(stderr, "Add\t%i\n", key);
  162.         }
  163.         else if (choice < 3) {
  164.             if (assigned_keys.size() == 0) continue;
  165.             int max_val = *assigned_keys.rbegin();
  166.            
  167.             int key = std::rand() % (max_val+1);
  168.  
  169.             if (assigned_keys.find(key) != assigned_keys.end()) {
  170.                 active_nodes.at(key)->remove = true;
  171.                 assigned_keys.erase(key);
  172.                 active_nodes.erase(key);
  173.                 fprintf(stderr, "Rem\t%i\n", key);
  174.             }
  175.         }
  176.     }
  177.    
  178.     /*
  179.     // Constant Node (No IO)
  180.     std::shared_ptr<Node> Constant = std::make_shared<Node>();
  181.     Constant->data = std::make_shared<PrintMessage>("Constant");
  182.     AddNode(Constant);
  183.  
  184.     Sleep(25);
  185.    
  186.     // Constant Node (IO)
  187.     std::shared_ptr<int> MyCounter = std::make_shared<int>(0);
  188.     std::shared_ptr<Node> IONode = std::make_shared<Node>();
  189.     IONode->data = std::make_shared<CountLoops>(MyCounter);
  190.     AddNode(IONode);
  191.    
  192.     Sleep(50);
  193.  
  194.     // Temporary Node (No IO)
  195.     std::shared_ptr<Node> TmpNode = std::make_shared<Node>();
  196.     TmpNode->temporary = true;
  197.     TmpNode->data = std::make_shared<PrintMessage>("OnceOff!");
  198.     AddNode(TmpNode);
  199.  
  200.     Sleep(500);
  201.  
  202.     Constant->remove = true;
  203.  
  204.     Sleep(500);
  205.     */
  206.  
  207.     run_loop = false;
  208.     thread_obj.join();
  209.  
  210.     // std::cout << *MyCounter << std::endl;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement