C/C++ nvm::collections::List class -- shared

usage

#include "object.h"
#include "collections/HashSet.h"
#include "collections/Dictionary.h"
#include "collections/List.h"
#include "gc/mempoll.h"
#include "array.h"
#include "string.h"
#include "utils/TextHelpers.h"

#include <Windows.h>

using nvm::collections::HashSet;
using nvm::collections::IEnumerator;
using nvm::collections::Dictionary;
using nvm::collections::KeyValuePair;
using nvm::collections::List;
using nvm::Array;
using nvm::String;
using nvm::Int32;
using nvm::Boolean;

int __cdecl main(int argc, char* argv[])
{
	setlocale(LC_ALL, "chs");

	List<String> list;
	list.Add("三清正道");
	list.Add("佛门外道");
	list.Add("白莲邪魔");

	list.Clear();

	list.Insert(0, "南极仙翁:南方南极长生大帝(玉清,道门四御)");
	list.Insert(0, "后土娘娘:承天效法厚德光大后土皇地祇(道门四御)");
	list.Insert(2, "女娲娘娘:娲皇(超脱)");
	list.Insert(3, "无当圣母:无极无生老母(上清)");
	list.Insert(4, "多宝道人:多宝无极天尊(上清)");
	list.Insert(5, "勾陈大帝:勾陈上宫天皇大帝");
	list.Insert(6, "清源妙道真君:英烈昭惠清源妙道敷泽兴济二郎显圣真君(玉清)");
	list.Insert(7, "紫微大帝:中天紫微北极太皇大帝(道门四御)");
	list.Insert(8, "玉皇大天尊:昊天金阙无上至尊自然妙有弥罗至真玉皇上帝(道门四御)");
	list.Insert(9, "太乙救苦天尊:东极青华大帝太乙救苦天尊");

	List<String>::Enumerator enumerator = list.GetEnumerator();
	while (enumerator.MoveNext())
	{
		wprintf(L"%s\n", enumerator.GetCurrent().GetBuffer());
	}
	return getchar();
}

List.h

#ifndef LIST_H
#define LIST_H

#include "../object.h"
#include "../array.h"
#include "../IDisposable.h"
#include "../gc/mempoll.h"
#include "../exception.h"
#include "../threading/Interlocked.h"
#include "../utils/RuntimeHelpers.h"
#include "./IList.h"
#include "./IEnumerator.h"

namespace nvm
{
	namespace collections
	{
		template<typename T>
		class List : 
			public Object,
			public IDisposable,
			public IList<T>
		{
		private:
			T* m_items;
			volatile nvm::Int32 m_version;
			volatile nvm::Int32 m_count;
			nvm::Int32 m_capacity;
			nvm::Int32 m_slotcapacity;
			nvm::Boolean m_disposed;

			inline nvm::Int32 IncrementOperationVersion()
			{
				return nvm::threading::Interlocked::Increment(this->m_version);
			}
			inline void EnlargeArrayCapacity(nvm::Int32 mode)
			{
				nvm::Int32 capacity = this->GetCapacity();
				if (capacity <= 0)
				{
					capacity = this->m_slotcapacity;
				}
				else
				{
					capacity *= 2;
				}
				if (capacity >= ~(1 << 31) || capacity < 0)
				{
					throw new OutOfMemoryException("this");
				}
				if (this->m_capacity != capacity)
				{
					do
					{
						T* old__ = this->m_items;
						T* new__ = new T[capacity];
						if (old__ != NULL)
						{
							if (mode == -1) // 增量模式
							{
								nvm::Int32 count__ = this->GetCount();
								for (nvm::Int32 i__ = 0; i__ < count__; i__++)
								{
									new__[i__] = old__[i__];
								}
							}
							else if (mode >= 0) // 后移模式
							{
								nvm::Int32 position__ = (nvm::Int32)mode;
								nvm::Int32 count__ = position__;
								for (nvm::Int32 i__ = 0; i__ < count__; i__++)
								{
									new__[i__] = old__[i__];
								}
								count__ = this->GetCount() - position__;
								T* px__ = new__ + (position__ + 1);
								T* py__ = old__ + position__;
								for (nvm::Int32 i__ = 0; i__ < count__; i__++)
								{
									px__[i__] = py__[i__];
								}
							}
							delete[] old__;
						}
						this->m_items = new__;
					} while (0, 0);
					this->m_capacity = capacity;
				}
			}
		public:
			class Enumerator :
				public IEnumerator<T>
			{
			private:
				List<T>* list_;
				nvm::Int32 index_;
				nvm::Int32 version_;

				inline nvm::Int32 GetCurrentListVersion()
				{
					return nvm::threading::Interlocked::Read(this->list_->m_version);
				}
				inline void CheckVersionOrThrowException()
				{
					if (this->version_ != this->GetCurrentListVersion())
					{
						throw new InvalidOperationException("version");
					}
				}
			public:
				Enumerator(List<T>* list)
				{
					if (list == NULL)
					{
						throw new ArgumentNullException("list");
					}
					this->index_ = -1;
					this->list_ = list;
					this->version_ = this->GetCurrentListVersion();
				}
				inline virtual nvm::Boolean MoveNext()
				{
					this->CheckVersionOrThrowException();
					return ++this->index_ < this->list_->GetCount();
				}
				inline virtual void Reset()
				{
					this->CheckVersionOrThrowException();
					this->index_ = -1;
				}
				inline virtual T& GetCurrent()
				{
					this->CheckVersionOrThrowException();
					nvm::Int32 index = this->index_;
					if (index < 0 || index >= this->list_->GetCount())
					{
						throw new InvalidOperationException("enumerator");
					}
					return this->list_->GetItem(index);
				}
			};
			List() : List(4)
			{

			}
			List(nvm::Int32 capacity)
			{
				if (capacity <= 0)
				{
					throw new ArgumentOutOfRangeException("capacity");
				}
				this->m_disposed = false;
				this->m_count = 0;
				this->m_version = 0;
				this->m_items = NULL;
				this->m_slotcapacity = capacity;
				this->EnlargeArrayCapacity(-1);
			}
			~List()
			{
				this->Dispose();
			}
			inline virtual void Dispose()
			{
				if (!this->m_disposed)
				{
					this->Clear();
					this->m_disposed = true;
				}
			}
			inline virtual void Insert(nvm::Int32 index, const T& item)
			{
				if (this->m_disposed)
				{
					throw new ObjectDisposedException("this");
				}
				nvm::Int32 count = this->GetCount();
				if (index < 0 || index > count)
				{
					throw new IndexOutOfRangeException("index");
				}
				if (index == count) // 插入到尾部
				{
					if (index >= this->GetCapacity())
					{
						this->EnlargeArrayCapacity(-1);
					}
				}
				else if ((count + 1) >= this->GetCapacity())
				{
					this->EnlargeArrayCapacity(index);
				}
				else
				{
					for (nvm::Int32 i = count; i >= index; i--) // 后移
					{
						this->m_items[i + 1] = this->m_items[i];
					}
				}
				this->m_items[index] = item;
				this->m_count++;
				this->IncrementOperationVersion();
			}
			inline virtual void Remove(nvm::Int32 index)
			{
				if (this->m_disposed)
				{
					throw new ObjectDisposedException("this");
				}
				nvm::Int32 count = this->GetCount();
				if (index < 0 || index >= count)
				{
					throw new IndexOutOfRangeException("index");
				}
				if ((index + 1) < count) 
				{
					for (nvm::Int32 i = (index + 1); i < count; i++) // 前移
					{
						this->m_items[i - 1] = this->m_items[i];
					}
				}
				this->m_count--;
				this->IncrementOperationVersion();
			}
			inline virtual nvm::Int32 IndexOf(const T& item)
			{
				if (this->m_disposed)
				{
					return -1;
				}
				nvm::Int32 count = this->GetCount();
				if (count > 0)
				{
					for (nvm::Int32 i = 0; i < count; i++)
					{
						T& r = this->m_items[i];
						if (nvm::utils::RuntimeHelpers::Equals(r, (T&)item))
						{
							return i;
						}
					}
				}
				return -1;
			}
			inline virtual void Clear()
			{
				if (!this->m_disposed && this->m_count != 0)
				{
					if (this->m_items != NULL)
					{
						delete[] this->m_items;
						this->m_items = NULL;
					}
					this->m_count = 0;
					this->m_capacity = 0;
					this->IncrementOperationVersion();
				}
			}
			inline T& GetItem(nvm::Int32 index)
			{
				if (this->m_disposed)
				{
					throw new ObjectDisposedException("this");
				}
				nvm::Int32 count = this->GetCount();
				if (index < 0 || index >= count)
				{
					throw new IndexOutOfRangeException("index");
				}
				return this->m_items[index];
			}
		    inline nvm::Int32 GetCapacity()
			{
				return this->m_capacity;
			}
			inline nvm::Int32 GetCount()
			{
				return this->m_count;
			}
			inline const T* GetBuffer()
			{
				if (this->m_disposed)
				{
					throw new ObjectDisposedException("this");
				}
				return this->m_items;
			}
			inline Array<T>* ToArray()
			{
				if (this->m_disposed)
				{
					throw new ObjectDisposedException("this");
				}
				int count = this->m_count;
				Array<T>* array_ = new Array<T>(count);
				if (count > 0)
				{
					T* px_ = &*array_;
					T* py_ = this->m_items;
					for (nvm::Int32 i = 0; i < count; i++)
					{
						px_[i] = py_[i];
					}
				}
				return array_;
			}
			inline virtual Enumerator GetEnumerator()
			{
				return Enumerator(this);
			}
		};
	}
}
#endif

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值