Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// SMObjects.h
- #pragma once
- #include <string>
- #include <map>
- #include <vector>
- namespace SMObjects {
- template<typename VALUE>
- class ListEntry {
- public:
- VALUE * m_Value;
- bool operator==(const ListEntry<VALUE>& comparator)
- {
- return m_Value == comparator.m_Value;
- }
- bool operator==(const VALUE& value)
- {
- return m_Value == &value;
- }
- ListEntry(VALUE & value)
- {
- m_Value = &value;
- }
- ~ListEntry();
- };
- template<typename VALUE>
- class List {
- int m_MaxChunkSize;
- int m_CurrentChunkSize;
- int m_EntryCount;
- bool _CreateChunk();
- VALUE** m_ChunkArray;
- public:
- static const int& DEFAULT_CHUNK_COUNT;
- List(int DEFAULT_CHUNK_COUNT);
- List();
- ~List();
- int Count();
- void Add(const VALUE& value);
- bool Remove(int index);
- bool Remove(const VALUE& value, bool all = false);
- bool Insert(const VALUE& value, int index);
- int Find(const VALUE& value, int startPos);
- int Find(const VALUE& value);
- VALUE & Get(int index);
- //vector<VALUE*> Get();
- void Clear();
- VALUE& operator[] (int index);
- //int operator[] (const VALUE& value);
- VALUE& operator* () { return **m_ChunkArray; }
- void operator++ () { ++m_ChunkArray; }
- VALUE** begin() { return m_ChunkArray; }
- VALUE** end() { return m_ChunkArray + m_EntryCount; }
- };
- }
- ///SMObjects.cpp
- #include "SMObjects.h"
- #include <vector>
- #include <map>
- using namespace std;
- namespace SMObjects {
- /// <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];
- memcpy(newChunkArray, m_ChunkArray, sizeof(m_ChunkArray));
- delete[] m_ChunkArray;
- m_ChunkArray = newChunkArray;
- return true;
- }
- }
- return false;
- }
- template<typename VALUE>
- List<VALUE>::~List()
- {
- for (VALUE* currentListEntry : m_ChunkArray)
- {
- //delete currentListEntry->m_Value;
- //delete currentListEntry;
- }
- delete[] m_ChunkArray;
- }
- template<typename VALUE>
- VALUE& List<VALUE>::operator[] (int index)
- {
- return Get(index);
- }
- template<typename VALUE>
- bool List<VALUE>::Insert(const VALUE& value, int index)
- {
- if (index < m_EntryCount)
- {
- ++m_EntryCount;
- _CreateChunk();
- VALUE ** start = m_ChunkArray[index];
- VALUE ** end = m_ChunkArray[m_EntryCount - 1];
- int size = end - start;
- if (size > 0) memcpy(start + 1, start, size);
- m_ChunkArray[index] = &value;
- return true;
- }
- return false;
- }
- template<typename VALUE>
- void List<VALUE>::Add(const VALUE& value)
- {
- int index = m_EntryCount++;
- if (_CreateChunk())
- {
- //DEBUG NEW CHUNK CREATION
- }
- m_ChunkArray[index] = &value;
- }
- template<typename VALUE>
- bool List<VALUE>::Remove(int index)
- {
- if (index <= m_EntryCount)
- {
- VALUE ** _begin = m_ChunkArray[index];
- delete* _begin;
- VALUE ** _end = m_ChunkArray[--m_EntryCount];
- int size = _end - _begin;
- memcpy(_begin, _end, size);
- return true;
- }
- else
- {
- return false;
- }
- }
- template<typename VALUE>
- bool List<VALUE>::Remove(const 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)
- {
- Remove(f);
- return true;
- }
- return false;
- }
- template<typename VALUE>
- int List<VALUE>::Find(const VALUE& value)
- {
- return Find(value, 0);
- }
- template<typename VALUE>
- int List<VALUE>::Find(const VALUE& value, int startPos)
- {
- for (int i = startPos; i < m_ChunkArray; ++i)
- {
- if (value == *m_ChunkArray[i])
- {
- return i;
- }
- }
- return -1;
- }
- template<typename VALUE>
- VALUE & List<VALUE>::Get(int index)
- {
- if (index < m_EntryCount)
- {
- return **m_ChunkArray[index];
- }
- return NULL;
- }
- template<typename VALUE>
- void List<VALUE>::Clear()
- {
- m_EntryCount = 0;
- m_CurrentChunkSize = 0;
- }
- }
- // SMTesting.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include "SMObjects.h"
- using namespace SMObjects;
- using namespace std;
- int main()
- {
- List<int*>* someList = new List<int*>(16);
- for (int i = 0; i < 12; ++i)
- {
- someList->Add(new int(3 * i));
- }
- for (int i = 0; i < someList->Count(); ++i)
- {
- cout << "Current List Number :" << i << ", Current List Value: " << *someList->Get(i) << "\n";
- }
- while (someList->Count() > 0) {
- delete someList->Get(0);
- someList->Remove(0);
- }
- for (int i = 0; i < 38; ++i)
- {
- someList->Add(new int(98349834 % i));
- }
- for (int i = 0; i < someList->Count(); ++i)
- {
- cout << "Current List Number :" << i << ", Current List Value: " << *someList->Get(i) << "\n";
- //delete someList->Get(i);
- }
- someList->Clear();
- someList->Insert(new int(1337), 16);
- for (int i = 0; i < someList->Count(); ++i)
- {
- cout << "Current List Number :" << i << ", Current List Value: " << *someList->Get(i) << "\n";
- delete someList->Get(i);
- }
- someList->Clear();
- delete someList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement