Advertisement
imk0tter

Untitled

Aug 2nd, 2021
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <vector>
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. namespace SMObjects {
  10.     template<typename VALUE>
  11.     class List {
  12.  
  13.  
  14.         int                         m_MaxChunkSize;
  15.         int                         m_CurrentChunkSize;
  16.  
  17.         int                         m_EntryCount;
  18.  
  19.         bool                        _CreateChunk();
  20.         VALUE*                      m_ChunkArrayStart;
  21.         VALUE*                      m_ChunkArrayEnd;
  22.     public:
  23.         int                         DEFAULT_CHUNK_COUNT;
  24.  
  25.                                     List(int DEFAULT_CHUNK_COUNT);
  26.                                     List();
  27.  
  28.                                     ~List();
  29.  
  30.                                     int                         Count();
  31.         void                        Add(VALUE value);
  32.  
  33.         bool                        RemoveAt(int index);
  34.         bool                        Remove(VALUE value, bool all = false);
  35.  
  36.         bool                        Insert(VALUE value, int index);
  37.  
  38.         int                         Find(VALUE value, int pos = 0, int match = 1);
  39.         vector<int>                 FindAll(VALUE value, int pos = 0);
  40.  
  41.         VALUE                       Get(int index);
  42.         //vector<VALUE*>                Get();
  43.  
  44.         void                        Clear();
  45.        
  46.  
  47.         VALUE&                      operator[] (int index);
  48.  
  49.         VALUE&                      operator<<(VALUE& value);
  50.         VALUE&                      operator>>(VALUE& value);
  51.  
  52.         VALUE                       operator* () { return *m_ChunkArrayStart; }
  53.         void                        operator++ () { ++m_ChunkArrayStart; }
  54.  
  55.         VALUE*                      begin() { return m_ChunkArrayStart; }
  56.         VALUE*                      end() { return m_ChunkArrayStart + m_EntryCount; }
  57.     };
  58.  
  59.     /// <summary>
  60.     /// Class 'List' Implementation
  61.     /// </summary>
  62.     /// <typeparam name="VALUE"></typeparam>
  63.     /// <param name="DEFAULT_CHUNK_COUNT"></param>
  64.     template<typename VALUE>
  65.     List<VALUE>::List(int DEFAULT_CHUNK_COUNT)
  66.     {
  67.         this->DEFAULT_CHUNK_COUNT = DEFAULT_CHUNK_COUNT;
  68.         _CreateChunk();
  69.     }
  70.  
  71.     template<typename VALUE>
  72.     List<VALUE>::List() : List(128)
  73.     {
  74.     }
  75.  
  76.     template<typename VALUE>
  77.     int List<VALUE>::Count()
  78.     {
  79.         return m_EntryCount;
  80.     }
  81.  
  82.     template<typename VALUE>
  83.     bool List<VALUE>::_CreateChunk()
  84.     {
  85.         if (m_EntryCount >= m_CurrentChunkSize)
  86.         {
  87.             m_CurrentChunkSize += DEFAULT_CHUNK_COUNT;
  88.  
  89.             if (m_CurrentChunkSize > m_MaxChunkSize)
  90.             {
  91.                 VALUE* newChunkArray = new VALUE[m_CurrentChunkSize];
  92.  
  93.                 if (m_EntryCount > 0)
  94.                 {
  95.                     memcpy(newChunkArray, m_ChunkArrayStart, sizeof(VALUE) * m_EntryCount);
  96.                     delete[] m_ChunkArrayStart;
  97.                 }
  98.                 m_ChunkArrayStart = newChunkArray;
  99.                 m_ChunkArrayEnd = m_ChunkArrayStart + m_CurrentChunkSize;
  100.                 return true;
  101.             }
  102.         }
  103.         return false;
  104.     }
  105.  
  106.     template<typename VALUE>
  107.     List<VALUE>::~List()
  108.     {
  109.         delete[] m_ChunkArrayStart;
  110.     }
  111.  
  112.     template<typename VALUE>
  113.     VALUE& List<VALUE>::operator[] (int index)
  114.     {
  115.         if (index >= 0 && index < m_EntryCount)
  116.         return m_ChunkArrayStart[index];
  117.     }
  118.  
  119.  
  120.     template<typename VALUE>
  121.     VALUE& List<VALUE>::operator<<(VALUE& value)
  122.     {
  123.         return value;
  124.     }
  125.  
  126.     template<typename VALUE>
  127.     VALUE& List<VALUE>::operator>>(VALUE& value)
  128.     {
  129.         return value;
  130.     }
  131.  
  132.  
  133.     template<typename VALUE>
  134.     bool List<VALUE>::Insert(VALUE value, int index)
  135.     {
  136.         if (index < m_EntryCount)
  137.         {
  138.             ++m_EntryCount;
  139.             _CreateChunk();
  140.  
  141.             VALUE* start = m_ChunkArrayStart + index;
  142.  
  143.             int size = (m_ChunkArrayEnd - start - 1) * sizeof(VALUE);
  144.  
  145.             if (size > 0) memcpy(start + 1, start, size);
  146.  
  147.             m_ChunkArrayStart[index] = value;
  148.  
  149.             return true;
  150.         }
  151.         return false;
  152.     }
  153.     template<typename VALUE>
  154.     void List<VALUE>::Add(VALUE value)
  155.     {
  156.         int index = m_EntryCount++;
  157.  
  158.         if (_CreateChunk())
  159.         {
  160.             //DEBUG NEW CHUNK CREATION
  161.         }
  162.  
  163.         m_ChunkArrayStart[index] = value;
  164.     }
  165.  
  166.     template<typename VALUE>
  167.     bool List<VALUE>::RemoveAt(int index)
  168.     {
  169.         if (index < m_EntryCount)
  170.         {
  171.             VALUE* _begin = m_ChunkArrayStart + index;
  172.             VALUE* _start = m_ChunkArrayStart + index + 1;
  173.             int size = (m_ChunkArrayEnd - _start) * sizeof(VALUE);
  174.  
  175.             cout << "Begin = " << _begin << ", Start = " << _start << ", SIZE = " << size << ", Entry Count = " << m_EntryCount << "\n";
  176.  
  177.             if (size > 0)
  178.             {
  179.                 memcpy(_begin, _start, size);
  180.  
  181.                 --m_EntryCount;
  182.                 return true;
  183.             }
  184.             else
  185.             {
  186.                 --m_EntryCount;
  187.                 return true;
  188.             }
  189.         }
  190.         else
  191.         {
  192.  
  193.             return false;
  194.         }
  195.     }
  196.  
  197.     template<typename VALUE>
  198.     bool List<VALUE>::Remove(VALUE value, bool all)
  199.     {
  200.         int f = 0;
  201.         int removeCount = 0;
  202.         if (all) {
  203.             while (f = Find(value, f) >= 0)
  204.             {
  205.                 Remove(f);
  206.                 ++removeCount;
  207.             }
  208.             return removeCount > 0;
  209.         }
  210.         if (f = Find(value, f) >= 0)
  211.         {
  212.             RemoveAt(f);
  213.             return true;
  214.         }
  215.         return false;
  216.     }
  217.     template<typename VALUE>
  218.     int List<VALUE>::Find(VALUE value, int pos, int match)
  219.     {
  220.         int currentMatch = 1;
  221.         for (int i = pos; i < m_EntryCount; ++i)
  222.         {
  223.             if (value == m_ChunkArrayStart[i])
  224.             {
  225.                 if (currentMatch++ == match)
  226.                 return i;
  227.             }
  228.         }
  229.         return -1;
  230.     }
  231.     template<typename VALUE>
  232.     vector<int> List<VALUE>::FindAll(VALUE value, int pos)
  233.     {
  234.         vector<int> ret;
  235.         int currentMatch = 1;
  236.         for (int i = pos; i < m_EntryCount; ++i)
  237.         {
  238.             if (value == m_ChunkArrayStart[i])
  239.             {
  240.                 ret.push_back(i);
  241.             }
  242.         }
  243.         return ret;
  244.     }
  245.  
  246.     template<typename VALUE>
  247.     VALUE List<VALUE>::Get(int index)
  248.     {
  249.         if (index < m_EntryCount)
  250.         {
  251.             return m_ChunkArrayStart[index];
  252.         }
  253.         return NULL;
  254.     }
  255.  
  256.     template<typename VALUE>
  257.     void List<VALUE>::Clear()
  258.     {
  259.         m_EntryCount = 0;
  260.         m_CurrentChunkSize = DEFAULT_CHUNK_COUNT;
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement