Advertisement
imk0tter
Mar 31st, 2023
79
0
Never
This is comment for paste LiTHiuM List
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Primative_List.h"
  2.  
  3. #include <iostream>
  4.  
  5. Primative_List::Primative_List() {
  6.   this->list_ptr = (long)malloc(sizeof(long) * 2);
  7.   ((long *)list_ptr)[0] = this->list_ptr;
  8.   ((long *)list_ptr)[1] = 0;
  9. }
  10.  
  11. long Primative_List::Count() { return ((long *)this->list_ptr)[1]; }
  12.  
  13. long Primative_List::GetEntry(long index) {
  14.   // TODO: Get Current List Object without iterating; dynamic number of
  15.   // dereference brackets frog code
  16.  
  17.   //TODO: index = (index + this.Count()) % this->Count();
  18.   if (index < 0) index = this->Count() + index;
  19.  
  20.   long current_list_entry_ptr = this->list_ptr;
  21.   for (int i = 0; i <= index; ++i) {
  22.     current_list_entry_ptr = ((long *)current_list_entry_ptr)[0];
  23.   }
  24.   return current_list_entry_ptr;
  25. }
  26.  
  27. void Primative_List::Remove(long index) {
  28.   long list_count = ((long *)this->list_ptr)[1];
  29.  
  30.   if (index == 0) {
  31.     long current_list_entry_ptr = ((long *)this->list_ptr)[0];
  32.     ((long *)this->list_ptr)[0] = ((long *)current_list_entry_ptr)[0];
  33.     --((long *)this->list_ptr)[1];
  34.     free((long *)current_list_entry_ptr);
  35.   } else {
  36.     long current_list_entry_ptr = this->GetEntry(index);
  37.  
  38.     if (index <= list_count - 1) {
  39.       long previous_list_entry_ptr = this->GetEntry(index - 1);
  40.       long next_list_entry_ptr = this->GetEntry(index + 1);
  41.  
  42.       ((long *)previous_list_entry_ptr)[0] = next_list_entry_ptr;
  43.       --((long *)this->list_ptr)[1];
  44.       free((long *)current_list_entry_ptr);
  45.     } else {
  46.       --((long *)this->list_ptr)[1];
  47.       free((long *)current_list_entry_ptr);
  48.     }
  49.   }
  50. }
  51.  
  52. void Primative_List::Delete() {
  53.   // TODO: Delete all list objects at same time with frog code
  54.   long list_count = ((long *)this->list_ptr)[1];
  55.   long next_list_entry_ptr = this->list_ptr;
  56.   while (this->Count() > 0) {
  57.     this->Remove(0);
  58.   }
  59.   free((long *)this->list_ptr);
  60.   delete this;
  61. }
  62.  
  63. long Primative_List::Add(long obj_ptr) {
  64.   long list_count = ((long *)this->list_ptr)[1];
  65.  
  66.   long current_list_entry_ptr = (long)malloc(sizeof(long) * 2);
  67.  
  68.   long last_list_entry_ptr = this->GetEntry(list_count - 1);
  69.   ((long *)last_list_entry_ptr)[0] = current_list_entry_ptr;
  70.   ((long *)current_list_entry_ptr)[1] = obj_ptr;
  71.   ((long *)current_list_entry_ptr)[0] = this->list_ptr;
  72.  
  73.   long count = ((long *)this->list_ptr)[1]++;
  74.  
  75.   return count;
  76. }
  77.  
  78. void Primative_List::Insert(long index, long obj_ptr) {
  79.  
  80.   long new_list_entry_ptr = (long)malloc(sizeof(long) * 2);
  81.  
  82.   ((long *)new_list_entry_ptr)[1] = obj_ptr;
  83.   if (index == 0) {
  84.     ((long *)new_list_entry_ptr)[0] = ((long *)this->list_ptr)[0];
  85.     ((long *)this->list_ptr)[0] = new_list_entry_ptr;
  86.   } else {
  87.     long list_count = ((long *)this->list_ptr)[1];
  88.  
  89.     if (index < list_count) {
  90.       long previous_object_entry_ptr = this->GetEntry(index - 1);
  91.       long current_object_entry_ptr = this->GetEntry(index);
  92.  
  93.       ((long *)new_list_entry_ptr)[0] = current_object_entry_ptr;
  94.       ((long *)new_list_entry_ptr)[1] = obj_ptr;
  95.  
  96.       ((long *)previous_object_entry_ptr)[0] = new_list_entry_ptr;
  97.  
  98.     } else {
  99.       long last_entry_in_list_ptr = this->GetEntry(list_count - 1);
  100.       long previous_entry_in_list_ptr = this->GetEntry(list_count - 2);
  101.       ((long *)new_list_entry_ptr)[0] = last_entry_in_list_ptr;
  102.       ((long *)last_entry_in_list_ptr)[0] = this->list_ptr;
  103.       ((long *)previous_entry_in_list_ptr)[0] = new_list_entry_ptr;
  104.     }
  105.   }
  106.  
  107.   ++((long *)this->list_ptr)[1];
  108. }
  109.  
  110. long Primative_List::Get(long index) {
  111.   return ((long *)this->GetEntry(index))[1];
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement