sobinist

LinkedList.ElementPointerFirst

Jul 13th, 2020
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using Ray2Mod.Utils;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace Ray2Mod.Game.Structs.LinkedLists {
  6.  
  7.     public abstract unsafe partial class LinkedList {
  8.  
  9.         [StructLayout(LayoutKind.Sequential)]
  10.         public unsafe struct ListElement_EPF<T> where T : unmanaged {
  11.             public T Element;
  12.             public ListElement_EPF<T>* Next;
  13.             public ListElement_EPF<T>* Previous;
  14.             // No Header Pointers
  15.         }
  16.  
  17.         [StructLayout(LayoutKind.Sequential)]
  18.         public unsafe struct ElementPointerFirst<T> where T : unmanaged {
  19.             public ListElement_EPF<T>* Head;
  20.             public ListElement_EPF<T>* Tail;
  21.             public int Count;
  22.  
  23.             public unsafe ListElement_EPF<T>*[] Read()
  24.             {
  25.                 ListElement_EPF<T>*[] results = new ListElement_EPF<T>*[Count];
  26.  
  27.                 ListElement_EPF<T>* Next = Head;
  28.  
  29.                 for (int i = 0; i < Count; i++) {
  30.                     if (Next == null) {
  31.                         break;
  32.                     }
  33.  
  34.                     ListElement_EPF<T>* LinkedListElement = Next;
  35.                     Next = LinkedListElement->Next;
  36.  
  37.                     results[i] = LinkedListElement;
  38.                 }
  39.  
  40.                 return results;
  41.             }
  42.  
  43.             public void Write(T[] items)
  44.             {
  45.                 ListElement_EPF<T>*[] elements = new ListElement_EPF<T>*[items.Length];
  46.  
  47.                 Count = elements.Length;
  48.  
  49.                 for (int i = 0; i < Count; i++) {
  50.                     ListElement_EPF<T>* NewElement = new ListElement_EPF<T>()
  51.                     {
  52.                         Element = items[i],
  53.                         Previous = null,
  54.                         Next = null,
  55.                     }.ToUnmanaged();
  56.  
  57.                     if (i == 0) {
  58.                         Head = NewElement;
  59.                     } else if (i == Count - 1) {
  60.                         Tail = NewElement;
  61.                     }
  62.  
  63.                     // No Header Pointers
  64.  
  65.                     elements[i] = NewElement;
  66.                 }
  67.  
  68.                 // Set Next and Previous
  69.                 for (int i = 0; i < Count; i++) {
  70.                     if (i > 0) { elements[i]->Previous = elements[i - 1]; }
  71.                     if (i < Count - 1) { elements[i]->Next = elements[i + 1]; }
  72.                 }
  73.             }
  74.  
  75.             public void Write(ListElement_EPF<T>*[] items)
  76.             {
  77.                 Count = items.Length;
  78.  
  79.                 for (int i = 0; i < Count; i++) {
  80.  
  81.                     if (i == 0) {
  82.                         Head = items[i];
  83.                     } else if (i == Count - 1) {
  84.                         Tail = items[i];
  85.                     }
  86.                 }
  87.  
  88.                 // Set Next and Previous
  89.                 for (int i = 0; i < Count; i++) {
  90.                     if (i > 0) { items[i]->Previous = items[i - 1]; }
  91.                     if (i < Count - 1) { items[i]->Next = items[i + 1]; }
  92.                 }
  93.             }
  94.  
  95.             /// <summary>
  96.             /// Adds a new item to the list
  97.             /// </summary>
  98.             /// <param name="item">The item to add</param>
  99.             /// <returns>A pointer to the newly created list element</returns>
  100.             public ListElement_EPF<T>* Add(T item)
  101.             {
  102.                 var items = Read();
  103.  
  104.                 var newItem = new ListElement_EPF<T>()
  105.                 {
  106.                     Next = null,
  107.                     Previous = items[items.Length - 1],
  108.                     // No Header
  109.                     Element = item
  110.                 }.ToUnmanaged();
  111.  
  112.                 items[items.Length - 1]->Next = newItem;
  113.  
  114.                 Count++;
  115.  
  116.                 return newItem;
  117.             }
  118.  
  119.             /// <summary>
  120.             /// Removes the first occurrence of an item from the list
  121.             /// </summary>
  122.             /// <param name="item">The item to remove</param>
  123.             /// <returns>True if the item was found and removed, false otherwise</returns>
  124.             public bool Remove(ListElement_EPF<T>* item)
  125.             {
  126.                 var items = Read();
  127.  
  128.                 for (int i = 0; i < items.Length; i++) {
  129.                     if (items[i] == item) {
  130.                         if (i + 1 < items.Length) {
  131.                             items[i + 1]->Previous = i - 1 >= 0 ? items[i - 1] : null;
  132.                         }
  133.  
  134.                         if (i - 1 >= 0) {
  135.                             items[i - 1]->Next = i + 1 < items.Length ? items[i + 1] : null;
  136.                         }
  137.  
  138.                         Count--;
  139.                         return true;
  140.                     }
  141.                 }
  142.  
  143.                 return false;
  144.             }
  145.         }
  146.     }
  147. }
Add Comment
Please, Sign In to add comment