From 04d43cd71f081f1b8d2f0fd803a47cb6342e4fcd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Thu, 15 Jun 2017 15:21:20 -0700 Subject: Remove more "core" code that isn't used. It is always easier to add back code when you need it, than it is to maintain code you aren't using. --- source/core/basic.h | 2 - source/core/common.h | 6 - source/core/core.natvis | 40 --- source/core/core.vcxproj | 5 - source/core/dictionary.h | 406 --------------------------- source/core/exception.h | 5 +- source/core/link.h | 334 ---------------------- source/core/linq.h | 665 -------------------------------------------- source/core/memory-pool.cpp | 132 --------- source/core/memory-pool.h | 122 -------- source/core/slang-io.cpp | 7 - source/core/slang-io.h | 20 -- source/core/slang-math.cpp | 8 - source/core/stream.h | 243 +--------------- source/core/text-io.h | 11 +- 15 files changed, 11 insertions(+), 1995 deletions(-) delete mode 100644 source/core/link.h delete mode 100644 source/core/linq.h delete mode 100644 source/core/memory-pool.cpp delete mode 100644 source/core/memory-pool.h delete mode 100644 source/core/slang-math.cpp (limited to 'source/core') diff --git a/source/core/basic.h b/source/core/basic.h index c5ed11091..e89d740bf 100644 --- a/source/core/basic.h +++ b/source/core/basic.h @@ -6,10 +6,8 @@ #include "slang-string.h" #include "array.h" #include "list.h" -#include "link.h" #include "smart-pointer.h" #include "exception.h" #include "dictionary.h" -#include "linq.h" #endif \ No newline at end of file diff --git a/source/core/common.h b/source/core/common.h index ac8456009..25e85dc66 100644 --- a/source/core/common.h +++ b/source/core/common.h @@ -20,12 +20,6 @@ namespace Slang #else typedef int PtrInt; #endif - class Object - { - public: - virtual ~Object() - {} - }; template inline T&& _Move(T & obj) diff --git a/source/core/core.natvis b/source/core/core.natvis index 271e39663..19c6db395 100644 --- a/source/core/core.natvis +++ b/source/core/core.natvis @@ -42,18 +42,6 @@ - - {{ size={FCount} }} - - - FCount - FHead - pNext - Value - - - - {{ size={_count} }} @@ -66,34 +54,6 @@ - - {{ size={_count} }} - - _count - bucketSizeMinusOne + 1 - - kvPairs.FCount - kvPairs.FHead - pNext - Value - - - - - - {{ size={dict._count} }} - - dict._count - dict.bucketSizeMinusOne + 1 - - dict.kvPairs.FCount - dict.kvPairs.FHead - pNext - Value - - - - pointer empty diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj index bba6102b7..88e776703 100644 --- a/source/core/core.vcxproj +++ b/source/core/core.vcxproj @@ -28,10 +28,7 @@ - - - @@ -42,9 +39,7 @@ - - diff --git a/source/core/dictionary.h b/source/core/dictionary.h index c052685fd..408835454 100644 --- a/source/core/dictionary.h +++ b/source/core/dictionary.h @@ -487,398 +487,6 @@ namespace Slang } }; - template - class EnumerableDictionary - { - friend class Iterator; - friend class ItemProxy; - private: - inline int GetProbeOffset(int /*probeIdx*/) const - { - // quadratic probing - return 1; - } - private: - int bucketSizeMinusOne, shiftBits; - int _count; - IntSet marks; - - // debug op - struct Op - { - TKey key; - int opType; - Op() - {} - Op(const TKey & key, int t) - { - this->key = key; - opType = t; - } - }; - LinkedList> kvPairs; - LinkedNode>** hashMap; - void Free() - { - if (hashMap) - delete[] hashMap; - hashMap = 0; - kvPairs.Clear(); - } - inline bool IsDeleted(int pos) const - { - return marks.Contains((pos << 1) + 1); - } - inline bool IsEmpty(int pos) const - { - return !marks.Contains((pos << 1)); - } - inline void SetDeleted(int pos, bool val) - { - if (val) - marks.Add((pos << 1) + 1); - else - marks.Remove((pos << 1) + 1); - } - inline void SetEmpty(int pos, bool val) - { - if (val) - marks.Remove((pos << 1)); - else - marks.Add((pos << 1)); - } - struct FindPositionResult - { - int ObjectPosition; - int InsertionPosition; - FindPositionResult() - { - ObjectPosition = -1; - InsertionPosition = -1; - } - FindPositionResult(int objPos, int insertPos) - { - ObjectPosition = objPos; - InsertionPosition = insertPos; - } - - }; - template - inline int GetHashPos(T & key) const - { - return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits; - } - template - FindPositionResult FindPosition(const T & key) const - { - int hashPos = GetHashPos((T&)key); - int insertPos = -1; - int numProbes = 0; - while (numProbes <= bucketSizeMinusOne) - { - if (IsEmpty(hashPos)) - { - if (insertPos == -1) - return FindPositionResult(-1, hashPos); - else - return FindPositionResult(-1, insertPos); - } - else if (IsDeleted(hashPos)) - { - if (insertPos == -1) - insertPos = hashPos; - } - else if (hashMap[hashPos]->Value.Key == key) - { - return FindPositionResult(hashPos, -1); - } - numProbes++; - hashPos = (hashPos + GetProbeOffset(numProbes)) & bucketSizeMinusOne; - } - if (insertPos != -1) - return FindPositionResult(-1, insertPos); - throw InvalidOperationException("Hash map is full. This indicates an error in Key::Equal or Key::GetHashCode."); - } - TValue & _Insert(KeyValuePair && kvPair, int pos) - { - auto node = kvPairs.AddLast(); - node->Value = _Move(kvPair); - hashMap[pos] = node; - SetEmpty(pos, false); - SetDeleted(pos, false); - return node->Value.Value; - } - void Rehash() - { - if (bucketSizeMinusOne == -1 || _count / (float)bucketSizeMinusOne >= MaxLoadFactor) - { - int newSize = (bucketSizeMinusOne + 1) * 2; - int newShiftBits = shiftBits - 1; - if (newSize == 0) - { - newSize = 16; - newShiftBits = 28; - } - EnumerableDictionary newDict; - newDict.shiftBits = newShiftBits; - newDict.bucketSizeMinusOne = newSize - 1; - newDict.hashMap = new LinkedNode>*[newSize]; - newDict.marks.SetMax(newSize * 2); - if (hashMap) - { - for (auto & kvPair : *this) - { - newDict.Add(_Move(kvPair)); - } - } - *this = _Move(newDict); - } - } - - bool AddIfNotExists(KeyValuePair && kvPair) - { - Rehash(); - auto pos = FindPosition(kvPair.Key); - if (pos.ObjectPosition != -1) - return false; - else if (pos.InsertionPosition != -1) - { - _count++; - _Insert(_Move(kvPair), pos.InsertionPosition); - return true; - } - else - throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation."); - } - void Add(KeyValuePair && kvPair) - { - if (!AddIfNotExists(_Move(kvPair))) - throw KeyExistsException("The key already exists in Dictionary."); - } - TValue & Set(KeyValuePair && kvPair) - { - Rehash(); - auto pos = FindPosition(kvPair.Key); - if (pos.ObjectPosition != -1) - { - hashMap[pos.ObjectPosition]->Delete(); - return _Insert(_Move(kvPair), pos.ObjectPosition); - } - else if (pos.InsertionPosition != -1) - { - _count++; - return _Insert(_Move(kvPair), pos.InsertionPosition); - } - else - throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation."); - } - public: - typedef typename LinkedList>::Iterator Iterator; - - typename LinkedList>::Iterator begin() const - { - return kvPairs.begin(); - } - typename LinkedList>::Iterator end() const - { - return kvPairs.end(); - } - public: - void Add(const TKey & key, const TValue & value) - { - Add(KeyValuePair(key, value)); - } - void Add(TKey && key, TValue && value) - { - Add(KeyValuePair(_Move(key), _Move(value))); - } - bool AddIfNotExists(const TKey & key, const TValue & value) - { - return AddIfNotExists(KeyValuePair(key, value)); - } - bool AddIfNotExists(TKey && key, TValue && value) - { - return AddIfNotExists(KeyValuePair(_Move(key), _Move(value))); - } - void Remove(const TKey & key) - { - if (_count > 0) - { - auto pos = FindPosition(key); - if (pos.ObjectPosition != -1) - { - kvPairs.Delete(hashMap[pos.ObjectPosition]); - hashMap[pos.ObjectPosition] = 0; - SetDeleted(pos.ObjectPosition, true); - _count--; - } - } - } - void Clear() - { - _count = 0; - kvPairs.Clear(); - marks.Clear(); - } - template - bool ContainsKey(const T & key) const - { - if (bucketSizeMinusOne == -1) - return false; - auto pos = FindPosition(key); - return pos.ObjectPosition != -1; - } - template - TValue * TryGetValue(const T & key) const - { - if (bucketSizeMinusOne == -1) - return nullptr; - auto pos = FindPosition(key); - if (pos.ObjectPosition != -1) - { - return &(hashMap[pos.ObjectPosition]->Value.Value); - } - return nullptr; - } - template - bool TryGetValue(const T & key, TValue & value) const - { - if (bucketSizeMinusOne == -1) - return false; - auto pos = FindPosition(key); - if (pos.ObjectPosition != -1) - { - value = hashMap[pos.ObjectPosition]->Value.Value; - return true; - } - return false; - } - class ItemProxy - { - private: - const EnumerableDictionary * dict; - TKey key; - public: - ItemProxy(const TKey & _key, const EnumerableDictionary * _dict) - { - this->dict = _dict; - this->key = _key; - } - ItemProxy(TKey && _key, const EnumerableDictionary * _dict) - { - this->dict = _dict; - this->key = _Move(_key); - } - TValue & GetValue() const - { - auto pos = dict->FindPosition(key); - if (pos.ObjectPosition != -1) - { - return dict->hashMap[pos.ObjectPosition]->Value.Value; - } - else - { - throw KeyNotFoundException("The key does not exists in dictionary."); - } - } - inline TValue & operator()() const - { - return GetValue(); - } - operator TValue&() const - { - return GetValue(); - } - TValue & operator = (const TValue & val) - { - return ((EnumerableDictionary*)dict)->Set(KeyValuePair(_Move(key), val)); - } - TValue & operator = (TValue && val) - { - return ((EnumerableDictionary*)dict)->Set(KeyValuePair(_Move(key), _Move(val))); - } - }; - ItemProxy operator [](const TKey & key) const - { - return ItemProxy(key, this); - } - ItemProxy operator [](TKey && key) const - { - return ItemProxy(_Move(key), this); - } - int Count() const - { - return _count; - } - KeyValuePair & First() const - { - return kvPairs.First(); - } - KeyValuePair & Last() const - { - return kvPairs.Last(); - } - private: - template - void Init(const KeyValuePair & kvPair, Args... args) - { - Add(kvPair); - Init(args...); - } - public: - EnumerableDictionary() - { - bucketSizeMinusOne = -1; - shiftBits = 32; - _count = 0; - hashMap = 0; - } - template - EnumerableDictionary(Arg arg, Args... args) - { - Init(arg, args...); - } - EnumerableDictionary(const EnumerableDictionary & other) - : bucketSizeMinusOne(-1), _count(0), hashMap(0) - { - *this = other; - } - EnumerableDictionary(EnumerableDictionary && other) - : bucketSizeMinusOne(-1), _count(0), hashMap(0) - { - *this = (_Move(other)); - } - EnumerableDictionary & operator = (const EnumerableDictionary & other) - { - if (this == &other) - return *this; - Clear(); - for (auto & item : other) - Add(item.Key, item.Value); - return *this; - } - EnumerableDictionary & operator = (EnumerableDictionary && other) - { - if (this == &other) - return *this; - Free(); - bucketSizeMinusOne = other.bucketSizeMinusOne; - _count = other._count; - hashMap = other.hashMap; - shiftBits = other.shiftBits; - marks = _Move(other.marks); - other.hashMap = 0; - other._count = 0; - other.bucketSizeMinusOne = -1; - kvPairs = _Move(other.kvPairs); - return *this; - } - ~EnumerableDictionary() - { - Free(); - } - }; - class _DummyClass {}; @@ -996,20 +604,6 @@ namespace Slang template class HashSet : public HashSetBase> {}; - - template - class EnumerableHashSet : public HashSetBase> - { - public: - T & First() const - { - return this->dict.First().Key; - } - T & Last() const - { - return this->dict.Last().Key; - } - }; } #endif diff --git a/source/core/exception.h b/source/core/exception.h index 4671ae10b..6739c6778 100644 --- a/source/core/exception.h +++ b/source/core/exception.h @@ -6,7 +6,7 @@ namespace Slang { - class Exception : public Object + class Exception { public: String Message; @@ -16,6 +16,9 @@ namespace Slang : Message(message) { } + + virtual ~Exception() + {} }; class IndexOutofRangeException : public Exception diff --git a/source/core/link.h b/source/core/link.h deleted file mode 100644 index f74a250b9..000000000 --- a/source/core/link.h +++ /dev/null @@ -1,334 +0,0 @@ -#ifndef CORE_LIB_LINK_H -#define CORE_LIB_LINK_H - -#include "Common.h" -#include "Exception.h" - -namespace Slang -{ - template - class LinkedList; - - template - class LinkedNode - { - template - friend class LinkedList; - private: - LinkedNode *pPrev, *pNext; - LinkedList * FLink; - public: - T Value; - LinkedNode (LinkedList * lnk):FLink(lnk) - { - pPrev = pNext = 0; - }; - LinkedNode * GetPrevious() - { - return pPrev; - }; - LinkedNode * GetNext() - { - return pNext; - }; - LinkedNode * InsertAfter(const T & nData) - { - LinkedNode * n = new LinkedNode(FLink); - n->Value = nData; - n->pPrev = this; - n->pNext = this->pNext; - LinkedNode *npp = n->pNext; - if (npp) - { - npp->pPrev = n; - } - pNext = n; - if (!n->pNext) - FLink->FTail = n; - FLink->FCount ++; - return n; - }; - LinkedNode * InsertBefore(const T & nData) - { - LinkedNode * n = new LinkedNode(FLink); - n->Value = nData; - n->pPrev = pPrev; - n->pNext = this; - pPrev = n; - LinkedNode *npp = n->pPrev; - if (npp) - npp->pNext = n; - if (!n->pPrev) - FLink->FHead = n; - FLink->FCount ++; - return n; - }; - void Delete() - { - if (pPrev) - pPrev->pNext = pNext; - if (pNext) - pNext->pPrev = pPrev; - FLink->FCount --; - if (FLink->FHead == this) - { - FLink->FHead = pNext; - } - if (FLink->FTail == this) - { - FLink->FTail = pPrev; - } - delete this; - } - }; - template - class LinkedList - { - template - friend class LinkedNode; - private: - LinkedNode * FHead, *FTail; - int FCount; - public: - class Iterator - { - public: - LinkedNode * Current, *Next; - void SetCurrent(LinkedNode * cur) - { - Current = cur; - if (Current) - Next = Current->GetNext(); - else - Next = 0; - } - Iterator() - { - Current = Next = 0; - } - Iterator(LinkedNode * cur) - { - SetCurrent(cur); - } - T & operator *() const - { - return Current->Value; - } - Iterator& operator ++() - { - SetCurrent(Next); - return *this; - } - Iterator operator ++(int) - { - Iterator rs = *this; - SetCurrent(Next); - return rs; - } - bool operator != (const Iterator & iter) const - { - return Current != iter.Current; - } - bool operator == (const Iterator & iter) const - { - return Current == iter.Current; - } - }; - Iterator begin() const - { - return Iterator(FHead); - } - Iterator end() const - { - return Iterator(0); - } - public: - LinkedList() : FHead(0), FTail(0), FCount(0) - { - } - ~LinkedList() - { - Clear(); - } - LinkedList(const LinkedList & link) : FHead(0), FTail(0), FCount(0) - { - this->operator=(link); - } - LinkedList(LinkedList && link) : FHead(0), FTail(0), FCount(0) - { - this->operator=(_Move(link)); - } - LinkedList & operator = (LinkedList && link) - { - if (FHead != 0) - Clear(); - FHead = link.FHead; - FTail = link.FTail; - FCount = link.FCount; - link.FHead = 0; - link.FTail = 0; - link.FCount = 0; - for (auto node = FHead; node; node = node->GetNext()) - node->FLink = this; - return *this; - } - LinkedList & operator = (const LinkedList & link) - { - if (FHead != 0) - Clear(); - auto p = link.FHead; - while (p) - { - AddLast(p->Value); - p = p->GetNext(); - } - return *this; - } - template - void ForEach(const IteratorFunc & f) - { - auto p = FHead; - while (p) - { - f(p->Value); - p = p->GetNext(); - } - } - LinkedNode * GetNode(int x) - { - LinkedNode *pCur = FHead; - for (int i=0;ipNext; - else - throw "Index out of range"; - } - return pCur; - }; - LinkedNode * Find(const T& fData) - { - for (LinkedNode * pCur = FHead; pCur; pCur = pCur->pNext) - { - if (pCur->Value == fData) - return pCur; - } - return 0; - }; - LinkedNode * FirstNode() const - { - return FHead; - }; - T & First() const - { - if (!FHead) - throw IndexOutofRangeException("LinkedList: index out of range."); - return FHead->Value; - } - T & Last() const - { - if (!FTail) - throw IndexOutofRangeException("LinkedList: index out of range."); - return FTail->Value; - } - LinkedNode * LastNode() const - { - return FTail; - }; - LinkedNode * AddLast(const T & nData) - { - LinkedNode * n = new LinkedNode(this); - n->Value = nData; - n->pPrev = FTail; - if (FTail) - FTail->pNext = n; - n->pNext = 0; - FTail = n; - if (!FHead) - FHead = n; - FCount ++; - return n; - }; - // Insert a blank node - LinkedNode * AddLast() - { - LinkedNode * n = new LinkedNode(this); - n->pPrev = FTail; - if (FTail) - FTail->pNext = n; - n->pNext = 0; - FTail = n; - if (!FHead) - FHead = n; - FCount ++; - return n; - }; - LinkedNode * AddFirst(const T& nData) - { - LinkedNode *n = new LinkedNode(this); - n->Value = nData; - n->pPrev = 0; - n->pNext = FHead; - if (FHead) - FHead->pPrev = n; - FHead = n; - if (!FTail) - FTail = n; - FCount ++; - return n; - }; - void Delete(LinkedNode*n, int Count = 1) - { - LinkedNode *n1,*n2 = 0, *tn; - n1 = n->pPrev; - tn = n; - int numDeleted = 0; - for (int i=0; ipNext; - delete tn; - tn = n2; - numDeleted++; - if (tn == 0) - break; - } - if (n1) - n1->pNext = n2; - else - FHead = n2; - if (n2) - n2->pPrev = n1; - else - FTail = n1; - FCount -= numDeleted; - } - void Clear() - { - for (LinkedNode *n = FHead; n; ) - { - LinkedNode * tmp = n->pNext; - delete n; - n = tmp; - } - FHead = 0; - FTail = 0; - FCount = 0; - } - List ToList() const - { - List rs; - rs.Reserve(FCount); - for (auto & item : *this) - { - rs.Add(item); - } - return rs; - } - int Count() - { - return FCount; - } - }; -} - -#endif diff --git a/source/core/linq.h b/source/core/linq.h deleted file mode 100644 index 52af3abd0..000000000 --- a/source/core/linq.h +++ /dev/null @@ -1,665 +0,0 @@ -#ifndef FUNDAMENTAL_LIB_LINQ_H -#define FUNDAMENTAL_LIB_LINQ_H - -#include "List.h" - -namespace Slang -{ - template - T ConstructT(); - - template - class RemoveReference - { - public: - typedef T Type; - }; - - template - class RemoveReference - { - public: - typedef T Type; - }; - - template - class RemoveReference - { - public: - typedef T Type; - }; - - template - struct RemovePointer - { - typedef T Type; - }; - - template - struct RemovePointer - { - typedef T Type; - }; - - template - class ConcatQuery - { - private: - TQueryable1 items1; - TQueryable2 items2; - public: - ConcatQuery(const TQueryable1 & queryable1, const TQueryable2 & queryable2) - : items1(queryable1), items2(queryable2) - {} - class Enumerator - { - private: - TEnumerator1 ptr1; - TEnumerator1 end1; - TEnumerator2 ptr2; - TEnumerator2 end2; - public: - Enumerator(const Enumerator &) = default; - Enumerator(TEnumerator1 pptr, TEnumerator1 pend, TEnumerator2 pptr2, TEnumerator2 pend2) - : ptr1(pptr), end1(pend), ptr2(pptr2), end2(pend2) - {} - T operator *() const - { - if (ptr1 != end1) - return *(ptr1); - else - return *(ptr2); - } - Enumerator& operator ++() - { - if (ptr1 != end1) - ++ptr1; - else - ++ptr2; - return *this; - } - Enumerator operator ++(int) - { - Enumerator rs = *this; - ++rs; - return rs; - } - bool operator != (const Enumerator & iter) const - { - return ptr1 != iter.ptr1 || ptr2 != iter.ptr2; - } - bool operator == (const Enumerator & iter) const - { - return ptr1 == iter.ptr1 && ptr2 == iter.ptr2; - } - }; - Enumerator begin() const - { - return Enumerator(items1.begin(), items1.end(), items2.begin(), items2.end()); - } - Enumerator end() const - { - return Enumerator(items1.end(), items1.end(), items2.end(), items2.end()); - } - }; - - template - class WhereQuery - { - private: - TQueryable items; - TFunc func; - public: - WhereQuery(const TQueryable & queryable, const TFunc & f) - : items(queryable), func(f) - {} - class Enumerator - { - private: - TEnumerator ptr; - TEnumerator end; - const TFunc * func; - public: - Enumerator(const Enumerator &) = default; - Enumerator(TEnumerator ptr, TEnumerator end, const TFunc & f) - : ptr(ptr), end(end), func(&f) - {} - T operator *() const - { - return *(ptr); - } - Enumerator& operator ++() - { - ++ptr; - while (ptr != end) - { - if ((*func)(*ptr)) - break; - else - ++ptr; - } - return *this; - } - Enumerator operator ++(int) - { - Enumerator rs = *this; - while (rs.ptr != end) - { - if ((*func)(*rs.ptr)) - break; - ++rs.ptr; - } - return rs; - } - bool operator != (const Enumerator & iter) const - { - return ptr != iter.ptr; - } - bool operator == (const Enumerator & iter) const - { - return ptr == iter.ptr; - } - }; - Enumerator begin() const - { - auto ptr = items.begin(); - auto end = items.end(); - while (ptr != end) - { - if (func(*ptr)) - break; - ++ptr; - } - return Enumerator(ptr, end, func); - } - Enumerator end() const - { - return Enumerator(items.end(), items.end(), func); - } - }; - - template - class SkipQuery - { - private: - TQueryable items; - int count = 0; - public: - SkipQuery(const TQueryable & queryable, int pCount) - : items(queryable), count(pCount) - {} - class Enumerator - { - private: - TEnumerator ptr; - TEnumerator end; - public: - Enumerator(const Enumerator &) = default; - Enumerator(TEnumerator pptr, TEnumerator pend) - : ptr(pptr), end(pend) - { - } - T operator *() const - { - return *(ptr); - } - Enumerator& operator ++() - { - ++ptr; - return *this; - } - Enumerator operator ++(int) - { - Enumerator rs = *this; - ++ptr; - return rs; - } - bool operator != (const Enumerator & iter) const - { - return ptr != iter.ptr; - } - bool operator == (const Enumerator & iter) const - { - return ptr == iter.ptr; - } - }; - Enumerator begin() const - { - auto ptr = items.begin(); - auto end = items.end(); - for (int i = 0; i < count; i++) - if (ptr != end) - ++ptr; - return Enumerator(ptr, end); - } - Enumerator end() const - { - return Enumerator(items.end(), items.end()); - } - }; - - template - class SelectQuery - { - private: - TQueryable items; - TFunc func; - public: - SelectQuery(const TQueryable & queryable, const TFunc & f) - : items(queryable), func(f) - {} - class Enumerator - { - private: - TEnumerator ptr; - TEnumerator end; - const TFunc * func; - public: - Enumerator(const Enumerator &) = default; - Enumerator(TEnumerator ptr, TEnumerator end, const TFunc & f) - : ptr(ptr), end(end), func(&f) - {} - auto operator *() const -> decltype((*func)(*ptr)) - { - return (*func)(*ptr); - } - Enumerator& operator ++() - { - ++ptr; - return *this; - } - Enumerator operator ++(int) - { - Enumerator rs = *this; - ++rs; - return rs; - } - bool operator != (const Enumerator & iter) const - { - return !(ptr == iter.ptr); - } - bool operator == (const Enumerator & iter) const - { - return ptr == iter.ptr; - } - }; - Enumerator begin() const - { - return Enumerator(items.begin(), items.end(), func); - } - Enumerator end() const - { - return Enumerator(items.end(), items.end(), func); - } - }; - - template - class SelectManyQuery - { - private: - TQueryable items; - TFunc func; - SelectManyQuery() - {} - public: - SelectManyQuery(const TQueryable & queryable, const TFunc & f) - : items(queryable), func(f) - {} - template - class Enumerator - { - private: - TEnumerator ptr; - TEnumerator end; - const TFunc * func; - TItems items; - TItemPtr subPtr; - public: - Enumerator(const Enumerator &) = default; - Enumerator(TEnumerator ptr, TEnumerator end, const TFunc & f) - : ptr(ptr), end(end), func(&f) - { - if (ptr != end) - { - items = f(*ptr); - subPtr = items.begin(); - } - } - auto operator *() const -> decltype(*subPtr) - { - return *subPtr; - } - Enumerator& operator ++() - { - ++subPtr; - while (subPtr == items.end() && ptr != end) - { - ++ptr; - if (ptr != end) - { - items = (*func)(*ptr); - subPtr = items.begin(); - } - else - break; - } - - return *this; - } - Enumerator operator ++(int) - { - Enumerator rs = *this; - ++rs; - return rs; - } - bool operator != (const Enumerator & iter) const - { - return !operator==(iter); - } - bool operator == (const Enumerator & iter) const - { - if (ptr == iter.ptr) - { - if (ptr == end) - return true; - else - return subPtr == iter.subPtr; - } - else - return false; - } - }; - auto begin() const ->Enumerator())), decltype(func(ConstructT()).begin())> - { - return Enumerator())), decltype(func(ConstructT()).begin())>(items.begin(), items.end(), func); - } - auto end() const ->Enumerator())), decltype(func(ConstructT()).begin())> - { - return Enumerator())), decltype(func(ConstructT()).begin())>(items.end(), items.end(), func); - } - }; - - template - struct EnumeratorType - { - typedef decltype(ConstructT().begin()) Type; - }; - - template - class ExtractReturnType - { - public: - static TFunc * f; - static TArg ConstructArg() {}; - typedef decltype((*f)(ConstructArg())) ReturnType; - }; - - template - class ExtractItemType - { - public: - typedef typename RemovePointer().begin())>::Type Type; - }; - - template - class Queryable - { - private: - TQueryable items; - public: - auto begin() const -> decltype(items.begin()) - { - return items.begin(); - } - auto end() const -> decltype(items.end()) - { - return items.end(); - } - public: - const TQueryable & GetItems() const - { - return items; - } - Queryable(const TQueryable & items) - : items(items) - {} - - Queryable, typename SkipQuery::Enumerator, T> Skip(int count) const - { - return Queryable, typename SkipQuery::Enumerator, T>(SkipQuery(items, count)); - } - - template - Queryable, typename ConcatQuery::Enumerator, T> Concat(const Queryable & other) const - { - return Queryable, typename ConcatQuery::Enumerator, T>(ConcatQuery(this->items, other.GetItems())); - } - - template - Queryable, typename WhereQuery::Enumerator, T> Where(const TFunc & f) const - { - return Queryable, typename WhereQuery::Enumerator, T>(WhereQuery(items, f)); - } - - template - Queryable, typename SelectQuery::Enumerator, typename RemoveReference::ReturnType>::Type> Select(const TFunc & f) const - { - return Queryable, typename SelectQuery::Enumerator, typename RemoveReference::ReturnType>::Type>(SelectQuery(items, f)); - } - - template - auto SelectMany(const TFunc & f) const ->Queryable, typename EnumeratorType>::Type, typename ExtractItemType()))>::Type> - { - return Queryable, typename EnumeratorType>::Type, typename ExtractItemType()))>::Type>(SelectManyQuery(items, f)); - } - - template - auto Aggregate(const TAggregateResult & initial, const TFunc & f) const -> decltype(f(initial, *items.begin())) - { - TAggregateResult rs = initial; - for (auto && x : items) - rs = f(rs, x); - return rs; - } - - template - bool Any(const TFunc & condition) const - { - for (auto && x : items) - if (condition(x)) - return true; - return false; - } - - template - T & First(const TFunc & condition) const - { - for (auto && x : items) - if (condition(x)) - return x; - } - - template - T Max(const TFunc & selector) const - { - return Aggregate(*items.begin(), [&](const T & v0, const T & v1) - { - return selector(v0) > selector(v1) ? v0 : v1; - }); - } - - template - T Min(const TFunc & selector) const - { - return Aggregate(*items.begin(), [&](const T & v0, const T & v1) - { - return selector(v0) < selector(v1) ? v0 : v1; - }); - } - - template - auto Sum(const TFunc & selector) const -> decltype(selector(ConstructT())) - { - decltype(selector(ConstructT())) rs(0); - for (auto && x : items) - rs = rs + selector(x); - return rs; - } - - T Max() const - { - return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 > v1 ? v0 : v1; }); - } - - T Min() const - { - return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 < v1 ? v0 : v1; }); - } - - T Sum() const - { - T rs = T(0); - for (auto && x : items) - rs = rs + x; - return rs; - } - - T Avg() const - { - T rs = T(0); - int count = 0; - for (auto && x : items) - { - rs = rs + x; - count++; - } - return rs / count; - } - - int Count() const - { - int rs = 0; - for (auto && x : items) - rs++; - return rs; - } - - List ToList() const - { - List rs; - for (auto && val : items) - rs.Add(val); - return rs; - } - }; - - - template - inline Queryable, T*, T> From(const List & list) - { - return Queryable, T*, T>(list.GetArrayView()); - } - - template - inline Queryable, T*, T> From(List && list) - { - return Queryable, T*, T>(_Move(list)); - } - - template - inline Queryable, T*, T> From(const ArrayView & list) - { - return Queryable, T*, T>(list); - } - - template - inline Queryable, T*, T> From(const Array & list) - { - return Queryable, T*, T>(list); - } - - template - inline auto From(const T & list) -> Queryable - { - return Queryable(list); - } - - template - inline Queryable, T*, T> FromSingle(const T & obj) - { - Array arr; - arr.Add(obj); - return From(arr); - } - - template - struct LinkedListView - { - typename LinkedList::Iterator start, last; - typename LinkedList::Iterator begin() const - { - return start; - } - typename LinkedList::Iterator end() const - { - return last; - } - }; - - template - inline Queryable, LinkedNode, T> From(const LinkedList & list) - { - LinkedListView view; - view.start = list.begin(); - view.last = list.end(); - return Queryable, LinkedNode, T>(view); - } - - template - struct EnumerableDictView - { - typename EnumerableDictionary::Iterator start, last; - typename EnumerableDictionary::Iterator begin() const - { - return start; - } - typename EnumerableDictionary::Iterator end() const - { - return last; - } - }; - - template - inline Queryable, typename EnumerableDictionary::Iterator, KeyValuePair> From(const EnumerableDictionary & dict) - { - EnumerableDictView view; - view.start = dict.begin(); - view.last = dict.end(); - return Queryable, typename EnumerableDictionary::Iterator, KeyValuePair>(view); - } - - template - struct EnumerableHashSetView - { - typename HashSetBase>::Iterator start, last; - typename EnumerableHashSet::Iterator begin() const - { - return start; - } - typename EnumerableHashSet::Iterator end() const - { - return last; - } - }; - - template - inline Queryable, typename HashSetBase>::Iterator, TKey> From(const EnumerableHashSet & dict) - { - EnumerableHashSetView view; - view.start = dict.begin(); - view.last = dict.end(); - return Queryable, typename HashSetBase>::Iterator, TKey>(view); - } -} - -#endif \ No newline at end of file diff --git a/source/core/memory-pool.cpp b/source/core/memory-pool.cpp deleted file mode 100644 index aec1efc37..000000000 --- a/source/core/memory-pool.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include "memory-pool.h" -#include - -namespace Slang -{ - MemoryPool::MemoryPool(unsigned char * pBuffer, int pLog2BlockSize, int numBlocks) - { - Init(pBuffer, pLog2BlockSize, numBlocks); - } - void MemoryPool::Init(unsigned char * pBuffer, int pLog2BlockSize, int numBlocks) - { - assert(pLog2BlockSize >= 1 && pLog2BlockSize <= 30); - assert(numBlocks >= 4); - buffer = pBuffer; - blockSize = 1 << pLog2BlockSize; - log2BlockSize = pLog2BlockSize; - numLevels = Math::Log2Floor(numBlocks); - freeList[0] = (FreeListNode*)buffer; - freeList[0]->NextPtr = nullptr; - freeList[0]->PrevPtr = nullptr; - used.SetMax(1 << (numLevels)); - for (int i = 1; i < MaxLevels; i++) - { - freeList[i] = nullptr; - } - } - int MemoryPool::AllocBlock(int level) - { - if (level < 0) - return -1; - if (freeList[level] == nullptr) - { - auto largeBlockAddr = AllocBlock(level - 1); - if (largeBlockAddr != -1) - { - auto block1 = (FreeListNode*)(buffer + ((largeBlockAddr ^ (1 << (numLevels - level))) << log2BlockSize)); - block1->NextPtr = nullptr; - block1->PrevPtr = nullptr; - freeList[level] = block1; - - int blockIndex = (1 << level) + (largeBlockAddr >> (numLevels-level)) - 1; - used.Add(blockIndex); - return largeBlockAddr; - } - else - return -1; - } - else - { - auto node = freeList[level]; - if (node->NextPtr) - { - node->NextPtr->PrevPtr = node->PrevPtr; - } - freeList[level] = freeList[level]->NextPtr; - int rs = (int)((unsigned char *)node - buffer) >> log2BlockSize; - int blockIndex = (1 << level) + (rs >> (numLevels - level)) - 1; - used.Add(blockIndex); - return rs; - } - } - unsigned char * MemoryPool::Alloc(int size) - { - if (size == 0) - return nullptr; - int originalSize = size; - if (size < blockSize) - size = blockSize; - int order = numLevels - (Math::Log2Ceil(size) - log2BlockSize); - assert(order >= 0 && order < MaxLevels); - - bytesAllocated += (1 << ((numLevels-order) + log2BlockSize)); - bytesWasted += (1 << ((numLevels - order) + log2BlockSize)) - originalSize; - - int blockId = AllocBlock(order); - if (blockId != -1) - return buffer + (blockId << log2BlockSize); - else - return nullptr; - } - void MemoryPool::FreeBlock(unsigned char * ptr, int level) - { - int indexInLevel = (int)(ptr - buffer) >> (numLevels - level + log2BlockSize); - int blockIndex = (1 << level) + indexInLevel - 1; - assert(used.Contains(blockIndex)); - int buddyIndex = (blockIndex & 1) ? blockIndex + 1 : blockIndex - 1; - used.Remove(blockIndex); - if (level > 0 && !used.Contains(buddyIndex)) - { - auto buddyPtr = (FreeListNode *)(buffer + ((((int)(ptr - buffer) >> log2BlockSize) ^ (1 << (numLevels - level))) << log2BlockSize)); - if (buddyPtr->PrevPtr) - { - buddyPtr->PrevPtr->NextPtr = buddyPtr->NextPtr; - } - if (buddyPtr->NextPtr) - { - buddyPtr->NextPtr->PrevPtr = buddyPtr->PrevPtr; - } - if (freeList[level] == buddyPtr) - { - freeList[level] = buddyPtr->NextPtr; - } - // recursively free parent blocks - auto parentPtr = Math::Min(buddyPtr, (FreeListNode*)ptr); - if (level > 0) - FreeBlock((unsigned char*)parentPtr, level - 1); - } - else - { - // insert to freelist - auto freeNode = (FreeListNode *)ptr; - freeNode->NextPtr = freeList[level]; - freeNode->PrevPtr = nullptr; - if (freeList[level]) - freeList[level]->PrevPtr = freeNode; - freeList[level] = freeNode; - } - } - void MemoryPool::Free(unsigned char * ptr, int size) - { - if (size == 0) - return; - int originalSize = size; - if (size < blockSize) - size = blockSize; - int level = numLevels - (Math::Log2Ceil(size) - log2BlockSize); - bytesAllocated -= (1 << ((numLevels-level) + log2BlockSize)); - bytesWasted -= (1 << ((numLevels - level) + log2BlockSize)) - originalSize; - FreeBlock(ptr, level); - } -} - diff --git a/source/core/memory-pool.h b/source/core/memory-pool.h deleted file mode 100644 index e735fb11c..000000000 --- a/source/core/memory-pool.h +++ /dev/null @@ -1,122 +0,0 @@ -#ifndef CORE_LIB_MEMORY_POOL_H -#define CORE_LIB_MEMORY_POOL_H - -#include "basic.h" -#include "int-set.h" - -namespace Slang -{ - struct MemoryBlockFields - { - unsigned int Occupied : 1; - unsigned int Order : 31; - }; - struct FreeListNode - { - FreeListNode * PrevPtr = nullptr, *NextPtr = nullptr; - }; - class MemoryPool - { - private: - static const int MaxLevels = 32; - int blockSize = 0, log2BlockSize = 0; - int numLevels = 0; - int bytesAllocated = 0; - int bytesWasted = 0; - unsigned char * buffer = nullptr; - FreeListNode * freeList[MaxLevels]; - IntSet used; - int AllocBlock(int level); - void FreeBlock(unsigned char * ptr, int level); - public: - MemoryPool(unsigned char * buffer, int log2BlockSize, int numBlocks); - MemoryPool() = default; - void Init(unsigned char * buffer, int log2BlockSize, int numBlocks); - unsigned char * Alloc(int size); - void Free(unsigned char * ptr, int size); - }; - - class OutofPoolMemoryException : public Exception - {}; - - template - class ObjectPool - { - static const int ObjectSize = sizeof(T) < 8 ? 8 : sizeof(T); - private: - struct FreeList - { - FreeList* Next; - }; - FreeList * freeList = nullptr; - int allocPtr = 0; - int poolSize = 0; - void * buffer = 0; - T * GetFreeObject() - { - if (freeList) - { - auto rs = (T*)freeList; - freeList = freeList->Next; - return rs; - } - return nullptr; - } - public: - ObjectPool() - { - freeList = nullptr; - allocPtr = 0; - buffer = malloc(PoolSize * ObjectSize); - } - - void Close() - { - free(buffer); - } - - void Free(T * obj) - { - auto newList = (FreeList*)obj; - newList->Next = freeList; - freeList = newList; - } - - void * Buffer() - { - return buffer; - } - - T * Alloc() - { - auto rs = GetFreeObject(); - if (!rs) - { - if (allocPtr < PoolSize) - { - rs = (T*)buffer + allocPtr; - allocPtr++; - } - } - if (!rs) - { - throw OutofPoolMemoryException(); - } - return rs; - } - }; -}; - -#define USE_POOL_ALLOCATOR(T, PoolSize) \ -private:\ - static Slang::ObjectPool _pool;\ -public:\ - void * operator new(std::size_t) { return _pool.Alloc(); } \ - void operator delete(void * ptr) {_pool.Free((T*)ptr); }\ - int GetObjectId() { return (int)(this - (T*)_pool.Buffer()); }\ - static void ClosePool(); -#define IMPL_POOL_ALLOCATOR(T, PoolSize) \ -Slang::ObjectPool T::_pool;\ -void T::ClosePool() { _pool.Close(); } - -#endif \ No newline at end of file diff --git a/source/core/slang-io.cpp b/source/core/slang-io.cpp index 5675bf24b..dc2793fc4 100644 --- a/source/core/slang-io.cpp +++ b/source/core/slang-io.cpp @@ -10,13 +10,6 @@ namespace Slang { - CommandLineWriter * currentCommandWriter = nullptr; - - void SetCommandLineWriter(CommandLineWriter * writer) - { - currentCommandWriter = writer; - } - bool File::Exists(const String & fileName) { #ifdef _WIN32 diff --git a/source/core/slang-io.h b/source/core/slang-io.h index fb9583c67..869fad873 100644 --- a/source/core/slang-io.h +++ b/source/core/slang-io.h @@ -35,26 +35,6 @@ namespace Slang static String Combine(const String & path1, const String & path2, const String & path3); static bool CreateDir(const String & path); }; - - class CommandLineWriter : public Object - { - public: - virtual void Write(const String & text) = 0; - }; - - void SetCommandLineWriter(CommandLineWriter * writer); - - extern CommandLineWriter * currentCommandWriter; - template - void uiprintf(const wchar_t * format, Args... args) - { - if (currentCommandWriter) - { - char buffer[1024]; - snprintf(buffer, 1024, format, args...); - currentCommandWriter->Write(buffer); - } - } } #endif \ No newline at end of file diff --git a/source/core/slang-math.cpp b/source/core/slang-math.cpp deleted file mode 100644 index 76be23f30..000000000 --- a/source/core/slang-math.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "slang-math.h" - -#if 0 -namespace Slang -{ - const float Math::Pi = 3.141592654f; -} -#endif diff --git a/source/core/stream.h b/source/core/stream.h index 1a5f96307..0f5991cb7 100644 --- a/source/core/stream.h +++ b/source/core/stream.h @@ -32,9 +32,10 @@ namespace Slang Start, End, Current }; - class Stream : public Slang::Object + class Stream { public: + virtual ~Stream() {} virtual Int64 GetPosition()=0; virtual void Seek(SeekOrigin origin, Int64 offset)=0; virtual Int64 Read(void * buffer, Int64 length) = 0; @@ -45,147 +46,6 @@ namespace Slang virtual void Close() = 0; }; - class BinaryReader - { - private: - RefPtr stream; - inline void Throw(Int64 val) - { - if (val == 0) - throw IOException("read operation failed."); - } - public: - BinaryReader(RefPtr stream) - { - this->stream = stream; - } - Stream * GetStream() - { - return stream.Ptr(); - } - void ReleaseStream() - { - stream.Release(); - } - template - void Read(T * buffer, int count) - { - stream->Read(buffer, sizeof(T)*(Int64)count); - } - template - void Read(T & buffer) - { - Throw(stream->Read(&buffer, sizeof(T))); - } - template - void Read(List & buffer) - { - int count = ReadInt32(); - buffer.SetSize(count); - Read(buffer.Buffer(), count); - } - void Read(String & buffer) - { - buffer = ReadString(); - } - int ReadInt32() - { - int rs; - Throw(stream->Read(&rs, sizeof(int))); - return rs; - } - short ReadInt16() - { - short rs; - Throw(stream->Read(&rs, sizeof(short))); - return rs; - } - Int64 ReadInt64() - { - Int64 rs; - Throw(stream->Read(&rs, sizeof(Int64))); - return rs; - } - float ReadFloat() - { - float rs; - Throw(stream->Read(&rs, sizeof(float))); - return rs; - } - double ReadDouble() - { - double rs; - Throw(stream->Read(&rs, sizeof(double))); - return rs; - } - char ReadChar() - { - char rs; - Throw(stream->Read(&rs, sizeof(char))); - return rs; - } - String ReadString() - { - int len = ReadInt32(); - char * buffer = new char[len+1]; - try - { - Throw(stream->Read(buffer, len)); - } - catch(IOException & e) - { - delete [] buffer; - throw e; - } - buffer[len] = 0; - return String::FromBuffer(buffer, len); - } - }; - - class BinaryWriter - { - private: - RefPtr stream; - public: - BinaryWriter(RefPtr stream) - { - this->stream = stream; - } - Stream * GetStream() - { - return stream.Ptr(); - } - template - void Write(const T& val) - { - stream->Write(&val, sizeof(T)); - } - template - void Write(T * buffer, int count) - { - stream->Write(buffer, sizeof(T)*(Int64)count); - } - template - void Write(const List & list) - { - Write(list.Count()); - stream->Write(list.Buffer(), sizeof(T)*list.Count()); - } - void Write(const String & str) - { - Write(str.Length()); - Write(str.Buffer(), str.Length()); - } - void ReleaseStream() - { - stream.Release(); - } - void Close() - { - stream->Close(); - } - }; - enum class FileMode { Create, Open, CreateNew, Append @@ -222,105 +82,6 @@ namespace Slang virtual void Close(); virtual bool IsEnd(); }; - - class MemoryStream : public Stream - { - private: - List writeBuffer; - ArrayView readBuffer; - int ptr = 0; - bool isReadStream; - public: - MemoryStream() - { - isReadStream = false; - } - MemoryStream(unsigned char * mem, int length) - { - isReadStream = true; - readBuffer = MakeArrayView(mem, length); - } - MemoryStream(ArrayView source) - { - isReadStream = true; - readBuffer = source; - } - virtual Int64 GetPosition() - { - return ptr; - } - virtual void Seek(SeekOrigin origin, Int64 offset) - { - if (origin == SeekOrigin::Start) - ptr = (int)offset; - else if (origin == SeekOrigin::End) - { - if (isReadStream) - ptr = readBuffer.Count() + (int)offset; - else - ptr = writeBuffer.Count() + (int)offset; - } - } - virtual Int64 Read(void * pbuffer, Int64 length) - { - Int64 i; - for (i = 0; i < length; i++) - { - if (ptr + i < readBuffer.Count()) - { - ((unsigned char*)pbuffer)[i] = readBuffer[(int)(ptr + i)]; - } - else - break; - } - return i; - } - virtual Int64 Write(const void * pbuffer, Int64 length) - { - writeBuffer.SetSize(ptr); - if (pbuffer) - writeBuffer.AddRange((unsigned char *)pbuffer, (int)length); - else - for (auto i = 0; i < length; i++) - writeBuffer.Add(0); - ptr = writeBuffer.Count(); - return length; - } - virtual bool CanRead() - { - return isReadStream; - } - virtual bool CanWrite() - { - return !isReadStream; - } - virtual void Close() - { - writeBuffer.SetSize(0); - writeBuffer.Compress(); - } - virtual bool IsEnd() - { - if (isReadStream) - return ptr >= readBuffer.Count(); - else - return ptr == writeBuffer.Count(); - } - void * GetBuffer() - { - if (isReadStream) - return readBuffer.Buffer(); - else - return writeBuffer.Buffer(); - } - int GetBufferSize() - { - if (isReadStream) - return readBuffer.Count(); - else - return writeBuffer.Count(); - } - }; } #endif diff --git a/source/core/text-io.h b/source/core/text-io.h index e0881cb91..acdaf0b9d 100644 --- a/source/core/text-io.h +++ b/source/core/text-io.h @@ -9,14 +9,14 @@ namespace Slang using Slang::List; using Slang::_EndLine; - class TextReader : public Slang::Object + class TextReader { protected: char decodedChar[5]; int decodedCharPtr = 0, decodedCharSize = 0; virtual void ReadChar() = 0; public: - ~TextReader() + virtual ~TextReader() { Close(); } @@ -45,10 +45,10 @@ namespace Slang } }; - class TextWriter : public Slang::Object + class TextWriter { public: - ~TextWriter() + virtual ~TextWriter() { Close(); } @@ -267,7 +267,7 @@ namespace Slang } }; - class StreamReader : public TextReader + class StreamReader : public TextReader { private: RefPtr stream; @@ -311,7 +311,6 @@ namespace Slang stream.Release(); } }; - } #endif -- cgit v1.2.3