Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <vector>
- #include <iostream>
- using namespace std;
- namespace SMObjects {
- template<typename VALUE>
- class List {
- int m_MaxChunkSize;
- int m_CurrentChunkSize;
- int m_EntryCount;
- bool _CreateChunk();
- VALUE* m_ChunkArrayStart;
- VALUE* m_ChunkArrayEnd;
- public:
- int DEFAULT_CHUNK_COUNT;
- List(int DEFAULT_CHUNK_COUNT);
- List();
- ~List();
- int Count();
- void Add(VALUE value);
- bool RemoveAt(int index);
- bool Remove(VALUE value, bool all = false);
- bool Insert(VALUE value, int index);
- int Find(VALUE value, int pos = 0, int match = 1);
- vector<int> FindAll(VALUE value, int pos = 0);
- VALUE Get(int index);
- //vector<VALUE*> Get();
- void Clear();
- VALUE& operator[] (int index);
- VALUE& operator<<(VALUE& value);
- VALUE& operator>>(VALUE& value);
- VALUE operator* () { return *m_ChunkArrayStart; }
- void operator++ () { ++m_ChunkArrayStart; }
- VALUE* begin() { return m_ChunkArrayStart; }
- VALUE* end() { return m_ChunkArrayStart + m_EntryCount; }
- };
- /// <summary>
- /// Class 'List' Implementation
- /// </summary>
- /// <typeparam name="VALUE"></typeparam>
- /// <param name="DEFAULT_CHUNK_COUNT"></param>
- template<typename VALUE>
- List<VALUE>::List(int DEFAULT_CHUNK_COUNT)
- {
- this->DEFAULT_CHUNK_COUNT = DEFAULT_CHUNK_COUNT;
- _CreateChunk();
- }
- template<typename VALUE>
- List<VALUE>::List() : List(128)
- {
- }
- template<typename VALUE>
- int List<VALUE>::Count()
- {
- return m_EntryCount;
- }
- template<typename VALUE>
- bool List<VALUE>::_CreateChunk()
- {
- if (m_EntryCount >= m_CurrentChunkSize)
- {
- m_CurrentChunkSize += DEFAULT_CHUNK_COUNT;
- if (m_CurrentChunkSize > m_MaxChunkSize)
- {
- VALUE* newChunkArray = new VALUE[m_CurrentChunkSize];
- if (m_EntryCount > 0)
- {
- memcpy(newChunkArray, m_ChunkArrayStart, sizeof(VALUE) * m_EntryCount);
- delete[] m_ChunkArrayStart;
- }
- m_ChunkArrayStart = newChunkArray;
- m_ChunkArrayEnd = m_ChunkArrayStart + m_CurrentChunkSize;
- return true;
- }
- }
- return false;
- }
- template<typename VALUE>
- List<VALUE>::~List()
- {
- delete[] m_ChunkArrayStart;
- }
- template<typename VALUE>
- VALUE& List<VALUE>::operator[] (int index)
- {
- if (index >= 0 && index < m_EntryCount)
- return m_ChunkArrayStart[index];
- }
- template<typename VALUE>
- VALUE& List<VALUE>::operator<<(VALUE& value)
- {
- return value;
- }
- template<typename VALUE>
- VALUE& List<VALUE>::operator>>(VALUE& value)
- {
- return value;
- }
- template<typename VALUE>
- bool List<VALUE>::Insert(VALUE value, int index)
- {
- if (index < m_EntryCount)
- {
- ++m_EntryCount;
- _CreateChunk();
- VALUE* start = m_ChunkArrayStart + index;
- int size = (m_ChunkArrayEnd - start - 1) * sizeof(VALUE);
- if (size > 0) memcpy(start + 1, start, size);
- m_ChunkArrayStart[index] = value;
- return true;
- }
- return false;
- }
- template<typename VALUE>
- void List<VALUE>::Add(VALUE value)
- {
- int index = m_EntryCount++;
- if (_CreateChunk())
- {
- //DEBUG NEW CHUNK CREATION
- }
- m_ChunkArrayStart[index] = value;
- }
- template<typename VALUE>
- bool List<VALUE>::RemoveAt(int index)
- {
- if (index < m_EntryCount)
- {
- VALUE* _begin = m_ChunkArrayStart + index;
- VALUE* _start = m_ChunkArrayStart + index + 1;
- int size = (m_ChunkArrayEnd - _start) * sizeof(VALUE);
- cout << "Begin = " << _begin << ", Start = " << _start << ", SIZE = " << size << ", Entry Count = " << m_EntryCount << "\n";
- if (size > 0)
- {
- memcpy(_begin, _start, size);
- --m_EntryCount;
- return true;
- }
- else
- {
- --m_EntryCount;
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- template<typename VALUE>
- bool List<VALUE>::Remove(VALUE value, bool all)
- {
- int f = 0;
- int removeCount = 0;
- if (all) {
- while (f = Find(value, f) >= 0)
- {
- Remove(f);
- ++removeCount;
- }
- return removeCount > 0;
- }
- if (f = Find(value, f) >= 0)
- {
- RemoveAt(f);
- return true;
- }
- return false;
- }
- template<typename VALUE>
- int List<VALUE>::Find(VALUE value, int pos, int match)
- {
- int currentMatch = 1;
- for (int i = pos; i < m_EntryCount; ++i)
- {
- if (value == m_ChunkArrayStart[i])
- {
- if (currentMatch++ == match)
- return i;
- }
- }
- return -1;
- }
- template<typename VALUE>
- vector<int> List<VALUE>::FindAll(VALUE value, int pos)
- {
- vector<int> ret;
- int currentMatch = 1;
- for (int i = pos; i < m_EntryCount; ++i)
- {
- if (value == m_ChunkArrayStart[i])
- {
- ret.push_back(i);
- }
- }
- return ret;
- }
- template<typename VALUE>
- VALUE List<VALUE>::Get(int index)
- {
- if (index < m_EntryCount)
- {
- return m_ChunkArrayStart[index];
- }
- return NULL;
- }
- template<typename VALUE>
- void List<VALUE>::Clear()
- {
- m_EntryCount = 0;
- m_CurrentChunkSize = DEFAULT_CHUNK_COUNT;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement