summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-06-15 13:24:25 -0700
committerTim Foley <tfoley@nvidia.com>2017-06-15 13:24:25 -0700
commit205187b561c3b31fa931e73e8f7263f0c4b1de41 (patch)
tree7bd2cd5ae3c14416b71ef8319ff02ace429d1132
parent517513645afb8eaf4841e7b7035f1ba3a9c7cd57 (diff)
Rename `CoreLib::*` to `Slang`
Getting rid of more namespace complexity and stripping things down to the basics. This also gets rid of some dead code in the "core" library.
-rw-r--r--source/core/allocator.h79
-rw-r--r--source/core/array-view.h202
-rw-r--r--source/core/array.h219
-rw-r--r--source/core/basic.h6
-rw-r--r--source/core/common.h37
-rw-r--r--source/core/core.natvis18
-rw-r--r--source/core/core.vcxproj1
-rw-r--r--source/core/dictionary.h1695
-rw-r--r--source/core/exception.h167
-rw-r--r--source/core/func.h216
-rw-r--r--source/core/hash.h156
-rw-r--r--source/core/int-set.h285
-rw-r--r--source/core/link.h572
-rw-r--r--source/core/linq.h1062
-rw-r--r--source/core/list.h1033
-rw-r--r--source/core/memory-pool.cpp211
-rw-r--r--source/core/memory-pool.h194
-rw-r--r--source/core/slang-io.cpp234
-rw-r--r--source/core/slang-io.h77
-rw-r--r--source/core/slang-math.cpp11
-rw-r--r--source/core/slang-math.h365
-rw-r--r--source/core/slang-string.cpp251
-rw-r--r--source/core/slang-string.h1287
-rw-r--r--source/core/smart-pointer.h755
-rw-r--r--source/core/stream.cpp364
-rw-r--r--source/core/stream.h589
-rw-r--r--source/core/text-io.cpp511
-rw-r--r--source/core/text-io.h543
-rw-r--r--source/core/type-traits.h69
-rw-r--r--source/slang/compiler.cpp3
-rw-r--r--source/slang/compiler.h2
-rw-r--r--source/slang/diagnostics.cpp2
-rw-r--r--source/slang/diagnostics.h4
-rw-r--r--source/slang/emit.h2
-rw-r--r--source/slang/lexer.h2
-rw-r--r--source/slang/preprocessor.cpp10
-rw-r--r--source/slang/preprocessor.h14
-rw-r--r--source/slang/slang-stdlib.cpp2
-rw-r--r--source/slang/slang-stdlib.h6
-rw-r--r--source/slang/slang.cpp15
-rw-r--r--source/slang/source-loc.h2
-rw-r--r--source/slang/syntax.cpp6
-rw-r--r--source/slang/syntax.h5
-rw-r--r--source/slang/token.h2
-rw-r--r--source/slangc/main.cpp5
-rw-r--r--tools/slang-test/main.cpp31
-rw-r--r--tools/slang-test/os.cpp18
-rw-r--r--tools/slang-test/os.h38
48 files changed, 5525 insertions, 5853 deletions
diff --git a/source/core/allocator.h b/source/core/allocator.h
index d4dfcf5a9..46550a054 100644
--- a/source/core/allocator.h
+++ b/source/core/allocator.h
@@ -3,60 +3,57 @@
#include <stdlib.h>
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ inline void * AlignedAlloc(size_t size, size_t alignment)
{
- inline void * AlignedAlloc(size_t size, size_t alignment)
- {
#ifdef _MSC_VER
- return _aligned_malloc(size, alignment);
+ return _aligned_malloc(size, alignment);
#else
- void * rs = 0;
- int succ = posix_memalign(&rs, alignment, size);
- if (succ!=0)
- rs = 0;
- return rs;
+ void * rs = 0;
+ int succ = posix_memalign(&rs, alignment, size);
+ if (succ!=0)
+ rs = 0;
+ return rs;
#endif
- }
+ }
- inline void AlignedFree(void * ptr)
- {
+ inline void AlignedFree(void * ptr)
+ {
#ifdef _MSC_VER
- _aligned_free(ptr);
+ _aligned_free(ptr);
#else
- free(ptr);
+ free(ptr);
#endif
- }
+ }
- class StandardAllocator
+ class StandardAllocator
+ {
+ public:
+ // not really called
+ void * Alloc(size_t size)
{
- public:
- // not really called
- void * Alloc(size_t size)
- {
- return malloc(size);
- }
- void Free(void * ptr)
- {
- return free(ptr);
- }
- };
+ return malloc(size);
+ }
+ void Free(void * ptr)
+ {
+ return free(ptr);
+ }
+ };
- template<int alignment>
- class AlignedAllocator
+ template<int alignment>
+ class AlignedAllocator
+ {
+ public:
+ void * Alloc(size_t size)
{
- public:
- void * Alloc(size_t size)
- {
- return AlignedAlloc(size, alignment);
- }
- void Free(void * ptr)
- {
- return AlignedFree(ptr);
- }
- };
- }
+ return AlignedAlloc(size, alignment);
+ }
+ void Free(void * ptr)
+ {
+ return AlignedFree(ptr);
+ }
+ };
}
#endif \ No newline at end of file
diff --git a/source/core/array-view.h b/source/core/array-view.h
index 201ef2a79..4e0057cdb 100644
--- a/source/core/array-view.h
+++ b/source/core/array-view.h
@@ -3,129 +3,127 @@
#include "Exception.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ template<typename T>
+ class ArrayView
{
- template<typename T>
- class ArrayView
+ private:
+ T * _buffer;
+ int _count;
+ int stride;
+ public:
+ T* begin() const
{
- private:
- T * _buffer;
- int _count;
- int stride;
- public:
- T* begin() const
- {
- return _buffer;
- }
- T* end() const
- {
- return (T*)((char*)_buffer + _count*stride);
- }
- public:
- ArrayView()
- {
- _buffer = 0;
- _count = 0;
- }
- ArrayView(const T & singleObj)
- {
- SetData((T*)&singleObj, 1, sizeof(T));
- }
- ArrayView(T * buffer, int count)
- {
- SetData(buffer, count, sizeof(T));
- }
- ArrayView(void * buffer, int count, int _stride)
- {
- SetData(buffer, count, _stride);
- }
- void SetData(void * buffer, int count, int _stride)
- {
- this->_buffer = (T*)buffer;
- this->_count = count;
- this->stride = _stride;
- }
- inline int GetCapacity() const
- {
- return _count;
- }
- inline int Count() const
- {
- return _count;
- }
+ return _buffer;
+ }
+ T* end() const
+ {
+ return (T*)((char*)_buffer + _count*stride);
+ }
+ public:
+ ArrayView()
+ {
+ _buffer = 0;
+ _count = 0;
+ }
+ ArrayView(const T & singleObj)
+ {
+ SetData((T*)&singleObj, 1, sizeof(T));
+ }
+ ArrayView(T * buffer, int count)
+ {
+ SetData(buffer, count, sizeof(T));
+ }
+ ArrayView(void * buffer, int count, int _stride)
+ {
+ SetData(buffer, count, _stride);
+ }
+ void SetData(void * buffer, int count, int _stride)
+ {
+ this->_buffer = (T*)buffer;
+ this->_count = count;
+ this->stride = _stride;
+ }
+ inline int GetCapacity() const
+ {
+ return _count;
+ }
+ inline int Count() const
+ {
+ return _count;
+ }
- inline T & operator [](int id) const
- {
+ inline T & operator [](int id) const
+ {
#if _DEBUG
- if (id >= _count || id < 0)
- throw IndexOutofRangeException("Operator[]: Index out of Range.");
+ if (id >= _count || id < 0)
+ throw IndexOutofRangeException("Operator[]: Index out of Range.");
#endif
- return *(T*)((char*)_buffer+id*stride);
- }
+ return *(T*)((char*)_buffer+id*stride);
+ }
- inline T* Buffer() const
- {
- return _buffer;
- }
+ inline T* Buffer() const
+ {
+ return _buffer;
+ }
- template<typename T2>
- int IndexOf(const T2 & val) const
+ template<typename T2>
+ int IndexOf(const T2 & val) const
+ {
+ for (int i = 0; i < _count; i++)
{
- for (int i = 0; i < _count; i++)
- {
- if (*(T*)((char*)_buffer + i*stride) == val)
- return i;
- }
- return -1;
+ if (*(T*)((char*)_buffer + i*stride) == val)
+ return i;
}
+ return -1;
+ }
- template<typename T2>
- int LastIndexOf(const T2 & val) const
+ template<typename T2>
+ int LastIndexOf(const T2 & val) const
+ {
+ for (int i = _count - 1; i >= 0; i--)
{
- for (int i = _count - 1; i >= 0; i--)
- {
- if (*(T*)((char*)_buffer + i*stride) == val)
- return i;
- }
- return -1;
+ if (*(T*)((char*)_buffer + i*stride) == val)
+ return i;
}
+ return -1;
+ }
- template<typename Func>
- int FindFirst(const Func & predicate) const
+ template<typename Func>
+ int FindFirst(const Func & predicate) const
+ {
+ for (int i = 0; i < _count; i++)
{
- for (int i = 0; i < _count; i++)
- {
- if (predicate(_buffer[i]))
- return i;
- }
- return -1;
+ if (predicate(_buffer[i]))
+ return i;
}
+ return -1;
+ }
- template<typename Func>
- int FindLast(const Func & predicate) const
+ template<typename Func>
+ int FindLast(const Func & predicate) const
+ {
+ for (int i = _count - 1; i >= 0; i--)
{
- for (int i = _count - 1; i >= 0; i--)
- {
- if (predicate(_buffer[i]))
- return i;
- }
- return -1;
+ if (predicate(_buffer[i]))
+ return i;
}
- };
-
- template<typename T>
- ArrayView<T> MakeArrayView(const T & obj)
- {
- return ArrayView<T>(obj);
+ return -1;
}
+ };
+
+ template<typename T>
+ ArrayView<T> MakeArrayView(const T & obj)
+ {
+ return ArrayView<T>(obj);
+ }
- template<typename T>
- ArrayView<T> MakeArrayView(T * buffer, int count)
- {
- return ArrayView<T>(buffer, count);
- }
+ template<typename T>
+ ArrayView<T> MakeArrayView(T * buffer, int count)
+ {
+ return ArrayView<T>(buffer, count);
}
}
+
#endif \ No newline at end of file
diff --git a/source/core/array.h b/source/core/array.h
index b6dbeab07..96508879f 100644
--- a/source/core/array.h
+++ b/source/core/array.h
@@ -4,142 +4,139 @@
#include "exception.h"
#include "array-view.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ template<typename T, int size>
+ class Array
{
- template<typename T, int size>
- class Array
- {
- private:
- T _buffer[size];
- int _count = 0;
- public:
- T* begin() const
- {
- return (T*)_buffer;
- }
- T* end() const
- {
- return (T*)_buffer + _count;
- }
- public:
- inline int GetCapacity() const
- {
- return size;
- }
- inline int Count() const
- {
- return _count;
- }
- inline T & First() const
- {
- return const_cast<T&>(_buffer[0]);
- }
- inline T & Last() const
- {
- return const_cast<T&>(_buffer[_count - 1]);
- }
- inline void SetSize(int newSize)
- {
+ private:
+ T _buffer[size];
+ int _count = 0;
+ public:
+ T* begin() const
+ {
+ return (T*)_buffer;
+ }
+ T* end() const
+ {
+ return (T*)_buffer + _count;
+ }
+ public:
+ inline int GetCapacity() const
+ {
+ return size;
+ }
+ inline int Count() const
+ {
+ return _count;
+ }
+ inline T & First() const
+ {
+ return const_cast<T&>(_buffer[0]);
+ }
+ inline T & Last() const
+ {
+ return const_cast<T&>(_buffer[_count - 1]);
+ }
+ inline void SetSize(int newSize)
+ {
#ifdef _DEBUG
- if (newSize > size)
- throw IndexOutofRangeException("size too large.");
+ if (newSize > size)
+ throw IndexOutofRangeException("size too large.");
#endif
- _count = newSize;
- }
- inline void Add(const T & item)
- {
+ _count = newSize;
+ }
+ inline void Add(const T & item)
+ {
#ifdef _DEBUG
- if (_count == size)
- throw IndexOutofRangeException("out of range access to static array.");
+ if (_count == size)
+ throw IndexOutofRangeException("out of range access to static array.");
#endif
- _buffer[_count++] = item;
- }
- inline void Add(T && item)
- {
+ _buffer[_count++] = item;
+ }
+ inline void Add(T && item)
+ {
#ifdef _DEBUG
- if (_count == size)
- throw IndexOutofRangeException("out of range access to static array.");
+ if (_count == size)
+ throw IndexOutofRangeException("out of range access to static array.");
#endif
- _buffer[_count++] = _Move(item);
- }
+ _buffer[_count++] = _Move(item);
+ }
- inline T & operator [](int id) const
- {
+ inline T & operator [](int id) const
+ {
#if _DEBUG
- if (id >= _count || id < 0)
- throw IndexOutofRangeException("Operator[]: Index out of Range.");
+ if (id >= _count || id < 0)
+ throw IndexOutofRangeException("Operator[]: Index out of Range.");
#endif
- return ((T*)_buffer)[id];
- }
-
- inline T* Buffer() const
- {
- return (T*)_buffer;
- }
+ return ((T*)_buffer)[id];
+ }
- inline void Clear()
- {
- _count = 0;
- }
+ inline T* Buffer() const
+ {
+ return (T*)_buffer;
+ }
- template<typename T2>
- int IndexOf(const T2 & val) const
- {
- for (int i = 0; i < _count; i++)
- {
- if (_buffer[i] == val)
- return i;
- }
- return -1;
- }
+ inline void Clear()
+ {
+ _count = 0;
+ }
- template<typename T2>
- int LastIndexOf(const T2 & val) const
+ template<typename T2>
+ int IndexOf(const T2 & val) const
+ {
+ for (int i = 0; i < _count; i++)
{
- for (int i = _count - 1; i >= 0; i--)
- {
- if (_buffer[i] == val)
- return i;
- }
- return -1;
+ if (_buffer[i] == val)
+ return i;
}
+ return -1;
+ }
- inline ArrayView<T> GetArrayView() const
- {
- return ArrayView<T>((T*)_buffer, _count);
- }
- inline ArrayView<T> GetArrayView(int start, int count) const
+ template<typename T2>
+ int LastIndexOf(const T2 & val) const
+ {
+ for (int i = _count - 1; i >= 0; i--)
{
- return ArrayView<T>((T*)_buffer + start, count);
+ if (_buffer[i] == val)
+ return i;
}
- };
+ return -1;
+ }
- template<typename T, typename ...TArgs>
- struct FirstType
+ inline ArrayView<T> GetArrayView() const
{
- typedef T type;
- };
+ return ArrayView<T>((T*)_buffer, _count);
+ }
+ inline ArrayView<T> GetArrayView(int start, int count) const
+ {
+ return ArrayView<T>((T*)_buffer + start, count);
+ }
+ };
+ template<typename T, typename ...TArgs>
+ struct FirstType
+ {
+ typedef T type;
+ };
- template<typename T, int size>
- void InsertArray(Array<T, size> &) {}
- template<typename T, typename ...TArgs, int size>
- void InsertArray(Array<T, size> & arr, const T & val, TArgs... args)
- {
- arr.Add(val);
- InsertArray(arr, args...);
- }
+ template<typename T, int size>
+ void InsertArray(Array<T, size> &) {}
- template<typename ...TArgs>
- auto MakeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::type, sizeof...(args)>
- {
- Array<typename FirstType<TArgs...>::type, sizeof...(args)> rs;
- InsertArray(rs, args...);
- return rs;
- }
+ template<typename T, typename ...TArgs, int size>
+ void InsertArray(Array<T, size> & arr, const T & val, TArgs... args)
+ {
+ arr.Add(val);
+ InsertArray(arr, args...);
+ }
+
+ template<typename ...TArgs>
+ auto MakeArray(TArgs ...args) -> Array<typename FirstType<TArgs...>::type, sizeof...(args)>
+ {
+ Array<typename FirstType<TArgs...>::type, sizeof...(args)> rs;
+ InsertArray(rs, args...);
+ return rs;
}
}
diff --git a/source/core/basic.h b/source/core/basic.h
index 5b0e05e7b..c5ed11091 100644
--- a/source/core/basic.h
+++ b/source/core/basic.h
@@ -10,12 +10,6 @@
#include "smart-pointer.h"
#include "exception.h"
#include "dictionary.h"
-#include "func.h"
#include "linq.h"
-namespace CoreLib
-{
- using namespace Basic;
-}
-
#endif \ No newline at end of file
diff --git a/source/core/common.h b/source/core/common.h
index 517b018a7..ac8456009 100644
--- a/source/core/common.h
+++ b/source/core/common.h
@@ -11,7 +11,7 @@
#define VARIADIC_TEMPLATE
-namespace CoreLib
+namespace Slang
{
typedef int64_t Int64;
typedef unsigned short Word;
@@ -20,28 +20,25 @@ namespace CoreLib
#else
typedef int PtrInt;
#endif
- namespace Basic
+ class Object
{
- class Object
- {
- public:
- virtual ~Object()
- {}
- };
+ public:
+ virtual ~Object()
+ {}
+ };
- template <typename T>
- inline T&& _Move(T & obj)
- {
- return static_cast<T&&>(obj);
- }
+ template <typename T>
+ inline T&& _Move(T & obj)
+ {
+ return static_cast<T&&>(obj);
+ }
- template <typename T>
- inline void Swap(T & v0, T & v1)
- {
- T tmp = _Move(v0);
- v0 = _Move(v1);
- v1 = _Move(tmp);
- }
+ template <typename T>
+ inline void Swap(T & v0, T & v1)
+ {
+ T tmp = _Move(v0);
+ v0 = _Move(v1);
+ v1 = _Move(tmp);
}
}
diff --git a/source/core/core.natvis b/source/core/core.natvis
index b856d31db..271e39663 100644
--- a/source/core/core.natvis
+++ b/source/core/core.natvis
@@ -2,12 +2,12 @@
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
-<Type Name="CoreLib::Basic::String">
+<Type Name="Slang::String">
<DisplayString>{buffer.pointer,s}</DisplayString>
<StringView>buffer.pointer,s</StringView>
</Type>
-<Type Name="CoreLib::Basic::ArrayView&lt;*&gt;">
+<Type Name="Slang::ArrayView&lt;*&gt;">
<DisplayString>{{ size={_count} }}</DisplayString>
<Expand>
<Item Name="[size]">_count</Item>
@@ -18,7 +18,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::List&lt;*&gt;">
+<Type Name="Slang::List&lt;*&gt;">
<DisplayString>{{ size={_count} }}</DisplayString>
<Expand>
<Item Name="[size]">_count</Item>
@@ -31,7 +31,7 @@
</Type>
-<Type Name="CoreLib::Basic::Array&lt;*,*&gt;">
+<Type Name="Slang::Array&lt;*,*&gt;">
<DisplayString>{{ size={_count} }}</DisplayString>
<Expand>
<Item Name="[size]">_count</Item>
@@ -42,7 +42,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::LinkedList&lt;*&gt;">
+<Type Name="Slang::LinkedList&lt;*&gt;">
<DisplayString>{{ size={FCount} }}</DisplayString>
<Expand>
<LinkedListItems>
@@ -54,7 +54,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::Dictionary&lt;*,*&gt;">
+<Type Name="Slang::Dictionary&lt;*,*&gt;">
<DisplayString>{{ size={_count} }}</DisplayString>
<Expand>
<Item Name="[size]">_count</Item>
@@ -66,7 +66,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::EnumerableDictionary&lt;*,*&gt;">
+<Type Name="Slang::EnumerableDictionary&lt;*,*&gt;">
<DisplayString>{{ size={_count} }}</DisplayString>
<Expand>
<Item Name="[size]">_count</Item>
@@ -80,7 +80,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::EnumerableHashSet&lt;*,*&gt;">
+<Type Name="Slang::EnumerableHashSet&lt;*,*&gt;">
<DisplayString>{{ size={dict._count} }}</DisplayString>
<Expand>
<Item Name="[size]">dict._count</Item>
@@ -94,7 +94,7 @@
</Expand>
</Type>
-<Type Name="CoreLib::Basic::RefPtrImpl&lt;*,*,*&gt;">
+<Type Name="Slang::RefPtrImpl&lt;*,*,*&gt;">
<SmartPointer Usage="Minimal">pointer</SmartPointer>
<DisplayString Condition="pointer == 0">empty</DisplayString>
<DisplayString Condition="pointer != 0">RefPtr {*pointer}</DisplayString>
diff --git a/source/core/core.vcxproj b/source/core/core.vcxproj
index d1204ff95..bba6102b7 100644
--- a/source/core/core.vcxproj
+++ b/source/core/core.vcxproj
@@ -26,7 +26,6 @@
<ClInclude Include="common.h" />
<ClInclude Include="dictionary.h" />
<ClInclude Include="exception.h" />
- <ClInclude Include="func.h" />
<ClInclude Include="hash.h" />
<ClInclude Include="int-set.h" />
<ClInclude Include="link.h" />
diff --git a/source/core/dictionary.h b/source/core/dictionary.h
index 77f0df515..c052685fd 100644
--- a/source/core/dictionary.h
+++ b/source/core/dictionary.h
@@ -7,1012 +7,1009 @@
#include "slang-math.h"
#include "hash.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ template<typename TKey, typename TValue>
+ class KeyValuePair
{
- template<typename TKey, typename TValue>
- class KeyValuePair
+ public:
+ TKey Key;
+ TValue Value;
+ KeyValuePair()
+ {}
+ KeyValuePair(const TKey & key, const TValue & value)
{
- public:
- TKey Key;
- TValue Value;
- KeyValuePair()
- {}
- KeyValuePair(const TKey & key, const TValue & value)
- {
- Key = key;
- Value = value;
- }
- KeyValuePair(TKey && key, TValue && value)
- {
- Key = _Move(key);
- Value = _Move(value);
- }
- KeyValuePair(TKey && key, const TValue & value)
- {
- Key = _Move(key);
- Value = value;
- }
- KeyValuePair(const KeyValuePair<TKey, TValue> & _that)
- {
- Key = _that.Key;
- Value = _that.Value;
- }
- KeyValuePair(KeyValuePair<TKey, TValue> && _that)
- {
- operator=(_Move(_that));
- }
- KeyValuePair & operator=(KeyValuePair<TKey, TValue> && that)
- {
- Key = _Move(that.Key);
- Value = _Move(that.Value);
- return *this;
- }
- KeyValuePair & operator=(const KeyValuePair<TKey, TValue> & that)
- {
- Key = that.Key;
- Value = that.Value;
- return *this;
- }
- int GetHashCode()
- {
- return GetHashCode(Key);
- }
- };
-
- template<typename TKey, typename TValue>
- inline KeyValuePair<TKey, TValue> KVPair(const TKey & k, const TValue & v)
+ Key = key;
+ Value = value;
+ }
+ KeyValuePair(TKey && key, TValue && value)
+ {
+ Key = _Move(key);
+ Value = _Move(value);
+ }
+ KeyValuePair(TKey && key, const TValue & value)
+ {
+ Key = _Move(key);
+ Value = value;
+ }
+ KeyValuePair(const KeyValuePair<TKey, TValue> & _that)
+ {
+ Key = _that.Key;
+ Value = _that.Value;
+ }
+ KeyValuePair(KeyValuePair<TKey, TValue> && _that)
+ {
+ operator=(_Move(_that));
+ }
+ KeyValuePair & operator=(KeyValuePair<TKey, TValue> && that)
+ {
+ Key = _Move(that.Key);
+ Value = _Move(that.Value);
+ return *this;
+ }
+ KeyValuePair & operator=(const KeyValuePair<TKey, TValue> & that)
{
- return KeyValuePair<TKey, TValue>(k, v);
+ Key = that.Key;
+ Value = that.Value;
+ return *this;
}
+ int GetHashCode()
+ {
+ return GetHashCode(Key);
+ }
+ };
+
+ template<typename TKey, typename TValue>
+ inline KeyValuePair<TKey, TValue> KVPair(const TKey & k, const TValue & v)
+ {
+ return KeyValuePair<TKey, TValue>(k, v);
+ }
- const float MaxLoadFactor = 0.7f;
+ const float MaxLoadFactor = 0.7f;
- template<typename TKey, typename TValue>
- class Dictionary
+ template<typename TKey, typename TValue>
+ class Dictionary
+ {
+ friend class Iterator;
+ friend class ItemProxy;
+ private:
+ inline int GetProbeOffset(int /*probeId*/) const
{
- friend class Iterator;
- friend class ItemProxy;
- private:
- inline int GetProbeOffset(int /*probeId*/) const
- {
- // quadratic probing
- return 1;
- }
- private:
- int bucketSizeMinusOne, shiftBits;
- int _count;
- IntSet marks;
- KeyValuePair<TKey, TValue>* hashMap;
- void Free()
- {
- if (hashMap)
- delete[] hashMap;
- hashMap = 0;
- }
- 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)
+ // quadratic probing
+ return 1;
+ }
+ private:
+ int bucketSizeMinusOne, shiftBits;
+ int _count;
+ IntSet marks;
+ KeyValuePair<TKey, TValue>* hashMap;
+ void Free()
+ {
+ if (hashMap)
+ delete[] hashMap;
+ hashMap = 0;
+ }
+ 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()
{
- if (val)
- marks.Add((pos << 1) + 1);
- else
- marks.Remove((pos << 1) + 1);
+ ObjectPosition = -1;
+ InsertionPosition = -1;
}
- inline void SetEmpty(int pos, bool val)
+ FindPositionResult(int objPos, int insertPos)
{
- if (val)
- marks.Remove((pos << 1));
- else
- marks.Add((pos << 1));
+ ObjectPosition = objPos;
+ InsertionPosition = insertPos;
}
- struct FindPositionResult
- {
- int ObjectPosition;
- int InsertionPosition;
- FindPositionResult()
- {
- ObjectPosition = -1;
- InsertionPosition = -1;
- }
- FindPositionResult(int objPos, int insertPos)
- {
- ObjectPosition = objPos;
- InsertionPosition = insertPos;
- }
- };
- template<typename T>
- inline int GetHashPos(T & key) const
- {
- return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits;
- }
- template<typename T>
- FindPositionResult FindPosition(const T & key) const
+ };
+ template<typename T>
+ inline int GetHashPos(T & key) const
+ {
+ return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits;
+ }
+ template<typename T>
+ FindPositionResult FindPosition(const T & key) const
+ {
+ int hashPos = GetHashPos((T&)key);
+ int insertPos = -1;
+ int numProbes = 0;
+ while (numProbes <= bucketSizeMinusOne)
{
- int hashPos = GetHashPos((T&)key);
- int insertPos = -1;
- int numProbes = 0;
- while (numProbes <= bucketSizeMinusOne)
+ if (IsEmpty(hashPos))
{
- 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].Key == key)
- {
- return FindPositionResult(hashPos, -1);
- }
- numProbes++;
- hashPos = (hashPos + GetProbeOffset(numProbes)) & bucketSizeMinusOne;
+ if (insertPos == -1)
+ return FindPositionResult(-1, hashPos);
+ else
+ return FindPositionResult(-1, insertPos);
}
- 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<TKey, TValue> && kvPair, int pos)
- {
- hashMap[pos] = _Move(kvPair);
- SetEmpty(pos, false);
- SetDeleted(pos, false);
- return hashMap[pos].Value;
- }
- void Rehash()
- {
- if (bucketSizeMinusOne == -1 || _count / (float)bucketSizeMinusOne >= MaxLoadFactor)
+ else if (IsDeleted(hashPos))
{
- int newSize = (bucketSizeMinusOne + 1) * 2;
- int newShiftBits = shiftBits - 1;
- if (newSize == 0)
- {
- newSize = 16;
- newShiftBits = 28;
- }
- Dictionary<TKey, TValue> newDict;
- newDict.shiftBits = newShiftBits;
- newDict.bucketSizeMinusOne = newSize - 1;
- newDict.hashMap = new KeyValuePair<TKey, TValue>[newSize];
- newDict.marks.SetMax(newSize * 2);
- if (hashMap)
- {
- for (auto & kvPair : *this)
- {
- newDict.Add(_Move(kvPair));
- }
- }
- *this = _Move(newDict);
+ if (insertPos == -1)
+ insertPos = hashPos;
}
- }
-
- bool AddIfNotExists(KeyValuePair<TKey, TValue> && kvPair)
- {
- Rehash();
- auto pos = FindPosition(kvPair.Key);
- if (pos.ObjectPosition != -1)
- return false;
- else if (pos.InsertionPosition != -1)
+ else if (hashMap[hashPos].Key == key)
{
- _count++;
- _Insert(_Move(kvPair), pos.InsertionPosition);
- return true;
+ return FindPositionResult(hashPos, -1);
}
- else
- throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
- }
- void Add(KeyValuePair<TKey, TValue> && kvPair)
- {
- if (!AddIfNotExists(_Move(kvPair)))
- throw KeyExistsException("The key already exists in Dictionary.");
+ numProbes++;
+ hashPos = (hashPos + GetProbeOffset(numProbes)) & bucketSizeMinusOne;
}
- TValue & Set(KeyValuePair<TKey, TValue> && kvPair)
- {
- Rehash();
- auto pos = FindPosition(kvPair.Key);
- if (pos.ObjectPosition != -1)
- 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:
- class Iterator
+ 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<TKey, TValue> && kvPair, int pos)
+ {
+ hashMap[pos] = _Move(kvPair);
+ SetEmpty(pos, false);
+ SetDeleted(pos, false);
+ return hashMap[pos].Value;
+ }
+ void Rehash()
+ {
+ if (bucketSizeMinusOne == -1 || _count / (float)bucketSizeMinusOne >= MaxLoadFactor)
{
- private:
- const Dictionary<TKey, TValue> * dict;
- int pos;
- public:
- KeyValuePair<TKey, TValue> & operator *() const
- {
- return dict->hashMap[pos];
- }
- KeyValuePair<TKey, TValue> * operator ->() const
+ int newSize = (bucketSizeMinusOne + 1) * 2;
+ int newShiftBits = shiftBits - 1;
+ if (newSize == 0)
{
- return dict->hashMap + pos;
+ newSize = 16;
+ newShiftBits = 28;
}
- Iterator & operator ++()
+ Dictionary<TKey, TValue> newDict;
+ newDict.shiftBits = newShiftBits;
+ newDict.bucketSizeMinusOne = newSize - 1;
+ newDict.hashMap = new KeyValuePair<TKey, TValue>[newSize];
+ newDict.marks.SetMax(newSize * 2);
+ if (hashMap)
{
- if (pos > dict->bucketSizeMinusOne)
- return *this;
- pos++;
- while (pos <= dict->bucketSizeMinusOne && (dict->IsDeleted(pos) || dict->IsEmpty(pos)))
+ for (auto & kvPair : *this)
{
- pos++;
+ newDict.Add(_Move(kvPair));
}
- return *this;
- }
- Iterator operator ++(int)
- {
- Iterator rs = *this;
- operator++();
- return rs;
- }
- bool operator != (const Iterator & _that) const
- {
- return pos != _that.pos || dict != _that.dict;
- }
- bool operator == (const Iterator & _that) const
- {
- return pos == _that.pos && dict == _that.dict;
- }
- Iterator(const Dictionary<TKey, TValue> * _dict, int _pos)
- {
- this->dict = _dict;
- this->pos = _pos;
- }
- Iterator()
- {
- this->dict = 0;
- this->pos = 0;
}
- };
+ *this = _Move(newDict);
+ }
+ }
- Iterator begin() const
+ bool AddIfNotExists(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ Rehash();
+ auto pos = FindPosition(kvPair.Key);
+ if (pos.ObjectPosition != -1)
+ return false;
+ else if (pos.InsertionPosition != -1)
{
- int pos = 0;
- while (pos < bucketSizeMinusOne + 1)
- {
- if (IsEmpty(pos) || IsDeleted(pos))
- pos++;
- else
- break;
- }
- return Iterator(this, pos);
+ _count++;
+ _Insert(_Move(kvPair), pos.InsertionPosition);
+ return true;
}
- Iterator end() const
+ else
+ throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
+ }
+ void Add(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ if (!AddIfNotExists(_Move(kvPair)))
+ throw KeyExistsException("The key already exists in Dictionary.");
+ }
+ TValue & Set(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ Rehash();
+ auto pos = FindPosition(kvPair.Key);
+ if (pos.ObjectPosition != -1)
+ return _Insert(_Move(kvPair), pos.ObjectPosition);
+ else if (pos.InsertionPosition != -1)
{
- return Iterator(this, bucketSizeMinusOne + 1);
+ _count++;
+ return _Insert(_Move(kvPair), pos.InsertionPosition);
}
+ else
+ throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
+ }
+ public:
+ class Iterator
+ {
+ private:
+ const Dictionary<TKey, TValue> * dict;
+ int pos;
public:
- void Add(const TKey & key, const TValue & value)
- {
- Add(KeyValuePair<TKey, TValue>(key, value));
- }
- void Add(TKey && key, TValue && value)
+ KeyValuePair<TKey, TValue> & operator *() const
{
- Add(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
+ return dict->hashMap[pos];
}
- bool AddIfNotExists(const TKey & key, const TValue & value)
+ KeyValuePair<TKey, TValue> * operator ->() const
{
- return AddIfNotExists(KeyValuePair<TKey, TValue>(key, value));
+ return dict->hashMap + pos;
}
- bool AddIfNotExists(TKey && key, TValue && value)
+ Iterator & operator ++()
{
- return AddIfNotExists(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
- }
- void Remove(const TKey & key)
- {
- if (_count == 0)
- return;
- auto pos = FindPosition(key);
- if (pos.ObjectPosition != -1)
+ if (pos > dict->bucketSizeMinusOne)
+ return *this;
+ pos++;
+ while (pos <= dict->bucketSizeMinusOne && (dict->IsDeleted(pos) || dict->IsEmpty(pos)))
{
- SetDeleted(pos.ObjectPosition, true);
- _count--;
+ pos++;
}
+ return *this;
}
- void Clear()
+ Iterator operator ++(int)
{
- _count = 0;
-
- marks.Clear();
+ Iterator rs = *this;
+ operator++();
+ return rs;
}
-
- template<typename T>
- bool ContainsKey(const T & key) const
+ bool operator != (const Iterator & _that) const
{
- if (bucketSizeMinusOne == -1)
- return false;
- auto pos = FindPosition(key);
- return pos.ObjectPosition != -1;
+ return pos != _that.pos || dict != _that.dict;
}
- template<typename T>
- bool TryGetValue(const T & key, TValue & value) const
+ bool operator == (const Iterator & _that) const
{
- if (bucketSizeMinusOne == -1)
- return false;
- auto pos = FindPosition(key);
- if (pos.ObjectPosition != -1)
- {
- value = hashMap[pos.ObjectPosition].Value;
- return true;
- }
- return false;
+ return pos == _that.pos && dict == _that.dict;
}
- template<typename T>
- TValue * TryGetValue(const T & key) const
+ Iterator(const Dictionary<TKey, TValue> * _dict, int _pos)
{
- if (bucketSizeMinusOne == -1)
- return nullptr;
- auto pos = FindPosition(key);
- if (pos.ObjectPosition != -1)
- {
- return &hashMap[pos.ObjectPosition].Value;
- }
- return nullptr;
+ this->dict = _dict;
+ this->pos = _pos;
}
- class ItemProxy
+ Iterator()
{
- private:
- const Dictionary<TKey, TValue> * dict;
- TKey key;
- public:
- ItemProxy(const TKey & _key, const Dictionary<TKey, TValue> * _dict)
- {
- this->dict = _dict;
- this->key = _key;
- }
- ItemProxy(TKey && _key, const Dictionary<TKey, TValue> * _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;
- }
- 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) const
- {
- return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), val));
- }
- TValue & operator = (TValue && val) const
- {
- return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), _Move(val)));
- }
- };
- ItemProxy operator [](const TKey & key) const
+ this->dict = 0;
+ this->pos = 0;
+ }
+ };
+
+ Iterator begin() const
+ {
+ int pos = 0;
+ while (pos < bucketSizeMinusOne + 1)
{
- return ItemProxy(key, this);
+ if (IsEmpty(pos) || IsDeleted(pos))
+ pos++;
+ else
+ break;
}
- ItemProxy operator [](TKey && key) const
+ return Iterator(this, pos);
+ }
+ Iterator end() const
+ {
+ return Iterator(this, bucketSizeMinusOne + 1);
+ }
+ public:
+ void Add(const TKey & key, const TValue & value)
+ {
+ Add(KeyValuePair<TKey, TValue>(key, value));
+ }
+ void Add(TKey && key, TValue && value)
+ {
+ Add(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
+ }
+ bool AddIfNotExists(const TKey & key, const TValue & value)
+ {
+ return AddIfNotExists(KeyValuePair<TKey, TValue>(key, value));
+ }
+ bool AddIfNotExists(TKey && key, TValue && value)
+ {
+ return AddIfNotExists(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
+ }
+ void Remove(const TKey & key)
+ {
+ if (_count == 0)
+ return;
+ auto pos = FindPosition(key);
+ if (pos.ObjectPosition != -1)
{
- return ItemProxy(_Move(key), this);
+ SetDeleted(pos.ObjectPosition, true);
+ _count--;
}
- int Count() const
+ }
+ void Clear()
+ {
+ _count = 0;
+
+ marks.Clear();
+ }
+
+ template<typename T>
+ bool ContainsKey(const T & key) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return false;
+ auto pos = FindPosition(key);
+ return pos.ObjectPosition != -1;
+ }
+ template<typename T>
+ bool TryGetValue(const T & key, TValue & value) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return false;
+ auto pos = FindPosition(key);
+ if (pos.ObjectPosition != -1)
{
- return _count;
+ value = hashMap[pos.ObjectPosition].Value;
+ return true;
}
- private:
- template<typename... Args>
- void Init(const KeyValuePair<TKey, TValue> & kvPair, Args... args)
+ return false;
+ }
+ template<typename T>
+ TValue * TryGetValue(const T & key) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return nullptr;
+ auto pos = FindPosition(key);
+ if (pos.ObjectPosition != -1)
{
- Add(kvPair);
- Init(args...);
+ return &hashMap[pos.ObjectPosition].Value;
}
+ return nullptr;
+ }
+ class ItemProxy
+ {
+ private:
+ const Dictionary<TKey, TValue> * dict;
+ TKey key;
public:
- Dictionary()
+ ItemProxy(const TKey & _key, const Dictionary<TKey, TValue> * _dict)
{
- bucketSizeMinusOne = -1;
- shiftBits = 32;
- _count = 0;
- hashMap = 0;
+ this->dict = _dict;
+ this->key = _key;
}
- template<typename Arg, typename... Args>
- Dictionary(Arg arg, Args... args)
+ ItemProxy(TKey && _key, const Dictionary<TKey, TValue> * _dict)
{
- Init(arg, args...);
+ this->dict = _dict;
+ this->key = _Move(_key);
}
- Dictionary(const Dictionary<TKey, TValue> & other)
- : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ TValue & GetValue() const
{
- *this = other;
+ auto pos = dict->FindPosition(key);
+ if (pos.ObjectPosition != -1)
+ {
+ return dict->hashMap[pos.ObjectPosition].Value;
+ }
+ else
+ throw KeyNotFoundException("The key does not exists in dictionary.");
}
- Dictionary(Dictionary<TKey, TValue> && other)
- : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ inline TValue & operator()() const
{
- *this = (_Move(other));
+ return GetValue();
}
- Dictionary<TKey, TValue> & operator = (const Dictionary<TKey, TValue> & other)
+ operator TValue&() const
{
- if (this == &other)
- return *this;
- Free();
- bucketSizeMinusOne = other.bucketSizeMinusOne;
- _count = other._count;
- shiftBits = other.shiftBits;
- hashMap = new KeyValuePair<TKey, TValue>[other.bucketSizeMinusOne + 1];
- marks = other.marks;
- for (int i = 0; i <= bucketSizeMinusOne; i++)
- hashMap[i] = other.hashMap[i];
- return *this;
+ return GetValue();
}
- Dictionary<TKey, TValue> & operator = (Dictionary<TKey, TValue> && other)
+ TValue & operator = (const TValue & val) const
{
- 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;
- return *this;
+ return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), val));
}
- ~Dictionary()
+ TValue & operator = (TValue && val) const
{
- Free();
+ return ((Dictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_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;
+ }
+ private:
+ template<typename... Args>
+ void Init(const KeyValuePair<TKey, TValue> & kvPair, Args... args)
+ {
+ Add(kvPair);
+ Init(args...);
+ }
+ public:
+ Dictionary()
+ {
+ bucketSizeMinusOne = -1;
+ shiftBits = 32;
+ _count = 0;
+ hashMap = 0;
+ }
+ template<typename Arg, typename... Args>
+ Dictionary(Arg arg, Args... args)
+ {
+ Init(arg, args...);
+ }
+ Dictionary(const Dictionary<TKey, TValue> & other)
+ : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ {
+ *this = other;
+ }
+ Dictionary(Dictionary<TKey, TValue> && other)
+ : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ {
+ *this = (_Move(other));
+ }
+ Dictionary<TKey, TValue> & operator = (const Dictionary<TKey, TValue> & other)
+ {
+ if (this == &other)
+ return *this;
+ Free();
+ bucketSizeMinusOne = other.bucketSizeMinusOne;
+ _count = other._count;
+ shiftBits = other.shiftBits;
+ hashMap = new KeyValuePair<TKey, TValue>[other.bucketSizeMinusOne + 1];
+ marks = other.marks;
+ for (int i = 0; i <= bucketSizeMinusOne; i++)
+ hashMap[i] = other.hashMap[i];
+ return *this;
+ }
+ Dictionary<TKey, TValue> & operator = (Dictionary<TKey, TValue> && 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;
+ return *this;
+ }
+ ~Dictionary()
+ {
+ Free();
+ }
+ };
- template<typename TKey, typename TValue>
- class EnumerableDictionary
+ template<typename TKey, typename TValue>
+ class EnumerableDictionary
+ {
+ friend class Iterator;
+ friend class ItemProxy;
+ private:
+ inline int GetProbeOffset(int /*probeIdx*/) const
{
- 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;
+ // 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<KeyValuePair<TKey, TValue>> kvPairs;
- LinkedNode<KeyValuePair<TKey, TValue>>** 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
+ // debug op
+ struct Op
+ {
+ TKey key;
+ int opType;
+ Op()
+ {}
+ Op(const TKey & key, int t)
{
- return !marks.Contains((pos << 1));
+ this->key = key;
+ opType = t;
}
- inline void SetDeleted(int pos, bool val)
+ };
+ LinkedList<KeyValuePair<TKey, TValue>> kvPairs;
+ LinkedNode<KeyValuePair<TKey, TValue>>** 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()
{
- if (val)
- marks.Add((pos << 1) + 1);
- else
- marks.Remove((pos << 1) + 1);
+ ObjectPosition = -1;
+ InsertionPosition = -1;
}
- inline void SetEmpty(int pos, bool val)
+ FindPositionResult(int objPos, int insertPos)
{
- if (val)
- marks.Remove((pos << 1));
- else
- marks.Add((pos << 1));
+ ObjectPosition = objPos;
+ InsertionPosition = insertPos;
}
- struct FindPositionResult
- {
- int ObjectPosition;
- int InsertionPosition;
- FindPositionResult()
- {
- ObjectPosition = -1;
- InsertionPosition = -1;
- }
- FindPositionResult(int objPos, int insertPos)
- {
- ObjectPosition = objPos;
- InsertionPosition = insertPos;
- }
- };
- template<typename T>
- inline int GetHashPos(T & key) const
- {
- return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits;
- }
- template<typename T>
- FindPositionResult FindPosition(const T & key) const
+ };
+ template<typename T>
+ inline int GetHashPos(T & key) const
+ {
+ return ((unsigned int)(GetHashCode(key) * 2654435761)) >> shiftBits;
+ }
+ template<typename T>
+ FindPositionResult FindPosition(const T & key) const
+ {
+ int hashPos = GetHashPos((T&)key);
+ int insertPos = -1;
+ int numProbes = 0;
+ while (numProbes <= bucketSizeMinusOne)
{
- int hashPos = GetHashPos((T&)key);
- int insertPos = -1;
- int numProbes = 0;
- while (numProbes <= bucketSizeMinusOne)
+ if (IsEmpty(hashPos))
{
- 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, hashPos);
+ else
+ return FindPositionResult(-1, insertPos);
}
- 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<TKey, TValue> && 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)
+ else if (IsDeleted(hashPos))
{
- int newSize = (bucketSizeMinusOne + 1) * 2;
- int newShiftBits = shiftBits - 1;
- if (newSize == 0)
- {
- newSize = 16;
- newShiftBits = 28;
- }
- EnumerableDictionary<TKey, TValue> newDict;
- newDict.shiftBits = newShiftBits;
- newDict.bucketSizeMinusOne = newSize - 1;
- newDict.hashMap = new LinkedNode<KeyValuePair<TKey, TValue>>*[newSize];
- newDict.marks.SetMax(newSize * 2);
- if (hashMap)
- {
- for (auto & kvPair : *this)
- {
- newDict.Add(_Move(kvPair));
- }
- }
- *this = _Move(newDict);
+ if (insertPos == -1)
+ insertPos = hashPos;
}
- }
-
- bool AddIfNotExists(KeyValuePair<TKey, TValue> && kvPair)
- {
- Rehash();
- auto pos = FindPosition(kvPair.Key);
- if (pos.ObjectPosition != -1)
- return false;
- else if (pos.InsertionPosition != -1)
+ else if (hashMap[hashPos]->Value.Key == key)
{
- _count++;
- _Insert(_Move(kvPair), pos.InsertionPosition);
- return true;
+ return FindPositionResult(hashPos, -1);
}
- else
- throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
- }
- void Add(KeyValuePair<TKey, TValue> && kvPair)
- {
- if (!AddIfNotExists(_Move(kvPair)))
- throw KeyExistsException("The key already exists in Dictionary.");
+ numProbes++;
+ hashPos = (hashPos + GetProbeOffset(numProbes)) & bucketSizeMinusOne;
}
- TValue & Set(KeyValuePair<TKey, TValue> && kvPair)
+ 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<TKey, TValue> && 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)
{
- Rehash();
- auto pos = FindPosition(kvPair.Key);
- if (pos.ObjectPosition != -1)
+ int newSize = (bucketSizeMinusOne + 1) * 2;
+ int newShiftBits = shiftBits - 1;
+ if (newSize == 0)
{
- hashMap[pos.ObjectPosition]->Delete();
- return _Insert(_Move(kvPair), pos.ObjectPosition);
+ newSize = 16;
+ newShiftBits = 28;
}
- 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<KeyValuePair<TKey, TValue>>::Iterator Iterator;
-
- typename LinkedList<KeyValuePair<TKey, TValue>>::Iterator begin() const
- {
- return kvPairs.begin();
- }
- typename LinkedList<KeyValuePair<TKey, TValue>>::Iterator end() const
- {
- return kvPairs.end();
- }
- public:
- void Add(const TKey & key, const TValue & value)
- {
- Add(KeyValuePair<TKey, TValue>(key, value));
- }
- void Add(TKey && key, TValue && value)
- {
- Add(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
- }
- bool AddIfNotExists(const TKey & key, const TValue & value)
- {
- return AddIfNotExists(KeyValuePair<TKey, TValue>(key, value));
- }
- bool AddIfNotExists(TKey && key, TValue && value)
- {
- return AddIfNotExists(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
- }
- void Remove(const TKey & key)
- {
- if (_count > 0)
+ EnumerableDictionary<TKey, TValue> newDict;
+ newDict.shiftBits = newShiftBits;
+ newDict.bucketSizeMinusOne = newSize - 1;
+ newDict.hashMap = new LinkedNode<KeyValuePair<TKey, TValue>>*[newSize];
+ newDict.marks.SetMax(newSize * 2);
+ if (hashMap)
{
- auto pos = FindPosition(key);
- if (pos.ObjectPosition != -1)
+ for (auto & kvPair : *this)
{
- kvPairs.Delete(hashMap[pos.ObjectPosition]);
- hashMap[pos.ObjectPosition] = 0;
- SetDeleted(pos.ObjectPosition, true);
- _count--;
+ newDict.Add(_Move(kvPair));
}
}
+ *this = _Move(newDict);
}
- void Clear()
+ }
+
+ bool AddIfNotExists(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ Rehash();
+ auto pos = FindPosition(kvPair.Key);
+ if (pos.ObjectPosition != -1)
+ return false;
+ else if (pos.InsertionPosition != -1)
{
- _count = 0;
- kvPairs.Clear();
- marks.Clear();
+ _count++;
+ _Insert(_Move(kvPair), pos.InsertionPosition);
+ return true;
}
- template<typename T>
- bool ContainsKey(const T & key) const
+ else
+ throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
+ }
+ void Add(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ if (!AddIfNotExists(_Move(kvPair)))
+ throw KeyExistsException("The key already exists in Dictionary.");
+ }
+ TValue & Set(KeyValuePair<TKey, TValue> && kvPair)
+ {
+ Rehash();
+ auto pos = FindPosition(kvPair.Key);
+ if (pos.ObjectPosition != -1)
{
- if (bucketSizeMinusOne == -1)
- return false;
- auto pos = FindPosition(key);
- return pos.ObjectPosition != -1;
+ hashMap[pos.ObjectPosition]->Delete();
+ return _Insert(_Move(kvPair), pos.ObjectPosition);
}
- template<typename T>
- TValue * TryGetValue(const T & key) const
+ else if (pos.InsertionPosition != -1)
{
- if (bucketSizeMinusOne == -1)
- return nullptr;
- auto pos = FindPosition(key);
- if (pos.ObjectPosition != -1)
- {
- return &(hashMap[pos.ObjectPosition]->Value.Value);
- }
- return nullptr;
+ _count++;
+ return _Insert(_Move(kvPair), pos.InsertionPosition);
}
- template<typename T>
- bool TryGetValue(const T & key, TValue & value) const
+ else
+ throw InvalidOperationException("Inconsistent find result returned. This is a bug in Dictionary implementation.");
+ }
+ public:
+ typedef typename LinkedList<KeyValuePair<TKey, TValue>>::Iterator Iterator;
+
+ typename LinkedList<KeyValuePair<TKey, TValue>>::Iterator begin() const
+ {
+ return kvPairs.begin();
+ }
+ typename LinkedList<KeyValuePair<TKey, TValue>>::Iterator end() const
+ {
+ return kvPairs.end();
+ }
+ public:
+ void Add(const TKey & key, const TValue & value)
+ {
+ Add(KeyValuePair<TKey, TValue>(key, value));
+ }
+ void Add(TKey && key, TValue && value)
+ {
+ Add(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
+ }
+ bool AddIfNotExists(const TKey & key, const TValue & value)
+ {
+ return AddIfNotExists(KeyValuePair<TKey, TValue>(key, value));
+ }
+ bool AddIfNotExists(TKey && key, TValue && value)
+ {
+ return AddIfNotExists(KeyValuePair<TKey, TValue>(_Move(key), _Move(value)));
+ }
+ void Remove(const TKey & key)
+ {
+ if (_count > 0)
{
- 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<TKey, TValue> * dict;
- TKey key;
- public:
- ItemProxy(const TKey & _key, const EnumerableDictionary<TKey, TValue> * _dict)
- {
- this->dict = _dict;
- this->key = _key;
- }
- ItemProxy(TKey && _key, const EnumerableDictionary<TKey, TValue> * _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<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), val));
- }
- TValue & operator = (TValue && val)
- {
- return ((EnumerableDictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), _Move(val)));
+ kvPairs.Delete(hashMap[pos.ObjectPosition]);
+ hashMap[pos.ObjectPosition] = 0;
+ SetDeleted(pos.ObjectPosition, true);
+ _count--;
}
- };
- 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<TKey, TValue> & First() const
+ }
+ void Clear()
+ {
+ _count = 0;
+ kvPairs.Clear();
+ marks.Clear();
+ }
+ template<typename T>
+ bool ContainsKey(const T & key) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return false;
+ auto pos = FindPosition(key);
+ return pos.ObjectPosition != -1;
+ }
+ template<typename T>
+ TValue * TryGetValue(const T & key) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return nullptr;
+ auto pos = FindPosition(key);
+ if (pos.ObjectPosition != -1)
{
- return kvPairs.First();
+ return &(hashMap[pos.ObjectPosition]->Value.Value);
}
- KeyValuePair<TKey, TValue> & Last() const
+ return nullptr;
+ }
+ template<typename T>
+ bool TryGetValue(const T & key, TValue & value) const
+ {
+ if (bucketSizeMinusOne == -1)
+ return false;
+ auto pos = FindPosition(key);
+ if (pos.ObjectPosition != -1)
{
- return kvPairs.Last();
+ value = hashMap[pos.ObjectPosition]->Value.Value;
+ return true;
}
+ return false;
+ }
+ class ItemProxy
+ {
private:
- template<typename... Args>
- void Init(const KeyValuePair<TKey, TValue> & kvPair, Args... args)
- {
- Add(kvPair);
- Init(args...);
- }
+ const EnumerableDictionary<TKey, TValue> * dict;
+ TKey key;
public:
- EnumerableDictionary()
+ ItemProxy(const TKey & _key, const EnumerableDictionary<TKey, TValue> * _dict)
{
- bucketSizeMinusOne = -1;
- shiftBits = 32;
- _count = 0;
- hashMap = 0;
+ this->dict = _dict;
+ this->key = _key;
}
- template<typename Arg, typename... Args>
- EnumerableDictionary(Arg arg, Args... args)
+ ItemProxy(TKey && _key, const EnumerableDictionary<TKey, TValue> * _dict)
{
- Init(arg, args...);
+ this->dict = _dict;
+ this->key = _Move(_key);
}
- EnumerableDictionary(const EnumerableDictionary<TKey, TValue> & other)
- : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ TValue & GetValue() const
{
- *this = other;
+ 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.");
+ }
}
- EnumerableDictionary(EnumerableDictionary<TKey, TValue> && other)
- : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ inline TValue & operator()() const
{
- *this = (_Move(other));
+ return GetValue();
}
- EnumerableDictionary<TKey, TValue> & operator = (const EnumerableDictionary<TKey, TValue> & other)
+ operator TValue&() const
{
- if (this == &other)
- return *this;
- Clear();
- for (auto & item : other)
- Add(item.Key, item.Value);
- return *this;
+ return GetValue();
}
- EnumerableDictionary<TKey, TValue> & operator = (EnumerableDictionary<TKey, TValue> && other)
+ TValue & operator = (const TValue & val)
{
- 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;
+ return ((EnumerableDictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_Move(key), val));
}
- ~EnumerableDictionary()
+ TValue & operator = (TValue && val)
{
- Free();
+ return ((EnumerableDictionary<TKey, TValue>*)dict)->Set(KeyValuePair<TKey, TValue>(_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<TKey, TValue> & First() const
+ {
+ return kvPairs.First();
+ }
+ KeyValuePair<TKey, TValue> & Last() const
+ {
+ return kvPairs.Last();
+ }
+ private:
+ template<typename... Args>
+ void Init(const KeyValuePair<TKey, TValue> & kvPair, Args... args)
+ {
+ Add(kvPair);
+ Init(args...);
+ }
+ public:
+ EnumerableDictionary()
+ {
+ bucketSizeMinusOne = -1;
+ shiftBits = 32;
+ _count = 0;
+ hashMap = 0;
+ }
+ template<typename Arg, typename... Args>
+ EnumerableDictionary(Arg arg, Args... args)
+ {
+ Init(arg, args...);
+ }
+ EnumerableDictionary(const EnumerableDictionary<TKey, TValue> & other)
+ : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ {
+ *this = other;
+ }
+ EnumerableDictionary(EnumerableDictionary<TKey, TValue> && other)
+ : bucketSizeMinusOne(-1), _count(0), hashMap(0)
+ {
+ *this = (_Move(other));
+ }
+ EnumerableDictionary<TKey, TValue> & operator = (const EnumerableDictionary<TKey, TValue> & other)
+ {
+ if (this == &other)
+ return *this;
+ Clear();
+ for (auto & item : other)
+ Add(item.Key, item.Value);
+ return *this;
+ }
+ EnumerableDictionary<TKey, TValue> & operator = (EnumerableDictionary<TKey, TValue> && 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
- {};
+ class _DummyClass
+ {};
- template<typename T, typename DictionaryType>
- class HashSetBase
+ template<typename T, typename DictionaryType>
+ class HashSetBase
+ {
+ protected:
+ DictionaryType dict;
+ private:
+ template<typename... Args>
+ void Init(const T & v, Args... args)
+ {
+ Add(v);
+ Init(args...);
+ }
+ public:
+ HashSetBase()
+ {}
+ template<typename Arg, typename... Args>
+ HashSetBase(Arg arg, Args... args)
+ {
+ Init(arg, args...);
+ }
+ HashSetBase(const HashSetBase & set)
+ {
+ operator=(set);
+ }
+ HashSetBase(HashSetBase && set)
+ {
+ operator=(_Move(set));
+ }
+ HashSetBase & operator = (const HashSetBase & set)
+ {
+ dict = set.dict;
+ return *this;
+ }
+ HashSetBase & operator = (HashSetBase && set)
+ {
+ dict = _Move(set.dict);
+ return *this;
+ }
+ public:
+ class Iterator
{
- protected:
- DictionaryType dict;
private:
- template<typename... Args>
- void Init(const T & v, Args... args)
- {
- Add(v);
- Init(args...);
- }
+ typename DictionaryType::Iterator iter;
public:
- HashSetBase()
- {}
- template<typename Arg, typename... Args>
- HashSetBase(Arg arg, Args... args)
- {
- Init(arg, args...);
- }
- HashSetBase(const HashSetBase & set)
+ Iterator() = default;
+ T & operator *() const
{
- operator=(set);
+ return (*iter).Key;
}
- HashSetBase(HashSetBase && set)
+ T * operator ->() const
{
- operator=(_Move(set));
+ return &(*iter).Key;
}
- HashSetBase & operator = (const HashSetBase & set)
+ Iterator & operator ++()
{
- dict = set.dict;
+ ++iter;
return *this;
}
- HashSetBase & operator = (HashSetBase && set)
- {
- dict = _Move(set.dict);
- return *this;
- }
- public:
- class Iterator
- {
- private:
- typename DictionaryType::Iterator iter;
- public:
- Iterator() = default;
- T & operator *() const
- {
- return (*iter).Key;
- }
- T * operator ->() const
- {
- return &(*iter).Key;
- }
- Iterator & operator ++()
- {
- ++iter;
- return *this;
- }
- Iterator operator ++(int)
- {
- Iterator rs = *this;
- operator++();
- return rs;
- }
- bool operator != (const Iterator & _that) const
- {
- return iter != _that.iter;
- }
- bool operator == (const Iterator & _that) const
- {
- return iter == _that.iter;
- }
- Iterator(const typename DictionaryType::Iterator & _iter)
- {
- this->iter = _iter;
- }
- };
- Iterator begin() const
- {
- return Iterator(dict.begin());
- }
- Iterator end() const
- {
- return Iterator(dict.end());
- }
- public:
- int Count() const
- {
- return dict.Count();
- }
- void Clear()
- {
- dict.Clear();
- }
- bool Add(const T& obj)
+ Iterator operator ++(int)
{
- return dict.AddIfNotExists(obj, _DummyClass());
+ Iterator rs = *this;
+ operator++();
+ return rs;
}
- bool Add(T && obj)
+ bool operator != (const Iterator & _that) const
{
- return dict.AddIfNotExists(_Move(obj), _DummyClass());
+ return iter != _that.iter;
}
- void Remove(const T & obj)
+ bool operator == (const Iterator & _that) const
{
- dict.Remove(obj);
+ return iter == _that.iter;
}
- bool Contains(const T & obj) const
+ Iterator(const typename DictionaryType::Iterator & _iter)
{
- return dict.ContainsKey(obj);
+ this->iter = _iter;
}
};
- template <typename T>
- class HashSet : public HashSetBase<T, Dictionary<T, _DummyClass>>
- {};
+ Iterator begin() const
+ {
+ return Iterator(dict.begin());
+ }
+ Iterator end() const
+ {
+ return Iterator(dict.end());
+ }
+ public:
+ int Count() const
+ {
+ return dict.Count();
+ }
+ void Clear()
+ {
+ dict.Clear();
+ }
+ bool Add(const T& obj)
+ {
+ return dict.AddIfNotExists(obj, _DummyClass());
+ }
+ bool Add(T && obj)
+ {
+ return dict.AddIfNotExists(_Move(obj), _DummyClass());
+ }
+ void Remove(const T & obj)
+ {
+ dict.Remove(obj);
+ }
+ bool Contains(const T & obj) const
+ {
+ return dict.ContainsKey(obj);
+ }
+ };
+ template <typename T>
+ class HashSet : public HashSetBase<T, Dictionary<T, _DummyClass>>
+ {};
- template <typename T>
- class EnumerableHashSet : public HashSetBase<T, EnumerableDictionary<T, _DummyClass>>
+ template <typename T>
+ class EnumerableHashSet : public HashSetBase<T, EnumerableDictionary<T, _DummyClass>>
+ {
+ public:
+ T & First() const
{
- public:
- T & First() const
- {
- return this->dict.First().Key;
- }
- T & Last() const
- {
- return this->dict.Last().Key;
- }
- };
- }
+ 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 652cafca7..4671ae10b 100644
--- a/source/core/exception.h
+++ b/source/core/exception.h
@@ -4,112 +4,109 @@
#include "common.h"
#include "slang-string.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ class Exception : public Object
{
- class Exception : public Object
+ public:
+ String Message;
+ Exception()
+ {}
+ Exception(const String & message)
+ : Message(message)
{
- public:
- String Message;
- Exception()
- {}
- Exception(const String & message)
- : Message(message)
- {
- }
- };
+ }
+ };
- class IndexOutofRangeException : public Exception
+ class IndexOutofRangeException : public Exception
+ {
+ public:
+ IndexOutofRangeException()
+ {}
+ IndexOutofRangeException(const String & message)
+ : Exception(message)
{
- public:
- IndexOutofRangeException()
- {}
- IndexOutofRangeException(const String & message)
- : Exception(message)
- {
- }
+ }
- };
+ };
- class InvalidOperationException : public Exception
+ class InvalidOperationException : public Exception
+ {
+ public:
+ InvalidOperationException()
+ {}
+ InvalidOperationException(const String & message)
+ : Exception(message)
{
- public:
- InvalidOperationException()
- {}
- InvalidOperationException(const String & message)
- : Exception(message)
- {
- }
+ }
- };
+ };
- class ArgumentException : public Exception
+ class ArgumentException : public Exception
+ {
+ public:
+ ArgumentException()
+ {}
+ ArgumentException(const String & message)
+ : Exception(message)
{
- public:
- ArgumentException()
- {}
- ArgumentException(const String & message)
- : Exception(message)
- {
- }
+ }
- };
+ };
- class KeyNotFoundException : public Exception
+ class KeyNotFoundException : public Exception
+ {
+ public:
+ KeyNotFoundException()
+ {}
+ KeyNotFoundException(const String & message)
+ : Exception(message)
{
- public:
- KeyNotFoundException()
- {}
- KeyNotFoundException(const String & message)
- : Exception(message)
- {
- }
- };
- class KeyExistsException : public Exception
+ }
+ };
+ class KeyExistsException : public Exception
+ {
+ public:
+ KeyExistsException()
+ {}
+ KeyExistsException(const String & message)
+ : Exception(message)
{
- public:
- KeyExistsException()
- {}
- KeyExistsException(const String & message)
- : Exception(message)
- {
- }
- };
+ }
+ };
- class NotSupportedException : public Exception
+ class NotSupportedException : public Exception
+ {
+ public:
+ NotSupportedException()
+ {}
+ NotSupportedException(const String & message)
+ : Exception(message)
{
- public:
- NotSupportedException()
- {}
- NotSupportedException(const String & message)
- : Exception(message)
- {
- }
- };
+ }
+ };
- class NotImplementedException : public Exception
+ class NotImplementedException : public Exception
+ {
+ public:
+ NotImplementedException()
+ {}
+ NotImplementedException(const String & message)
+ : Exception(message)
{
- public:
- NotImplementedException()
- {}
- NotImplementedException(const String & message)
- : Exception(message)
- {
- }
- };
+ }
+ };
- class InvalidProgramException : public Exception
+ class InvalidProgramException : public Exception
+ {
+ public:
+ InvalidProgramException()
+ {}
+ InvalidProgramException(const String & message)
+ : Exception(message)
{
- public:
- InvalidProgramException()
- {}
- InvalidProgramException(const String & message)
- : Exception(message)
- {
- }
- };
- }
+ }
+ };
}
#endif \ No newline at end of file
diff --git a/source/core/func.h b/source/core/func.h
deleted file mode 100644
index 8ec2ba8a5..000000000
--- a/source/core/func.h
+++ /dev/null
@@ -1,216 +0,0 @@
-#ifndef CORELIB_FUNC_H
-#define CORELIB_FUNC_H
-
-#if 0
-
-#include "SmartPointer.h"
-
-namespace CoreLib
-{
- namespace Basic
- {
- template<typename TResult, typename... Arguments>
- class FuncPtr
- {
- public:
- virtual TResult operator()(Arguments...) = 0;
- virtual bool operator == (const FuncPtr<TResult, Arguments...> *)
- {
- return false;
- }
- virtual ~FuncPtr() {}
- };
-
- template<typename TResult, typename... Arguments>
- class CdeclFuncPtr : public FuncPtr<TResult, Arguments...>
- {
- public:
- typedef TResult (*FuncType)(Arguments...);
- private:
- FuncType funcPtr;
- public:
- CdeclFuncPtr(FuncType func)
- :funcPtr(func)
- {
- }
-
- virtual TResult operator()(Arguments... params) override
- {
- return funcPtr(params...);
- }
-
- virtual bool operator == (const FuncPtr<TResult, Arguments...> * ptr) override
- {
- auto cptr = dynamic_cast<const CdeclFuncPtr<TResult, Arguments...>*>(ptr);
- if (cptr)
- return funcPtr == cptr->funcPtr;
- else
- return false;
- }
- };
-
- template<typename Class, typename TResult, typename... Arguments>
- class MemberFuncPtr : public FuncPtr<TResult, Arguments...>
- {
- public:
- typedef TResult (Class::*FuncType)(Arguments...);
- private:
- FuncType funcPtr;
- Class * object;
- public:
- MemberFuncPtr(Class * obj, FuncType func)
- : funcPtr(func), object(obj)
- {
- }
-
- virtual TResult operator()(Arguments... params) override
- {
- return (object->*funcPtr)(params...);
- }
-
- virtual bool operator == (const FuncPtr<TResult, Arguments...> * ptr) override
- {
- auto cptr = dynamic_cast<const MemberFuncPtr<Class, TResult, Arguments...>*>(ptr);
- if (cptr)
- return funcPtr == cptr->funcPtr && object == cptr->object;
- else
- return false;
- }
- };
-
- template<typename F, typename TResult, typename... Arguments>
- class LambdaFuncPtr : public FuncPtr<TResult, Arguments...>
- {
- private:
- F func;
- public:
- LambdaFuncPtr(const F & _func)
- : func(_func)
- {}
- virtual TResult operator()(Arguments... params) override
- {
- return func(params...);
- }
- virtual bool operator == (const FuncPtr<TResult, Arguments...> * /*ptr*/) override
- {
- return false;
- }
- };
-
- template<typename TResult, typename... Arguments>
- class Func
- {
- private:
- RefPtr<FuncPtr<TResult, Arguments...>> funcPtr;
- public:
- Func(){}
- Func(typename CdeclFuncPtr<TResult, Arguments...>::FuncType func)
- {
- funcPtr = new CdeclFuncPtr<TResult, Arguments...>(func);
- }
- template<typename Class>
- Func(Class * object, typename MemberFuncPtr<Class, TResult, Arguments...>::FuncType func)
- {
- funcPtr = new MemberFuncPtr<Class, TResult, Arguments...>(object, func);
- }
- template<typename TFuncObj>
- Func(const TFuncObj & func)
- {
- funcPtr = new LambdaFuncPtr<TFuncObj, TResult, Arguments...>(func);
- }
- Func & operator = (typename CdeclFuncPtr<TResult, Arguments...>::FuncType func)
- {
- funcPtr = new CdeclFuncPtr<TResult, Arguments...>(func);
- return *this;
- }
- template<typename Class>
- Func & operator = (const MemberFuncPtr<Class, TResult, Arguments...> & func)
- {
- funcPtr = new MemberFuncPtr<Class, TResult, Arguments...>(func);
- return *this;
- }
- template<typename TFuncObj>
- Func & operator = (const TFuncObj & func)
- {
- funcPtr = new LambdaFuncPtr<TFuncObj, TResult, Arguments...>(func);
- return *this;
- }
- bool operator == (const Func & f)
- {
- return *funcPtr == f.funcPtr.Ptr();
- }
- bool operator != (const Func & f)
- {
- return !(*this == f);
- }
- TResult operator()(Arguments... params)
- {
- return (*funcPtr)(params...);
- }
- };
-
- // template<typename... Arguments>
- // using Procedure = Func<void, Arguments...>;
-
- template<typename... Arguments>
- class Procedure : public Func<void, Arguments...>
- {
- private:
- RefPtr<FuncPtr<void, Arguments...>> funcPtr;
- public:
- Procedure(){}
- Procedure(const Procedure & proc)
- {
- funcPtr = proc.funcPtr;
- }
- Procedure(typename CdeclFuncPtr<void, Arguments...>::FuncType func)
- {
- funcPtr = new CdeclFuncPtr<void, Arguments...>(func);
- }
- template<typename Class>
- Procedure(Class * object, typename MemberFuncPtr<Class, void, Arguments...>::FuncType func)
- {
- funcPtr = new MemberFuncPtr<Class, void, Arguments...>(object, func);
- }
- template<typename TFuncObj>
- Procedure(const TFuncObj & func)
- {
- funcPtr = new LambdaFuncPtr<TFuncObj, void, Arguments...>(func);
- }
- Procedure & operator = (typename CdeclFuncPtr<void, Arguments...>::FuncType func)
- {
- funcPtr = new CdeclFuncPtr<void, Arguments...>(func);
- return *this;
- }
- template<typename Class>
- Procedure & operator = (const MemberFuncPtr<Class, void, Arguments...> & func)
- {
- funcPtr = new MemberFuncPtr<Class, void, Arguments...>(func);
- return *this;
- }
- template<typename TFuncObj>
- Procedure & operator = (const TFuncObj & func)
- {
- funcPtr = new LambdaFuncPtr<TFuncObj, void, Arguments...>(func);
- return *this;
- }
- Procedure & operator = (const Procedure & proc)
- {
- funcPtr = proc.funcPtr;
- }
- void Clear()
- {
- funcPtr = nullptr;
- }
- void operator()(Arguments... params)
- {
- if (funcPtr)
- (*funcPtr)(params...);
- }
- };
- }
-}
-
-#endif
-
-#endif \ No newline at end of file
diff --git a/source/core/hash.h b/source/core/hash.h
index 07327a415..07a14099b 100644
--- a/source/core/hash.h
+++ b/source/core/hash.h
@@ -4,102 +4,98 @@
#include "slang-math.h"
#include <string.h>
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ inline int GetHashCode(double key)
{
-
- inline int GetHashCode(double key)
- {
- return FloatAsInt((float)key);
- }
- inline int GetHashCode(float key)
- {
- return FloatAsInt(key);
- }
- inline int GetHashCode(const char * buffer)
+ return FloatAsInt((float)key);
+ }
+ inline int GetHashCode(float key)
+ {
+ return FloatAsInt(key);
+ }
+ inline int GetHashCode(const char * buffer)
+ {
+ if (!buffer)
+ return 0;
+ int hash = 0;
+ int c;
+ auto str = buffer;
+ c = *str++;
+ while (c)
{
- if (!buffer)
- return 0;
- int hash = 0;
- int c;
- auto str = buffer;
+ hash = c + (hash << 6) + (hash << 16) - hash;
c = *str++;
- while (c)
- {
- hash = c + (hash << 6) + (hash << 16) - hash;
- c = *str++;
- }
- return hash;
- }
- inline int GetHashCode(char * buffer)
- {
- return GetHashCode(const_cast<const char *>(buffer));
}
+ return hash;
+ }
+ inline int GetHashCode(char * buffer)
+ {
+ return GetHashCode(const_cast<const char *>(buffer));
+ }
- template<int IsInt>
- class Hash
- {
- public:
- };
- template<>
- class Hash<1>
- {
- public:
- template<typename TKey>
- static int GetHashCode(TKey & key)
- {
- return (int)key;
- }
- };
- template<>
- class Hash<0>
- {
- public:
- template<typename TKey>
- static int GetHashCode(TKey & key)
- {
- return key.GetHashCode();
- }
- };
- template<int IsPointer>
- class PointerHash
- {};
- template<>
- class PointerHash<1>
+ template<int IsInt>
+ class Hash
+ {
+ public:
+ };
+ template<>
+ class Hash<1>
+ {
+ public:
+ template<typename TKey>
+ static int GetHashCode(TKey & key)
{
- public:
- template<typename TKey>
- static int GetHashCode(TKey const& key)
- {
- return (int)((CoreLib::PtrInt)key) / 16; // sizeof(typename std::remove_pointer<TKey>::type);
- }
- };
- template<>
- class PointerHash<0>
+ return (int)key;
+ }
+ };
+ template<>
+ class Hash<0>
+ {
+ public:
+ template<typename TKey>
+ static int GetHashCode(TKey & key)
{
- public:
- template<typename TKey>
- static int GetHashCode(TKey & key)
- {
- return Hash<std::is_integral<TKey>::value || std::is_enum<TKey>::value>::GetHashCode(key);
- }
- };
-
+ return key.GetHashCode();
+ }
+ };
+ template<int IsPointer>
+ class PointerHash
+ {};
+ template<>
+ class PointerHash<1>
+ {
+ public:
template<typename TKey>
- int GetHashCode(const TKey & key)
+ static int GetHashCode(TKey const& key)
{
- return PointerHash<std::is_pointer<TKey>::value>::GetHashCode(key);
+ return (int)((PtrInt)key) / 16; // sizeof(typename std::remove_pointer<TKey>::type);
}
-
+ };
+ template<>
+ class PointerHash<0>
+ {
+ public:
template<typename TKey>
- int GetHashCode(TKey & key)
+ static int GetHashCode(TKey & key)
{
- return PointerHash<std::is_pointer<TKey>::value>::GetHashCode(key);
+ return Hash<std::is_integral<TKey>::value || std::is_enum<TKey>::value>::GetHashCode(key);
}
+ };
-
+ template<typename TKey>
+ int GetHashCode(const TKey & key)
+ {
+ return PointerHash<std::is_pointer<TKey>::value>::GetHashCode(key);
+ }
+
+ template<typename TKey>
+ int GetHashCode(TKey & key)
+ {
+ return PointerHash<std::is_pointer<TKey>::value>::GetHashCode(key);
}
+
+
}
#endif \ No newline at end of file
diff --git a/source/core/int-set.h b/source/core/int-set.h
index cb8f788f2..a56a58d64 100644
--- a/source/core/int-set.h
+++ b/source/core/int-set.h
@@ -8,159 +8,156 @@
#include <memory.h>
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ class IntSet
{
- class IntSet
- {
- private:
- List<int> buffer;
- public:
- IntSet()
- {}
- IntSet(const IntSet & other)
- {
- buffer = other.buffer;
- }
- IntSet(IntSet && other)
- {
- *this = (_Move(other));
- }
- IntSet & operator = (IntSet && other)
- {
- buffer = _Move(other.buffer);
- return *this;
- }
- IntSet & operator = (const IntSet & other)
- {
- buffer = other.buffer;
- return *this;
- }
- int GetHashCode()
- {
- int rs = 0;
- for (auto val : buffer)
- rs ^= val;
- return rs;
- }
- IntSet(int maxVal)
- {
- SetMax(maxVal);
- }
- int Size() const
- {
- return buffer.Count()*32;
- }
- void SetMax(int val)
- {
- Resize(val);
- Clear();
- }
- void SetAll()
- {
- for (int i = 0; i<buffer.Count(); i++)
- buffer[i] = 0xFFFFFFFF;
- }
- void Resize(int size)
- {
- int oldBufferSize = buffer.Count();
- buffer.SetSize((size+31)>>5);
- if (buffer.Count() > oldBufferSize)
- memset(buffer.Buffer()+oldBufferSize, 0, (buffer.Count()-oldBufferSize) * sizeof(int));
- }
- void Clear()
- {
- for (int i = 0; i<buffer.Count(); i++)
- buffer[i] = 0;
- }
- void Add(int val)
- {
- int id = val>>5;
- if (id < buffer.Count())
- buffer[id] |= (1<<(val&31));
- else
- {
- int oldSize = buffer.Count();
- buffer.SetSize(id+1);
- memset(buffer.Buffer() + oldSize, 0, (buffer.Count()-oldSize)*sizeof(int));
- buffer[id] |= (1<<(val&31));
- }
- }
- void Remove(int val)
- {
- if ((val>>5) < buffer.Count())
- buffer[(val>>5)] &= ~(1<<(val&31));
- }
- bool Contains(int val) const
- {
- if ((val>>5) >= buffer.Count())
- return false;
- return (buffer[(val>>5)] & (1<<(val&31))) != 0;
- }
- void UnionWith(const IntSet & set)
+ private:
+ List<int> buffer;
+ public:
+ IntSet()
+ {}
+ IntSet(const IntSet & other)
+ {
+ buffer = other.buffer;
+ }
+ IntSet(IntSet && other)
+ {
+ *this = (_Move(other));
+ }
+ IntSet & operator = (IntSet && other)
+ {
+ buffer = _Move(other.buffer);
+ return *this;
+ }
+ IntSet & operator = (const IntSet & other)
+ {
+ buffer = other.buffer;
+ return *this;
+ }
+ int GetHashCode()
+ {
+ int rs = 0;
+ for (auto val : buffer)
+ rs ^= val;
+ return rs;
+ }
+ IntSet(int maxVal)
+ {
+ SetMax(maxVal);
+ }
+ int Size() const
+ {
+ return buffer.Count()*32;
+ }
+ void SetMax(int val)
+ {
+ Resize(val);
+ Clear();
+ }
+ void SetAll()
+ {
+ for (int i = 0; i<buffer.Count(); i++)
+ buffer[i] = 0xFFFFFFFF;
+ }
+ void Resize(int size)
+ {
+ int oldBufferSize = buffer.Count();
+ buffer.SetSize((size+31)>>5);
+ if (buffer.Count() > oldBufferSize)
+ memset(buffer.Buffer()+oldBufferSize, 0, (buffer.Count()-oldBufferSize) * sizeof(int));
+ }
+ void Clear()
+ {
+ for (int i = 0; i<buffer.Count(); i++)
+ buffer[i] = 0;
+ }
+ void Add(int val)
+ {
+ int id = val>>5;
+ if (id < buffer.Count())
+ buffer[id] |= (1<<(val&31));
+ else
+ {
+ int oldSize = buffer.Count();
+ buffer.SetSize(id+1);
+ memset(buffer.Buffer() + oldSize, 0, (buffer.Count()-oldSize)*sizeof(int));
+ buffer[id] |= (1<<(val&31));
+ }
+ }
+ void Remove(int val)
+ {
+ if ((val>>5) < buffer.Count())
+ buffer[(val>>5)] &= ~(1<<(val&31));
+ }
+ bool Contains(int val) const
+ {
+ if ((val>>5) >= buffer.Count())
+ return false;
+ return (buffer[(val>>5)] & (1<<(val&31))) != 0;
+ }
+ void UnionWith(const IntSet & set)
+ {
+ for (int i = 0; i<Math::Min(set.buffer.Count(), buffer.Count()); i++)
{
- for (int i = 0; i<Math::Min(set.buffer.Count(), buffer.Count()); i++)
- {
- buffer[i] |= set.buffer[i];
- }
- if (set.buffer.Count() > buffer.Count())
- buffer.AddRange(set.buffer.Buffer()+buffer.Count(), set.buffer.Count()-buffer.Count());
+ buffer[i] |= set.buffer[i];
}
- bool operator == (const IntSet & set)
- {
- if (buffer.Count() != set.buffer.Count())
+ if (set.buffer.Count() > buffer.Count())
+ buffer.AddRange(set.buffer.Buffer()+buffer.Count(), set.buffer.Count()-buffer.Count());
+ }
+ bool operator == (const IntSet & set)
+ {
+ if (buffer.Count() != set.buffer.Count())
+ return false;
+ for (int i = 0; i<buffer.Count(); i++)
+ if (buffer[i] != set.buffer[i])
return false;
- for (int i = 0; i<buffer.Count(); i++)
- if (buffer[i] != set.buffer[i])
- return false;
- return true;
- }
- bool operator != (const IntSet & set)
- {
- return !(*this == set);
- }
- void IntersectWith(const IntSet & set)
- {
- if (set.buffer.Count() < buffer.Count())
- memset(buffer.Buffer() + set.buffer.Count(), 0, (buffer.Count()-set.buffer.Count())*sizeof(int));
- for (int i = 0; i<Math::Min(set.buffer.Count(), buffer.Count()); i++)
- {
- buffer[i] &= set.buffer[i];
- }
- }
- static void Union(IntSet & rs, const IntSet & set1, const IntSet & set2)
- {
- rs.buffer.SetSize(Math::Max(set1.buffer.Count(), set2.buffer.Count()));
- rs.Clear();
- for (int i = 0; i<set1.buffer.Count(); i++)
- rs.buffer[i] |= set1.buffer[i];
- for (int i = 0; i<set2.buffer.Count(); i++)
- rs.buffer[i] |= set2.buffer[i];
- }
- static void Intersect(IntSet & rs, const IntSet & set1, const IntSet & set2)
- {
- rs.buffer.SetSize(Math::Min(set1.buffer.Count(), set2.buffer.Count()));
- for (int i = 0; i<rs.buffer.Count(); i++)
- rs.buffer[i] = set1.buffer[i] & set2.buffer[i];
- }
- static void Subtract(IntSet & rs, const IntSet & set1, const IntSet & set2)
+ return true;
+ }
+ bool operator != (const IntSet & set)
+ {
+ return !(*this == set);
+ }
+ void IntersectWith(const IntSet & set)
+ {
+ if (set.buffer.Count() < buffer.Count())
+ memset(buffer.Buffer() + set.buffer.Count(), 0, (buffer.Count()-set.buffer.Count())*sizeof(int));
+ for (int i = 0; i<Math::Min(set.buffer.Count(), buffer.Count()); i++)
{
- rs.buffer.SetSize(set1.buffer.Count());
- for (int i = 0; i<Math::Min(set1.buffer.Count(), set2.buffer.Count()); i++)
- rs.buffer[i] = set1.buffer[i] & (~set2.buffer[i]);
+ buffer[i] &= set.buffer[i];
}
- static bool HasIntersection(const IntSet & set1, const IntSet & set2)
+ }
+ static void Union(IntSet & rs, const IntSet & set1, const IntSet & set2)
+ {
+ rs.buffer.SetSize(Math::Max(set1.buffer.Count(), set2.buffer.Count()));
+ rs.Clear();
+ for (int i = 0; i<set1.buffer.Count(); i++)
+ rs.buffer[i] |= set1.buffer[i];
+ for (int i = 0; i<set2.buffer.Count(); i++)
+ rs.buffer[i] |= set2.buffer[i];
+ }
+ static void Intersect(IntSet & rs, const IntSet & set1, const IntSet & set2)
+ {
+ rs.buffer.SetSize(Math::Min(set1.buffer.Count(), set2.buffer.Count()));
+ for (int i = 0; i<rs.buffer.Count(); i++)
+ rs.buffer[i] = set1.buffer[i] & set2.buffer[i];
+ }
+ static void Subtract(IntSet & rs, const IntSet & set1, const IntSet & set2)
+ {
+ rs.buffer.SetSize(set1.buffer.Count());
+ for (int i = 0; i<Math::Min(set1.buffer.Count(), set2.buffer.Count()); i++)
+ rs.buffer[i] = set1.buffer[i] & (~set2.buffer[i]);
+ }
+ static bool HasIntersection(const IntSet & set1, const IntSet & set2)
+ {
+ for (int i = 0; i<Math::Min(set1.buffer.Count(), set2.buffer.Count()); i++)
{
- for (int i = 0; i<Math::Min(set1.buffer.Count(), set2.buffer.Count()); i++)
- {
- if (set1.buffer[i] & set2.buffer[i])
- return true;
- }
- return false;
+ if (set1.buffer[i] & set2.buffer[i])
+ return true;
}
- };
- }
+ return false;
+ }
+ };
}
#endif \ No newline at end of file
diff --git a/source/core/link.h b/source/core/link.h
index 6abd5a9d5..f74a250b9 100644
--- a/source/core/link.h
+++ b/source/core/link.h
@@ -4,333 +4,331 @@
#include "Common.h"
#include "Exception.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
- {
- template<typename T>
- class LinkedList;
+ template<typename T>
+ class LinkedList;
- template<typename T>
- class LinkedNode
+ template<typename T>
+ class LinkedNode
+ {
+ template<typename T1>
+ friend class LinkedList;
+ private:
+ LinkedNode<T> *pPrev, *pNext;
+ LinkedList<T> * FLink;
+ public:
+ T Value;
+ LinkedNode (LinkedList<T> * lnk):FLink(lnk)
{
- template<typename T1>
- friend class LinkedList;
- private:
- LinkedNode<T> *pPrev, *pNext;
- LinkedList<T> * FLink;
- public:
- T Value;
- LinkedNode (LinkedList<T> * lnk):FLink(lnk)
- {
- pPrev = pNext = 0;
- };
- LinkedNode<T> * GetPrevious()
- {
- return pPrev;
- };
- LinkedNode<T> * GetNext()
- {
- return pNext;
- };
- LinkedNode<T> * InsertAfter(const T & nData)
- {
- LinkedNode<T> * n = new LinkedNode<T>(FLink);
- n->Value = nData;
- n->pPrev = this;
- n->pNext = this->pNext;
- LinkedNode<T> *npp = n->pNext;
- if (npp)
- {
- npp->pPrev = n;
- }
- pNext = n;
- if (!n->pNext)
- FLink->FTail = n;
- FLink->FCount ++;
- return n;
- };
- LinkedNode<T> * InsertBefore(const T & nData)
- {
- LinkedNode<T> * n = new LinkedNode<T>(FLink);
- n->Value = nData;
- n->pPrev = pPrev;
- n->pNext = this;
- pPrev = n;
- LinkedNode<T> *npp = n->pPrev;
- if (npp)
- npp->pNext = n;
- if (!n->pPrev)
- FLink->FHead = n;
- FLink->FCount ++;
- return n;
- };
- void Delete()
+ pPrev = pNext = 0;
+ };
+ LinkedNode<T> * GetPrevious()
+ {
+ return pPrev;
+ };
+ LinkedNode<T> * GetNext()
+ {
+ return pNext;
+ };
+ LinkedNode<T> * InsertAfter(const T & nData)
+ {
+ LinkedNode<T> * n = new LinkedNode<T>(FLink);
+ n->Value = nData;
+ n->pPrev = this;
+ n->pNext = this->pNext;
+ LinkedNode<T> *npp = n->pNext;
+ if (npp)
{
- 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;
+ npp->pPrev = n;
}
+ pNext = n;
+ if (!n->pNext)
+ FLink->FTail = n;
+ FLink->FCount ++;
+ return n;
};
- template<typename T>
- class LinkedList
+ LinkedNode<T> * InsertBefore(const T & nData)
{
- template<typename T1>
- friend class LinkedNode;
- private:
- LinkedNode<T> * FHead, *FTail;
- int FCount;
- public:
- class Iterator
- {
- public:
- LinkedNode<T> * Current, *Next;
- void SetCurrent(LinkedNode<T> * cur)
- {
- Current = cur;
- if (Current)
- Next = Current->GetNext();
- else
- Next = 0;
- }
- Iterator()
- {
- Current = Next = 0;
- }
- Iterator(LinkedNode<T> * 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
+ LinkedNode<T> * n = new LinkedNode<T>(FLink);
+ n->Value = nData;
+ n->pPrev = pPrev;
+ n->pNext = this;
+ pPrev = n;
+ LinkedNode<T> *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)
{
- return Iterator(FHead);
+ FLink->FHead = pNext;
}
- Iterator end() const
+ if (FLink->FTail == this)
{
- return Iterator(0);
+ FLink->FTail = pPrev;
}
+ delete this;
+ }
+ };
+ template<typename T>
+ class LinkedList
+ {
+ template<typename T1>
+ friend class LinkedNode;
+ private:
+ LinkedNode<T> * FHead, *FTail;
+ int FCount;
+ public:
+ class Iterator
+ {
public:
- LinkedList() : FHead(0), FTail(0), FCount(0)
+ LinkedNode<T> * Current, *Next;
+ void SetCurrent(LinkedNode<T> * cur)
{
+ Current = cur;
+ if (Current)
+ Next = Current->GetNext();
+ else
+ Next = 0;
}
- ~LinkedList()
+ Iterator()
{
- Clear();
+ Current = Next = 0;
}
- LinkedList(const LinkedList<T> & link) : FHead(0), FTail(0), FCount(0)
+ Iterator(LinkedNode<T> * cur)
{
- this->operator=(link);
+ SetCurrent(cur);
}
- LinkedList(LinkedList<T> && link) : FHead(0), FTail(0), FCount(0)
+ T & operator *() const
{
- this->operator=(_Move(link));
+ return Current->Value;
}
- LinkedList<T> & operator = (LinkedList<T> && link)
+ Iterator& operator ++()
{
- 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;
+ SetCurrent(Next);
return *this;
}
- LinkedList<T> & operator = (const LinkedList<T> & link)
+ Iterator operator ++(int)
{
- if (FHead != 0)
- Clear();
- auto p = link.FHead;
- while (p)
- {
- AddLast(p->Value);
- p = p->GetNext();
- }
- return *this;
+ Iterator rs = *this;
+ SetCurrent(Next);
+ return rs;
}
- template<typename IteratorFunc>
- void ForEach(const IteratorFunc & f)
+ bool operator != (const Iterator & iter) const
{
- auto p = FHead;
- while (p)
- {
- f(p->Value);
- p = p->GetNext();
- }
+ return Current != iter.Current;
}
- LinkedNode<T> * GetNode(int x)
+ bool operator == (const Iterator & iter) const
{
- LinkedNode<T> *pCur = FHead;
- for (int i=0;i<x;i++)
- {
- if (pCur)
- pCur = pCur->pNext;
- else
- throw "Index out of range";
- }
- return pCur;
- };
- LinkedNode<T> * Find(const T& fData)
- {
- for (LinkedNode<T> * pCur = FHead; pCur; pCur = pCur->pNext)
- {
- if (pCur->Value == fData)
- return pCur;
- }
- return 0;
- };
- LinkedNode<T> * FirstNode() const
- {
- return FHead;
- };
- T & First() const
- {
- if (!FHead)
- throw IndexOutofRangeException("LinkedList: index out of range.");
- return FHead->Value;
+ return Current == iter.Current;
}
- T & Last() const
+ };
+ 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<T> & link) : FHead(0), FTail(0), FCount(0)
+ {
+ this->operator=(link);
+ }
+ LinkedList(LinkedList<T> && link) : FHead(0), FTail(0), FCount(0)
+ {
+ this->operator=(_Move(link));
+ }
+ LinkedList<T> & operator = (LinkedList<T> && 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<T> & operator = (const LinkedList<T> & link)
+ {
+ if (FHead != 0)
+ Clear();
+ auto p = link.FHead;
+ while (p)
{
- if (!FTail)
- throw IndexOutofRangeException("LinkedList: index out of range.");
- return FTail->Value;
+ AddLast(p->Value);
+ p = p->GetNext();
}
- LinkedNode<T> * LastNode() const
- {
- return FTail;
- };
- LinkedNode<T> * AddLast(const T & nData)
- {
- LinkedNode<T> * n = new LinkedNode<T>(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<T> * AddLast()
- {
- LinkedNode<T> * n = new LinkedNode<T>(this);
- n->pPrev = FTail;
- if (FTail)
- FTail->pNext = n;
- n->pNext = 0;
- FTail = n;
- if (!FHead)
- FHead = n;
- FCount ++;
- return n;
- };
- LinkedNode<T> * AddFirst(const T& nData)
+ return *this;
+ }
+ template<typename IteratorFunc>
+ void ForEach(const IteratorFunc & f)
+ {
+ auto p = FHead;
+ while (p)
{
- LinkedNode<T> *n = new LinkedNode<T>(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<T>*n, int Count = 1)
+ f(p->Value);
+ p = p->GetNext();
+ }
+ }
+ LinkedNode<T> * GetNode(int x)
+ {
+ LinkedNode<T> *pCur = FHead;
+ for (int i=0;i<x;i++)
{
- LinkedNode<T> *n1,*n2 = 0, *tn;
- n1 = n->pPrev;
- tn = n;
- int numDeleted = 0;
- for (int i=0; i<Count; i++)
- {
- n2 = tn->pNext;
- delete tn;
- tn = n2;
- numDeleted++;
- if (tn == 0)
- break;
- }
- if (n1)
- n1->pNext = n2;
- else
- FHead = n2;
- if (n2)
- n2->pPrev = n1;
+ if (pCur)
+ pCur = pCur->pNext;
else
- FTail = n1;
- FCount -= numDeleted;
+ throw "Index out of range";
}
- void Clear()
+ return pCur;
+ };
+ LinkedNode<T> * Find(const T& fData)
+ {
+ for (LinkedNode<T> * pCur = FHead; pCur; pCur = pCur->pNext)
{
- for (LinkedNode<T> *n = FHead; n; )
- {
- LinkedNode<T> * tmp = n->pNext;
- delete n;
- n = tmp;
- }
- FHead = 0;
- FTail = 0;
- FCount = 0;
+ if (pCur->Value == fData)
+ return pCur;
}
- List<T> ToList() const
+ return 0;
+ };
+ LinkedNode<T> * 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<T> * LastNode() const
+ {
+ return FTail;
+ };
+ LinkedNode<T> * AddLast(const T & nData)
+ {
+ LinkedNode<T> * n = new LinkedNode<T>(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<T> * AddLast()
+ {
+ LinkedNode<T> * n = new LinkedNode<T>(this);
+ n->pPrev = FTail;
+ if (FTail)
+ FTail->pNext = n;
+ n->pNext = 0;
+ FTail = n;
+ if (!FHead)
+ FHead = n;
+ FCount ++;
+ return n;
+ };
+ LinkedNode<T> * AddFirst(const T& nData)
+ {
+ LinkedNode<T> *n = new LinkedNode<T>(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<T>*n, int Count = 1)
+ {
+ LinkedNode<T> *n1,*n2 = 0, *tn;
+ n1 = n->pPrev;
+ tn = n;
+ int numDeleted = 0;
+ for (int i=0; i<Count; i++)
{
- List<T> rs;
- rs.Reserve(FCount);
- for (auto & item : *this)
- {
- rs.Add(item);
- }
- return rs;
+ n2 = tn->pNext;
+ delete tn;
+ tn = n2;
+ numDeleted++;
+ if (tn == 0)
+ break;
}
- int Count()
+ if (n1)
+ n1->pNext = n2;
+ else
+ FHead = n2;
+ if (n2)
+ n2->pPrev = n1;
+ else
+ FTail = n1;
+ FCount -= numDeleted;
+ }
+ void Clear()
+ {
+ for (LinkedNode<T> *n = FHead; n; )
{
- return FCount;
+ LinkedNode<T> * tmp = n->pNext;
+ delete n;
+ n = tmp;
}
- };
- }
+ FHead = 0;
+ FTail = 0;
+ FCount = 0;
+ }
+ List<T> ToList() const
+ {
+ List<T> 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
index bd40f1bc2..52af3abd0 100644
--- a/source/core/linq.h
+++ b/source/core/linq.h
@@ -3,666 +3,662 @@
#include "List.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
- {
-
- template <typename T>
- T ConstructT();
+ template <typename T>
+ T ConstructT();
- template <typename T>
- class RemoveReference
- {
- public:
- typedef T Type;
- };
+ template <typename T>
+ class RemoveReference
+ {
+ public:
+ typedef T Type;
+ };
- template <typename T>
- class RemoveReference<T&>
- {
- public:
- typedef T Type;
- };
+ template <typename T>
+ class RemoveReference<T&>
+ {
+ public:
+ typedef T Type;
+ };
- template <typename T>
- class RemoveReference<T&&>
- {
- public:
- typedef T Type;
- };
+ template <typename T>
+ class RemoveReference<T&&>
+ {
+ public:
+ typedef T Type;
+ };
- template<typename T>
- struct RemovePointer
- {
- typedef T Type;
- };
+ template<typename T>
+ struct RemovePointer
+ {
+ typedef T Type;
+ };
- template<typename T>
- struct RemovePointer<T*>
- {
- typedef T Type;
- };
+ template<typename T>
+ struct RemovePointer<T*>
+ {
+ typedef T Type;
+ };
- template <typename TQueryable1, typename TEnumerator1, typename TQueryable2, typename TEnumerator2, typename T>
- class ConcatQuery
+ template <typename TQueryable1, typename TEnumerator1, typename TQueryable2, typename TEnumerator2, typename T>
+ class ConcatQuery
+ {
+ private:
+ TQueryable1 items1;
+ TQueryable2 items2;
+ public:
+ ConcatQuery(const TQueryable1 & queryable1, const TQueryable2 & queryable2)
+ : items1(queryable1), items2(queryable2)
+ {}
+ class Enumerator
{
private:
- TQueryable1 items1;
- TQueryable2 items2;
+ TEnumerator1 ptr1;
+ TEnumerator1 end1;
+ TEnumerator2 ptr2;
+ TEnumerator2 end2;
public:
- ConcatQuery(const TQueryable1 & queryable1, const TQueryable2 & queryable2)
- : items1(queryable1), items2(queryable2)
+ Enumerator(const Enumerator &) = default;
+ Enumerator(TEnumerator1 pptr, TEnumerator1 pend, TEnumerator2 pptr2, TEnumerator2 pend2)
+ : ptr1(pptr), end1(pend), ptr2(pptr2), end2(pend2)
{}
- class Enumerator
+ T operator *() const
{
- 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());
+ if (ptr1 != end1)
+ return *(ptr1);
+ else
+ return *(ptr2);
}
- Enumerator end() const
+ Enumerator& operator ++()
{
- return Enumerator(items1.end(), items1.end(), items2.end(), items2.end());
+ if (ptr1 != end1)
+ ++ptr1;
+ else
+ ++ptr2;
+ return *this;
}
- };
-
- template <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
- class WhereQuery
- {
- private:
- TQueryable items;
- TFunc func;
- public:
- WhereQuery(const TQueryable & queryable, const TFunc & f)
- : items(queryable), func(f)
- {}
- class Enumerator
+ Enumerator operator ++(int)
{
- 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 rs = *this;
+ ++rs;
+ return rs;
}
- Enumerator end() const
+ bool operator != (const Enumerator & iter) const
{
- return Enumerator(items.end(), items.end(), func);
+ return ptr1 != iter.ptr1 || ptr2 != iter.ptr2;
}
- };
-
- template <typename TQueryable, typename TEnumerator, typename T>
- class SkipQuery
- {
- private:
- TQueryable items;
- int count = 0;
- public:
- SkipQuery(const TQueryable & queryable, int pCount)
- : items(queryable), count(pCount)
- {}
- class Enumerator
+ bool operator == (const Enumerator & iter) const
{
- 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());
+ 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 <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
- class SelectQuery
+ template <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
+ class WhereQuery
+ {
+ private:
+ TQueryable items;
+ TFunc func;
+ public:
+ WhereQuery(const TQueryable & queryable, const TFunc & f)
+ : items(queryable), func(f)
+ {}
+ class Enumerator
{
private:
- TQueryable items;
- TFunc func;
+ TEnumerator ptr;
+ TEnumerator end;
+ const TFunc * func;
public:
- SelectQuery(const TQueryable & queryable, const TFunc & f)
- : items(queryable), func(f)
+ Enumerator(const Enumerator &) = default;
+ Enumerator(TEnumerator ptr, TEnumerator end, const TFunc & f)
+ : ptr(ptr), end(end), 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
+ T operator *() const
{
- return Enumerator(items.begin(), items.end(), func);
+ return *(ptr);
}
- Enumerator end() const
+ Enumerator& operator ++()
{
- return Enumerator(items.end(), items.end(), func);
- }
- };
-
- template <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
- class SelectManyQuery
- {
- private:
- TQueryable items;
- TFunc func;
- SelectManyQuery()
- {}
- public:
- SelectManyQuery(const TQueryable & queryable, const TFunc & f)
- : items(queryable), func(f)
- {}
- template<typename TItems, typename TItemPtr>
- 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 ++()
+ ++ptr;
+ while (ptr != end)
{
- ++subPtr;
- while (subPtr == items.end() && ptr != end)
- {
+ if ((*func)(*ptr))
+ break;
+ else
++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
+ return *this;
+ }
+ Enumerator operator ++(int)
+ {
+ Enumerator rs = *this;
+ while (rs.ptr != end)
{
- if (ptr == iter.ptr)
- {
- if (ptr == end)
- return true;
- else
- return subPtr == iter.subPtr;
- }
- else
- return false;
+ if ((*func)(*rs.ptr))
+ break;
+ ++rs.ptr;
}
- };
- auto begin() const ->Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>
+ return rs;
+ }
+ bool operator != (const Enumerator & iter) const
{
- return Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>(items.begin(), items.end(), func);
+ return ptr != iter.ptr;
}
- auto end() const ->Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>
+ bool operator == (const Enumerator & iter) const
{
- return Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>(items.end(), items.end(), func);
+ return ptr == iter.ptr;
}
};
-
- template <typename T>
- struct EnumeratorType
- {
- typedef decltype(ConstructT<T>().begin()) Type;
- };
-
- template <typename TFunc, typename TArg>
- class ExtractReturnType
+ Enumerator begin() const
{
- public:
- static TFunc * f;
- static TArg ConstructArg() {};
- typedef decltype((*f)(ConstructArg())) ReturnType;
- };
-
- template <typename T>
- class ExtractItemType
+ auto ptr = items.begin();
+ auto end = items.end();
+ while (ptr != end)
+ {
+ if (func(*ptr))
+ break;
+ ++ptr;
+ }
+ return Enumerator(ptr, end, func);
+ }
+ Enumerator end() const
{
- public:
- typedef typename RemovePointer<decltype(ConstructT<T>().begin())>::Type Type;
- };
+ return Enumerator(items.end(), items.end(), func);
+ }
+ };
- template <typename TQueryable, typename TEnumerator, typename T>
- class Queryable
+ template <typename TQueryable, typename TEnumerator, typename T>
+ class SkipQuery
+ {
+ private:
+ TQueryable items;
+ int count = 0;
+ public:
+ SkipQuery(const TQueryable & queryable, int pCount)
+ : items(queryable), count(pCount)
+ {}
+ class Enumerator
{
private:
- TQueryable items;
+ TEnumerator ptr;
+ TEnumerator end;
public:
- auto begin() const -> decltype(items.begin())
+ Enumerator(const Enumerator &) = default;
+ Enumerator(TEnumerator pptr, TEnumerator pend)
+ : ptr(pptr), end(pend)
{
- return items.begin();
}
- auto end() const -> decltype(items.end())
+ T operator *() const
{
- return items.end();
+ return *(ptr);
}
- public:
- const TQueryable & GetItems() const
+ Enumerator& operator ++()
{
- return items;
+ ++ptr;
+ return *this;
}
- Queryable(const TQueryable & items)
- : items(items)
- {}
-
- Queryable<SkipQuery<TQueryable, TEnumerator, T>, typename SkipQuery<TQueryable, TEnumerator, T>::Enumerator, T> Skip(int count) const
+ Enumerator operator ++(int)
{
- return Queryable<SkipQuery<TQueryable, TEnumerator, T>, typename SkipQuery<TQueryable, TEnumerator, T>::Enumerator, T>(SkipQuery<TQueryable, TEnumerator, T>(items, count));
+ Enumerator rs = *this;
+ ++ptr;
+ return rs;
}
-
- template<typename TQueryable2, typename TEnumerator2>
- Queryable<ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>, typename ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>::Enumerator, T> Concat(const Queryable<TQueryable2, TEnumerator2, T> & other) const
+ bool operator != (const Enumerator & iter) const
{
- return Queryable<ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>, typename ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>::Enumerator, T>(ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>(this->items, other.GetItems()));
+ return ptr != iter.ptr;
}
-
- template<typename TFunc>
- Queryable<WhereQuery<TQueryable, TEnumerator, T, TFunc>, typename WhereQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, T> Where(const TFunc & f) const
+ bool operator == (const Enumerator & iter) const
{
- return Queryable<WhereQuery<TQueryable, TEnumerator, T, TFunc>, typename WhereQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, T>(WhereQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
+ 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<typename TFunc>
- Queryable<SelectQuery<TQueryable, TEnumerator, T, TFunc>, typename SelectQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, typename RemoveReference<typename ExtractReturnType<TFunc, T>::ReturnType>::Type> Select(const TFunc & f) const
+ template <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
+ 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 Queryable<SelectQuery<TQueryable, TEnumerator, T, TFunc>, typename SelectQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, typename RemoveReference<typename ExtractReturnType<TFunc, T>::ReturnType>::Type>(SelectQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
+ return (*func)(*ptr);
}
-
- template<typename TFunc>
- auto SelectMany(const TFunc & f) const ->Queryable<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>, typename EnumeratorType<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>>::Type, typename ExtractItemType<decltype(f(ConstructT<T>()))>::Type>
+ Enumerator& operator ++()
{
- return Queryable<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>, typename EnumeratorType<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>>::Type, typename ExtractItemType<decltype(f(ConstructT<T>()))>::Type>(SelectManyQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
+ ++ptr;
+ return *this;
}
-
- template<typename TAggregateResult, typename TFunc>
- auto Aggregate(const TAggregateResult & initial, const TFunc & f) const -> decltype(f(initial, *items.begin()))
+ Enumerator operator ++(int)
{
- TAggregateResult rs = initial;
- for (auto && x : items)
- rs = f(rs, x);
+ Enumerator rs = *this;
+ ++rs;
return rs;
}
-
- template<typename TFunc>
- bool Any(const TFunc & condition) const
+ bool operator != (const Enumerator & iter) const
{
- for (auto && x : items)
- if (condition(x))
- return true;
- return false;
+ return !(ptr == iter.ptr);
}
-
- template<typename TFunc>
- T & First(const TFunc & condition) const
+ bool operator == (const Enumerator & iter) const
{
- for (auto && x : items)
- if (condition(x))
- return x;
+ 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 <typename TFunc>
- T Max(const TFunc & selector) const
+ template <typename TQueryable, typename TEnumerator, typename T, typename TFunc>
+ class SelectManyQuery
+ {
+ private:
+ TQueryable items;
+ TFunc func;
+ SelectManyQuery()
+ {}
+ public:
+ SelectManyQuery(const TQueryable & queryable, const TFunc & f)
+ : items(queryable), func(f)
+ {}
+ template<typename TItems, typename TItemPtr>
+ 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)
{
- return Aggregate(*items.begin(), [&](const T & v0, const T & v1)
+ if (ptr != end)
{
- return selector(v0) > selector(v1) ? v0 : v1;
- });
+ items = f(*ptr);
+ subPtr = items.begin();
+ }
}
-
- template <typename TFunc>
- T Min(const TFunc & selector) const
+ auto operator *() const -> decltype(*subPtr)
{
- return Aggregate(*items.begin(), [&](const T & v0, const T & v1)
- {
- return selector(v0) < selector(v1) ? v0 : v1;
- });
+ return *subPtr;
}
-
- template <typename TFunc>
- auto Sum(const TFunc & selector) const -> decltype(selector(ConstructT<T>()))
+ Enumerator& operator ++()
{
- decltype(selector(ConstructT<T>())) rs(0);
- for (auto && x : items)
- rs = rs + selector(x);
- return rs;
- }
+ ++subPtr;
+ while (subPtr == items.end() && ptr != end)
+ {
+ ++ptr;
+ if (ptr != end)
+ {
+ items = (*func)(*ptr);
+ subPtr = items.begin();
+ }
+ else
+ break;
+ }
- T Max() const
- {
- return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 > v1 ? v0 : v1; });
+ return *this;
}
-
- T Min() const
+ Enumerator operator ++(int)
{
- return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 < v1 ? v0 : v1; });
+ Enumerator rs = *this;
+ ++rs;
+ return rs;
}
-
- T Sum() const
+ bool operator != (const Enumerator & iter) const
{
- T rs = T(0);
- for (auto && x : items)
- rs = rs + x;
- return rs;
+ return !operator==(iter);
}
-
- T Avg() const
+ bool operator == (const Enumerator & iter) const
{
- T rs = T(0);
- int count = 0;
- for (auto && x : items)
+ if (ptr == iter.ptr)
{
- rs = rs + x;
- count++;
+ if (ptr == end)
+ return true;
+ else
+ return subPtr == iter.subPtr;
}
- return rs / count;
+ else
+ return false;
}
+ };
+ auto begin() const ->Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>
+ {
+ return Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>(items.begin(), items.end(), func);
+ }
+ auto end() const ->Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>
+ {
+ return Enumerator<decltype(func(ConstructT<T>())), decltype(func(ConstructT<T>()).begin())>(items.end(), items.end(), func);
+ }
+ };
- int Count() const
- {
- int rs = 0;
- for (auto && x : items)
- rs++;
- return rs;
- }
+ template <typename T>
+ struct EnumeratorType
+ {
+ typedef decltype(ConstructT<T>().begin()) Type;
+ };
- List<T> ToList() const
- {
- List<T> rs;
- for (auto && val : items)
- rs.Add(val);
- return rs;
- }
- };
+ template <typename TFunc, typename TArg>
+ class ExtractReturnType
+ {
+ public:
+ static TFunc * f;
+ static TArg ConstructArg() {};
+ typedef decltype((*f)(ConstructArg())) ReturnType;
+ };
+
+ template <typename T>
+ class ExtractItemType
+ {
+ public:
+ typedef typename RemovePointer<decltype(ConstructT<T>().begin())>::Type Type;
+ };
+
+ template <typename TQueryable, typename TEnumerator, typename T>
+ 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<SkipQuery<TQueryable, TEnumerator, T>, typename SkipQuery<TQueryable, TEnumerator, T>::Enumerator, T> Skip(int count) const
+ {
+ return Queryable<SkipQuery<TQueryable, TEnumerator, T>, typename SkipQuery<TQueryable, TEnumerator, T>::Enumerator, T>(SkipQuery<TQueryable, TEnumerator, T>(items, count));
+ }
+ template<typename TQueryable2, typename TEnumerator2>
+ Queryable<ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>, typename ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>::Enumerator, T> Concat(const Queryable<TQueryable2, TEnumerator2, T> & other) const
+ {
+ return Queryable<ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>, typename ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>::Enumerator, T>(ConcatQuery<TQueryable, TEnumerator, TQueryable2, TEnumerator2, T>(this->items, other.GetItems()));
+ }
- template<typename T, typename TAllocator>
- inline Queryable<ArrayView<T>, T*, T> From(const List<T, TAllocator> & list)
+ template<typename TFunc>
+ Queryable<WhereQuery<TQueryable, TEnumerator, T, TFunc>, typename WhereQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, T> Where(const TFunc & f) const
{
- return Queryable<ArrayView<T>, T*, T>(list.GetArrayView());
+ return Queryable<WhereQuery<TQueryable, TEnumerator, T, TFunc>, typename WhereQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, T>(WhereQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
}
- template<typename T, typename TAllocator>
- inline Queryable<List<T, TAllocator>, T*, T> From(List<T, TAllocator> && list)
+ template<typename TFunc>
+ Queryable<SelectQuery<TQueryable, TEnumerator, T, TFunc>, typename SelectQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, typename RemoveReference<typename ExtractReturnType<TFunc, T>::ReturnType>::Type> Select(const TFunc & f) const
{
- return Queryable<List<T, TAllocator>, T*, T>(_Move(list));
+ return Queryable<SelectQuery<TQueryable, TEnumerator, T, TFunc>, typename SelectQuery<TQueryable, TEnumerator, T, TFunc>::Enumerator, typename RemoveReference<typename ExtractReturnType<TFunc, T>::ReturnType>::Type>(SelectQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
}
- template<typename T>
- inline Queryable<ArrayView<T>, T*, T> From(const ArrayView<T> & list)
+ template<typename TFunc>
+ auto SelectMany(const TFunc & f) const ->Queryable<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>, typename EnumeratorType<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>>::Type, typename ExtractItemType<decltype(f(ConstructT<T>()))>::Type>
{
- return Queryable<ArrayView<T>, T*, T>(list);
+ return Queryable<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>, typename EnumeratorType<SelectManyQuery<TQueryable, TEnumerator, T, TFunc>>::Type, typename ExtractItemType<decltype(f(ConstructT<T>()))>::Type>(SelectManyQuery<TQueryable, TEnumerator, T, TFunc>(items, f));
}
- template<typename T, int count>
- inline Queryable<Array<T, count>, T*, T> From(const Array<T, count> & list)
+ template<typename TAggregateResult, typename TFunc>
+ auto Aggregate(const TAggregateResult & initial, const TFunc & f) const -> decltype(f(initial, *items.begin()))
{
- return Queryable<Array<T, count>, T*, T>(list);
+ TAggregateResult rs = initial;
+ for (auto && x : items)
+ rs = f(rs, x);
+ return rs;
}
- template<typename T>
- inline auto From(const T & list) -> Queryable<T, decltype(list.begin()), decltype(*list.begin())>
+ template<typename TFunc>
+ bool Any(const TFunc & condition) const
{
- return Queryable<T, decltype(list.begin()), decltype(*list.begin())>(list);
+ for (auto && x : items)
+ if (condition(x))
+ return true;
+ return false;
}
- template<typename T>
- inline Queryable<Array<T, 1>, T*, T> FromSingle(const T & obj)
+ template<typename TFunc>
+ T & First(const TFunc & condition) const
{
- Array<T, 1> arr;
- arr.Add(obj);
- return From(arr);
+ for (auto && x : items)
+ if (condition(x))
+ return x;
}
- template<typename T>
- struct LinkedListView
+ template <typename TFunc>
+ T Max(const TFunc & selector) const
{
- typename LinkedList<T>::Iterator start, last;
- typename LinkedList<T>::Iterator begin() const
+ return Aggregate(*items.begin(), [&](const T & v0, const T & v1)
{
- return start;
- }
- typename LinkedList<T>::Iterator end() const
+ return selector(v0) > selector(v1) ? v0 : v1;
+ });
+ }
+
+ template <typename TFunc>
+ T Min(const TFunc & selector) const
+ {
+ return Aggregate(*items.begin(), [&](const T & v0, const T & v1)
{
- return last;
- }
- };
+ return selector(v0) < selector(v1) ? v0 : v1;
+ });
+ }
- template<typename T>
- inline Queryable<LinkedListView<T>, LinkedNode<T>, T> From(const LinkedList<T> & list)
+ template <typename TFunc>
+ auto Sum(const TFunc & selector) const -> decltype(selector(ConstructT<T>()))
{
- LinkedListView<T> view;
- view.start = list.begin();
- view.last = list.end();
- return Queryable<LinkedListView<T>, LinkedNode<T>, T>(view);
+ decltype(selector(ConstructT<T>())) rs(0);
+ for (auto && x : items)
+ rs = rs + selector(x);
+ return rs;
}
- template<typename TKey, typename TValue>
- struct EnumerableDictView
+ T Max() const
{
- typename EnumerableDictionary<TKey, TValue>::Iterator start, last;
- typename EnumerableDictionary<TKey, TValue>::Iterator begin() const
- {
- return start;
- }
- typename EnumerableDictionary<TKey, TValue>::Iterator end() const
- {
- return last;
- }
- };
+ return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 > v1 ? v0 : v1; });
+ }
- template<typename TKey, typename TValue>
- inline Queryable<EnumerableDictView<TKey, TValue>, typename EnumerableDictionary<TKey, TValue>::Iterator, KeyValuePair<TKey, TValue>> From(const EnumerableDictionary<TKey, TValue> & dict)
+ T Min() const
{
- EnumerableDictView<TKey, TValue> view;
- view.start = dict.begin();
- view.last = dict.end();
- return Queryable<EnumerableDictView<TKey, TValue>, typename EnumerableDictionary<TKey, TValue>::Iterator, KeyValuePair<TKey, TValue>>(view);
+ return Aggregate(*items.begin(), [](const T & v0, const T & v1) {return v0 < v1 ? v0 : v1; });
}
- template<typename TKey>
- struct EnumerableHashSetView
+ T Sum() const
{
- typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator start, last;
- typename EnumerableHashSet<TKey>::Iterator begin() const
- {
- return start;
- }
- typename EnumerableHashSet<TKey>::Iterator end() 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)
{
- return last;
+ rs = rs + x;
+ count++;
}
- };
+ return rs / count;
+ }
+
+ int Count() const
+ {
+ int rs = 0;
+ for (auto && x : items)
+ rs++;
+ return rs;
+ }
+
+ List<T> ToList() const
+ {
+ List<T> rs;
+ for (auto && val : items)
+ rs.Add(val);
+ return rs;
+ }
+ };
+
+
+ template<typename T, typename TAllocator>
+ inline Queryable<ArrayView<T>, T*, T> From(const List<T, TAllocator> & list)
+ {
+ return Queryable<ArrayView<T>, T*, T>(list.GetArrayView());
+ }
- template<typename TKey>
- inline Queryable<EnumerableHashSetView<TKey>, typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator, TKey> From(const EnumerableHashSet<TKey> & dict)
+ template<typename T, typename TAllocator>
+ inline Queryable<List<T, TAllocator>, T*, T> From(List<T, TAllocator> && list)
+ {
+ return Queryable<List<T, TAllocator>, T*, T>(_Move(list));
+ }
+
+ template<typename T>
+ inline Queryable<ArrayView<T>, T*, T> From(const ArrayView<T> & list)
+ {
+ return Queryable<ArrayView<T>, T*, T>(list);
+ }
+
+ template<typename T, int count>
+ inline Queryable<Array<T, count>, T*, T> From(const Array<T, count> & list)
+ {
+ return Queryable<Array<T, count>, T*, T>(list);
+ }
+
+ template<typename T>
+ inline auto From(const T & list) -> Queryable<T, decltype(list.begin()), decltype(*list.begin())>
+ {
+ return Queryable<T, decltype(list.begin()), decltype(*list.begin())>(list);
+ }
+
+ template<typename T>
+ inline Queryable<Array<T, 1>, T*, T> FromSingle(const T & obj)
+ {
+ Array<T, 1> arr;
+ arr.Add(obj);
+ return From(arr);
+ }
+
+ template<typename T>
+ struct LinkedListView
+ {
+ typename LinkedList<T>::Iterator start, last;
+ typename LinkedList<T>::Iterator begin() const
+ {
+ return start;
+ }
+ typename LinkedList<T>::Iterator end() const
+ {
+ return last;
+ }
+ };
+
+ template<typename T>
+ inline Queryable<LinkedListView<T>, LinkedNode<T>, T> From(const LinkedList<T> & list)
+ {
+ LinkedListView<T> view;
+ view.start = list.begin();
+ view.last = list.end();
+ return Queryable<LinkedListView<T>, LinkedNode<T>, T>(view);
+ }
+
+ template<typename TKey, typename TValue>
+ struct EnumerableDictView
+ {
+ typename EnumerableDictionary<TKey, TValue>::Iterator start, last;
+ typename EnumerableDictionary<TKey, TValue>::Iterator begin() const
+ {
+ return start;
+ }
+ typename EnumerableDictionary<TKey, TValue>::Iterator end() const
{
- EnumerableHashSetView<TKey> view;
- view.start = dict.begin();
- view.last = dict.end();
- return Queryable<EnumerableHashSetView<TKey>, typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator, TKey>(view);
+ return last;
}
+ };
+
+ template<typename TKey, typename TValue>
+ inline Queryable<EnumerableDictView<TKey, TValue>, typename EnumerableDictionary<TKey, TValue>::Iterator, KeyValuePair<TKey, TValue>> From(const EnumerableDictionary<TKey, TValue> & dict)
+ {
+ EnumerableDictView<TKey, TValue> view;
+ view.start = dict.begin();
+ view.last = dict.end();
+ return Queryable<EnumerableDictView<TKey, TValue>, typename EnumerableDictionary<TKey, TValue>::Iterator, KeyValuePair<TKey, TValue>>(view);
+ }
+
+ template<typename TKey>
+ struct EnumerableHashSetView
+ {
+ typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator start, last;
+ typename EnumerableHashSet<TKey>::Iterator begin() const
+ {
+ return start;
+ }
+ typename EnumerableHashSet<TKey>::Iterator end() const
+ {
+ return last;
+ }
+ };
+
+ template<typename TKey>
+ inline Queryable<EnumerableHashSetView<TKey>, typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator, TKey> From(const EnumerableHashSet<TKey> & dict)
+ {
+ EnumerableHashSetView<TKey> view;
+ view.start = dict.begin();
+ view.last = dict.end();
+ return Queryable<EnumerableHashSetView<TKey>, typename HashSetBase<TKey, EnumerableDictionary<TKey, _DummyClass>>::Iterator, TKey>(view);
}
}
diff --git a/source/core/list.h b/source/core/list.h
index 6ff68ed7d..5315f788f 100644
--- a/source/core/list.h
+++ b/source/core/list.h
@@ -11,460 +11,458 @@
const int MIN_QSORT_SIZE = 32;
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ template<typename T, int isPOD>
+ class Initializer
{
- template<typename T, int isPOD>
- class Initializer
- {
- };
+ };
- template<typename T>
- class Initializer<T, 0>
+ template<typename T>
+ class Initializer<T, 0>
+ {
+ public:
+ static void Initialize(T * buffer, int size)
{
- public:
- static void Initialize(T * buffer, int size)
- {
- for (int i = 0; i<size; i++)
- new (buffer + i) T();
- }
- };
+ for (int i = 0; i<size; i++)
+ new (buffer + i) T();
+ }
+ };
- template<typename T, typename TAllocator>
- class AllocateMethod
+ template<typename T, typename TAllocator>
+ class AllocateMethod
+ {
+ public:
+ static inline T* Alloc(int size)
{
- public:
- static inline T* Alloc(int size)
- {
- TAllocator allocator;
- T * rs = (T*)allocator.Alloc(size*sizeof(T));
- Initializer<T, std::is_pod<T>::value>::Initialize(rs, size);
- return rs;
- }
- static inline void Free(T * ptr, int bufferSize)
- {
- TAllocator allocator;
- if (!std::is_trivially_destructible<T>::value)
- {
- for (int i = 0; i<bufferSize; i++)
- ptr[i].~T();
- }
- allocator.Free(ptr);
- }
- };
-
- template<typename T>
- class AllocateMethod<T, StandardAllocator>
+ TAllocator allocator;
+ T * rs = (T*)allocator.Alloc(size*sizeof(T));
+ Initializer<T, std::is_pod<T>::value>::Initialize(rs, size);
+ return rs;
+ }
+ static inline void Free(T * ptr, int bufferSize)
{
- public:
- static inline T* Alloc(int size)
- {
- return new T[size];
- }
- static inline void Free(T* ptr, int /*bufferSize*/)
+ TAllocator allocator;
+ if (!std::is_trivially_destructible<T>::value)
{
- delete [] ptr;
+ for (int i = 0; i<bufferSize; i++)
+ ptr[i].~T();
}
- };
+ allocator.Free(ptr);
+ }
+ };
- template<typename T>
- class Initializer<T, 1>
+ template<typename T>
+ class AllocateMethod<T, StandardAllocator>
+ {
+ public:
+ static inline T* Alloc(int size)
{
- public:
- static void Initialize(T * buffer, int size)
- {
- for (int i = 0; i<size; i++)
- new (buffer + i) T;
- }
- };
+ return new T[size];
+ }
+ static inline void Free(T* ptr, int /*bufferSize*/)
+ {
+ delete [] ptr;
+ }
+ };
- template<typename T, typename TAllocator = StandardAllocator>
- class List
+ template<typename T>
+ class Initializer<T, 1>
+ {
+ public:
+ static void Initialize(T * buffer, int size)
{
- private:
+ for (int i = 0; i<size; i++)
+ new (buffer + i) T;
+ }
+ };
- inline T * Allocate(int size)
- {
- return AllocateMethod<T, TAllocator>::Alloc(size);
+ template<typename T, typename TAllocator = StandardAllocator>
+ class List
+ {
+ private:
+
+ inline T * Allocate(int size)
+ {
+ return AllocateMethod<T, TAllocator>::Alloc(size);
- }
- private:
- static const int InitialSize = 16;
- TAllocator allocator;
- private:
- T * buffer;
- int _count;
- int bufferSize;
- void FreeBuffer()
- {
- AllocateMethod<T, TAllocator>::Free(buffer, bufferSize);
- buffer = 0;
- }
- void Free()
- {
- if (buffer)
- {
- FreeBuffer();
- }
- buffer = 0;
- _count = bufferSize = 0;
- }
- public:
- T* begin() const
- {
- return buffer;
- }
- T* end() const
- {
- return buffer+_count;
- }
- private:
- template<typename... Args>
- void Init(const T & val, Args... args)
- {
- Add(val);
- Init(args...);
- }
- public:
- List()
- : buffer(0), _count(0), bufferSize(0)
- {
- }
- template<typename... Args>
- List(const T & val, Args... args)
- {
- Init(val, args...);
- }
- List(const List<T> & list)
- : buffer(0), _count(0), bufferSize(0)
- {
- this->operator=(list);
- }
- List(List<T> && list)
- : buffer(0), _count(0), bufferSize(0)
- {
- this->operator=(static_cast<List<T>&&>(list));
- }
- static List<T> Create(const T & val, int count)
- {
- List<T> rs;
- rs.SetSize(count);
- for (int i = 0; i < count; i++)
- rs[i] = val;
- return rs;
- }
- ~List()
+ }
+ private:
+ static const int InitialSize = 16;
+ TAllocator allocator;
+ private:
+ T * buffer;
+ int _count;
+ int bufferSize;
+ void FreeBuffer()
+ {
+ AllocateMethod<T, TAllocator>::Free(buffer, bufferSize);
+ buffer = 0;
+ }
+ void Free()
+ {
+ if (buffer)
{
- Free();
+ FreeBuffer();
}
- List<T> & operator=(const List<T> & list)
- {
- Free();
- AddRange(list);
+ buffer = 0;
+ _count = bufferSize = 0;
+ }
+ public:
+ T* begin() const
+ {
+ return buffer;
+ }
+ T* end() const
+ {
+ return buffer+_count;
+ }
+ private:
+ template<typename... Args>
+ void Init(const T & val, Args... args)
+ {
+ Add(val);
+ Init(args...);
+ }
+ public:
+ List()
+ : buffer(0), _count(0), bufferSize(0)
+ {
+ }
+ template<typename... Args>
+ List(const T & val, Args... args)
+ {
+ Init(val, args...);
+ }
+ List(const List<T> & list)
+ : buffer(0), _count(0), bufferSize(0)
+ {
+ this->operator=(list);
+ }
+ List(List<T> && list)
+ : buffer(0), _count(0), bufferSize(0)
+ {
+ this->operator=(static_cast<List<T>&&>(list));
+ }
+ static List<T> Create(const T & val, int count)
+ {
+ List<T> rs;
+ rs.SetSize(count);
+ for (int i = 0; i < count; i++)
+ rs[i] = val;
+ return rs;
+ }
+ ~List()
+ {
+ Free();
+ }
+ List<T> & operator=(const List<T> & list)
+ {
+ Free();
+ AddRange(list);
- return *this;
- }
+ return *this;
+ }
- List<T> & operator=(List<T> && list)
- {
- Free();
- _count = list._count;
- bufferSize = list.bufferSize;
- buffer = list.buffer;
-
- list.buffer = 0;
- list._count = 0;
- list.bufferSize = 0;
- return *this;
- }
+ List<T> & operator=(List<T> && list)
+ {
+ Free();
+ _count = list._count;
+ bufferSize = list.bufferSize;
+ buffer = list.buffer;
+
+ list.buffer = 0;
+ list._count = 0;
+ list.bufferSize = 0;
+ return *this;
+ }
- T & First() const
- {
+ T & First() const
+ {
#ifdef _DEBUG
- if (_count == 0)
- throw "Index out of range.";
+ if (_count == 0)
+ throw "Index out of range.";
#endif
- return buffer[0];
- }
+ return buffer[0];
+ }
- T & Last() const
- {
+ T & Last() const
+ {
#ifdef _DEBUG
- if (_count == 0)
- throw "Index out of range.";
+ if (_count == 0)
+ throw "Index out of range.";
#endif
- return buffer[_count-1];
- }
+ return buffer[_count-1];
+ }
- inline void SwapWith(List<T, TAllocator> & other)
- {
- T* tmpBuffer = this->buffer;
- this->buffer = other.buffer;
- other.buffer = tmpBuffer;
- int tmpBufferSize = this->bufferSize;
- this->bufferSize = other.bufferSize;
- other.bufferSize = tmpBufferSize;
- int tmpCount = this->_count;
- this->_count = other._count;
- other._count = tmpCount;
- TAllocator tmpAlloc = _Move(this->allocator);
- this->allocator = _Move(other.allocator);
- other.allocator = _Move(tmpAlloc);
- }
+ inline void SwapWith(List<T, TAllocator> & other)
+ {
+ T* tmpBuffer = this->buffer;
+ this->buffer = other.buffer;
+ other.buffer = tmpBuffer;
+ int tmpBufferSize = this->bufferSize;
+ this->bufferSize = other.bufferSize;
+ other.bufferSize = tmpBufferSize;
+ int tmpCount = this->_count;
+ this->_count = other._count;
+ other._count = tmpCount;
+ TAllocator tmpAlloc = _Move(this->allocator);
+ this->allocator = _Move(other.allocator);
+ other.allocator = _Move(tmpAlloc);
+ }
- T* ReleaseBuffer()
- {
- T* rs = buffer;
- buffer = nullptr;
- _count = 0;
- bufferSize = 0;
- return rs;
- }
+ T* ReleaseBuffer()
+ {
+ T* rs = buffer;
+ buffer = nullptr;
+ _count = 0;
+ bufferSize = 0;
+ return rs;
+ }
- inline ArrayView<T> GetArrayView() const
- {
- return ArrayView<T>(buffer, _count);
- }
+ inline ArrayView<T> GetArrayView() const
+ {
+ return ArrayView<T>(buffer, _count);
+ }
- inline ArrayView<T> GetArrayView(int start, int count) const
- {
+ inline ArrayView<T> GetArrayView(int start, int count) const
+ {
#ifdef _DEBUG
- if (start + count > _count || start < 0 || count < 0)
- throw "Index out of range.";
+ if (start + count > _count || start < 0 || count < 0)
+ throw "Index out of range.";
#endif
- return ArrayView<T>(buffer + start, count);
- }
+ return ArrayView<T>(buffer + start, count);
+ }
- void Add(T && obj)
+ void Add(T && obj)
+ {
+ if (bufferSize < _count + 1)
{
- if (bufferSize < _count + 1)
- {
- int newBufferSize = InitialSize;
- if (bufferSize)
- newBufferSize = (bufferSize << 1);
+ int newBufferSize = InitialSize;
+ if (bufferSize)
+ newBufferSize = (bufferSize << 1);
- Reserve(newBufferSize);
- }
- buffer[_count++] = static_cast<T&&>(obj);
+ Reserve(newBufferSize);
}
+ buffer[_count++] = static_cast<T&&>(obj);
+ }
- void Add(const T & obj)
+ void Add(const T & obj)
+ {
+ if (bufferSize < _count + 1)
{
- if (bufferSize < _count + 1)
- {
- int newBufferSize = InitialSize;
- if (bufferSize)
- newBufferSize = (bufferSize << 1);
-
- Reserve(newBufferSize);
- }
- buffer[_count++] = obj;
+ int newBufferSize = InitialSize;
+ if (bufferSize)
+ newBufferSize = (bufferSize << 1);
+ Reserve(newBufferSize);
}
+ buffer[_count++] = obj;
- int Count() const
- {
- return _count;
- }
+ }
- T * Buffer() const
- {
- return buffer;
- }
+ int Count() const
+ {
+ return _count;
+ }
- int Capacity() const
- {
- return bufferSize;
- }
+ T * Buffer() const
+ {
+ return buffer;
+ }
- void Insert(int id, const T & val)
- {
- InsertRange(id, &val, 1);
- }
+ int Capacity() const
+ {
+ return bufferSize;
+ }
- void InsertRange(int id, const T * vals, int n)
+ void Insert(int id, const T & val)
+ {
+ InsertRange(id, &val, 1);
+ }
+
+ void InsertRange(int id, const T * vals, int n)
+ {
+ if (bufferSize < _count + n)
{
- if (bufferSize < _count + n)
- {
- int newBufferSize = InitialSize;
- while (newBufferSize < _count + n)
- newBufferSize = newBufferSize << 1;
+ int newBufferSize = InitialSize;
+ while (newBufferSize < _count + n)
+ newBufferSize = newBufferSize << 1;
- T * newBuffer = Allocate(newBufferSize);
- if (bufferSize)
- {
- /*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
- {
- memcpy(newBuffer, buffer, sizeof(T) * id);
- memcpy(newBuffer + id + n, buffer + id, sizeof(T) * (_count - id));
- }
- else*/
- {
- for (int i = 0; i < id; i++)
- newBuffer[i] = buffer[i];
- for (int i = id; i < _count; i++)
- newBuffer[i + n] = T(static_cast<T&&>(buffer[i]));
- }
- FreeBuffer();
- }
- buffer = newBuffer;
- bufferSize = newBufferSize;
- }
- else
+ T * newBuffer = Allocate(newBufferSize);
+ if (bufferSize)
{
/*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
- memmove(buffer + id + n, buffer + id, sizeof(T) * (_count - id));
+ {
+ memcpy(newBuffer, buffer, sizeof(T) * id);
+ memcpy(newBuffer + id + n, buffer + id, sizeof(T) * (_count - id));
+ }
else*/
{
- for (int i = _count - 1; i >= id; i--)
- buffer[i + n] = static_cast<T&&>(buffer[i]);
+ for (int i = 0; i < id; i++)
+ newBuffer[i] = buffer[i];
+ for (int i = id; i < _count; i++)
+ newBuffer[i + n] = T(static_cast<T&&>(buffer[i]));
}
+ FreeBuffer();
}
+ buffer = newBuffer;
+ bufferSize = newBufferSize;
+ }
+ else
+ {
/*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
- memcpy(buffer + id, vals, sizeof(T) * n);
+ memmove(buffer + id + n, buffer + id, sizeof(T) * (_count - id));
else*/
- for (int i = 0; i < n; i++)
- buffer[id + i] = vals[i];
-
- _count += n;
+ {
+ for (int i = _count - 1; i >= id; i--)
+ buffer[i + n] = static_cast<T&&>(buffer[i]);
+ }
}
+ /*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
+ memcpy(buffer + id, vals, sizeof(T) * n);
+ else*/
+ for (int i = 0; i < n; i++)
+ buffer[id + i] = vals[i];
- //slower than original edition
- //void Add(const T & val)
- //{
- // InsertRange(_count, &val, 1);
- //}
+ _count += n;
+ }
- void InsertRange(int id, const List<T> & list)
- {
- InsertRange(id, list.buffer, list._count);
- }
+ //slower than original edition
+ //void Add(const T & val)
+ //{
+ // InsertRange(_count, &val, 1);
+ //}
- void AddRange(ArrayView<T> list)
- {
- InsertRange(_count, list.Buffer(), list.Count());
- }
+ void InsertRange(int id, const List<T> & list)
+ {
+ InsertRange(id, list.buffer, list._count);
+ }
- void AddRange(const T * vals, int n)
- {
- InsertRange(_count, vals, n);
- }
+ void AddRange(ArrayView<T> list)
+ {
+ InsertRange(_count, list.Buffer(), list.Count());
+ }
- void AddRange(const List<T> & list)
- {
- InsertRange(_count, list.buffer, list._count);
- }
+ void AddRange(const T * vals, int n)
+ {
+ InsertRange(_count, vals, n);
+ }
- void RemoveRange(int id, int deleteCount)
- {
+ void AddRange(const List<T> & list)
+ {
+ InsertRange(_count, list.buffer, list._count);
+ }
+
+ void RemoveRange(int id, int deleteCount)
+ {
#if _DEBUG
- if (id >= _count || id < 0)
- throw "Remove: Index out of range.";
- if(deleteCount < 0)
- throw "Remove: deleteCount smaller than zero.";
+ if (id >= _count || id < 0)
+ throw "Remove: Index out of range.";
+ if(deleteCount < 0)
+ throw "Remove: deleteCount smaller than zero.";
#endif
- int actualDeleteCount = ((id + deleteCount) >= _count)? (_count - id) : deleteCount;
- for (int i = id + actualDeleteCount; i < _count; i++)
- buffer[i - actualDeleteCount] = static_cast<T&&>(buffer[i]);
- _count -= actualDeleteCount;
- }
+ int actualDeleteCount = ((id + deleteCount) >= _count)? (_count - id) : deleteCount;
+ for (int i = id + actualDeleteCount; i < _count; i++)
+ buffer[i - actualDeleteCount] = static_cast<T&&>(buffer[i]);
+ _count -= actualDeleteCount;
+ }
- void RemoveAt(int id)
- {
- RemoveRange(id, 1);
- }
+ void RemoveAt(int id)
+ {
+ RemoveRange(id, 1);
+ }
- void Remove(const T & val)
- {
- int idx = IndexOf(val);
- if (idx != -1)
- RemoveAt(idx);
- }
+ void Remove(const T & val)
+ {
+ int idx = IndexOf(val);
+ if (idx != -1)
+ RemoveAt(idx);
+ }
- void Reverse()
+ void Reverse()
+ {
+ for (int i = 0; i < (_count >> 1); i++)
{
- for (int i = 0; i < (_count >> 1); i++)
- {
- Swap(buffer, i, _count - i - 1);
- }
+ Swap(buffer, i, _count - i - 1);
}
+ }
- void FastRemove(const T & val)
- {
- int idx = IndexOf(val);
- FastRemoveAt(idx);
- }
+ void FastRemove(const T & val)
+ {
+ int idx = IndexOf(val);
+ FastRemoveAt(idx);
+ }
- void FastRemoveAt(int idx)
+ void FastRemoveAt(int idx)
+ {
+ if (idx != -1 && _count - 1 != idx)
{
- if (idx != -1 && _count - 1 != idx)
- {
- buffer[idx] = _Move(buffer[_count - 1]);
- }
- _count--;
+ buffer[idx] = _Move(buffer[_count - 1]);
}
+ _count--;
+ }
- void Clear()
- {
- _count = 0;
- }
+ void Clear()
+ {
+ _count = 0;
+ }
- void Reserve(int size)
+ void Reserve(int size)
+ {
+ if(size > bufferSize)
{
- if(size > bufferSize)
+ T * newBuffer = Allocate(size);
+ if (bufferSize)
{
- T * newBuffer = Allocate(size);
- if (bufferSize)
+ /*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
+ memcpy(newBuffer, buffer, _count * sizeof(T));
+ else*/
{
- /*if (std::has_trivial_copy_assign<T>::value && std::has_trivial_destructor<T>::value)
- memcpy(newBuffer, buffer, _count * sizeof(T));
- else*/
- {
- for (int i = 0; i < _count; i++)
- newBuffer[i] = static_cast<T&&>(buffer[i]);
- }
- FreeBuffer();
+ for (int i = 0; i < _count; i++)
+ newBuffer[i] = static_cast<T&&>(buffer[i]);
}
- buffer = newBuffer;
- bufferSize = size;
+ FreeBuffer();
}
+ buffer = newBuffer;
+ bufferSize = size;
}
+ }
- void GrowToSize(int size)
+ void GrowToSize(int size)
+ {
+ int newBufferSize = 1<<Math::Log2Ceil(size);
+ if (bufferSize < newBufferSize)
{
- int newBufferSize = 1<<Math::Log2Ceil(size);
- if (bufferSize < newBufferSize)
- {
- Reserve(newBufferSize);
- }
- this->_count = size;
+ Reserve(newBufferSize);
}
+ this->_count = size;
+ }
- void SetSize(int size)
- {
- Reserve(size);
- _count = size;
- }
+ void SetSize(int size)
+ {
+ Reserve(size);
+ _count = size;
+ }
- void UnsafeShrinkToSize(int size)
- {
- _count = size;
- }
+ void UnsafeShrinkToSize(int size)
+ {
+ _count = size;
+ }
- void Compress()
+ void Compress()
+ {
+ if (bufferSize > _count && _count > 0)
{
- if (bufferSize > _count && _count > 0)
- {
- T * newBuffer = Allocate(_count);
- for (int i = 0; i < _count; i++)
- newBuffer[i] = static_cast<T&&>(buffer[i]);
- FreeBuffer();
- buffer = newBuffer;
- bufferSize = _count;
- }
+ T * newBuffer = Allocate(_count);
+ for (int i = 0; i < _count; i++)
+ newBuffer[i] = static_cast<T&&>(buffer[i]);
+ FreeBuffer();
+ buffer = newBuffer;
+ bufferSize = _count;
}
+ }
#ifndef FORCE_INLINE
#ifdef _MSC_VER
@@ -474,200 +472,199 @@ namespace CoreLib
#endif
#endif
- FORCE_INLINE T & operator [](int id) const
- {
+ FORCE_INLINE T & operator [](int id) const
+ {
#if _DEBUG
- if(id >= _count || id < 0)
- throw IndexOutofRangeException("Operator[]: Index out of Range.");
+ if(id >= _count || id < 0)
+ throw IndexOutofRangeException("Operator[]: Index out of Range.");
#endif
- return buffer[id];
- }
+ return buffer[id];
+ }
- template<typename Func>
- int FindFirst(const Func & predicate) const
+ template<typename Func>
+ int FindFirst(const Func & predicate) const
+ {
+ for (int i = 0; i < _count; i++)
{
- for (int i = 0; i < _count; i++)
- {
- if (predicate(buffer[i]))
- return i;
- }
- return -1;
+ if (predicate(buffer[i]))
+ return i;
}
+ return -1;
+ }
- template<typename Func>
- int FindLast(const Func & predicate) const
+ template<typename Func>
+ int FindLast(const Func & predicate) const
+ {
+ for (int i = _count - 1; i >= 0; i--)
{
- for (int i = _count - 1; i >= 0; i--)
- {
- if (predicate(buffer[i]))
- return i;
- }
- return -1;
+ if (predicate(buffer[i]))
+ return i;
}
+ return -1;
+ }
- template<typename T2>
- int IndexOf(const T2 & val) const
+ template<typename T2>
+ int IndexOf(const T2 & val) const
+ {
+ for (int i = 0; i < _count; i++)
{
- for (int i = 0; i < _count; i++)
- {
- if (buffer[i] == val)
- return i;
- }
- return -1;
- }
-
- template<typename T2>
- int LastIndexOf(const T2 & val) const
- {
- for (int i = _count - 1; i >= 0; i--)
- {
- if(buffer[i] == val)
- return i;
- }
- return -1;
+ if (buffer[i] == val)
+ return i;
}
+ return -1;
+ }
- void Sort()
+ template<typename T2>
+ int LastIndexOf(const T2 & val) const
+ {
+ for (int i = _count - 1; i >= 0; i--)
{
- Sort([](T& t1, T& t2){return t1<t2;});
+ if(buffer[i] == val)
+ return i;
}
+ return -1;
+ }
- bool Contains(const T & val)
- {
- for (int i = 0; i<_count; i++)
- if (buffer[i] == val)
- return true;
- return false;
- }
+ void Sort()
+ {
+ Sort([](T& t1, T& t2){return t1<t2;});
+ }
- template<typename Comparer>
- void Sort(Comparer compare)
- {
- //InsertionSort(buffer, 0, _count - 1);
- //QuickSort(buffer, 0, _count - 1, compare);
- std::sort(buffer, buffer + _count, compare);
- }
+ bool Contains(const T & val)
+ {
+ for (int i = 0; i<_count; i++)
+ if (buffer[i] == val)
+ return true;
+ return false;
+ }
- template <typename IterateFunc>
- void ForEach(IterateFunc f) const
- {
- for (int i = 0; i<_count; i++)
- f(buffer[i]);
- }
+ template<typename Comparer>
+ void Sort(Comparer compare)
+ {
+ //InsertionSort(buffer, 0, _count - 1);
+ //QuickSort(buffer, 0, _count - 1, compare);
+ std::sort(buffer, buffer + _count, compare);
+ }
- template<typename Comparer>
- void QuickSort(T * vals, int startIndex, int endIndex, Comparer comparer)
- {
- if(startIndex < endIndex)
- {
- if (endIndex - startIndex < MIN_QSORT_SIZE)
- InsertionSort(vals, startIndex, endIndex, comparer);
- else
- {
- int pivotIndex = (startIndex + endIndex) >> 1;
- int pivotNewIndex = Partition(vals, startIndex, endIndex, pivotIndex, comparer);
- QuickSort(vals, startIndex, pivotNewIndex - 1, comparer);
- QuickSort(vals, pivotNewIndex + 1, endIndex, comparer);
- }
- }
+ template <typename IterateFunc>
+ void ForEach(IterateFunc f) const
+ {
+ for (int i = 0; i<_count; i++)
+ f(buffer[i]);
+ }
- }
- template<typename Comparer>
- int Partition(T * vals, int left, int right, int pivotIndex, Comparer comparer)
- {
- T pivotValue = vals[pivotIndex];
- Swap(vals, right, pivotIndex);
- int storeIndex = left;
- for (int i = left; i < right; i++)
- {
- if (comparer(vals[i], pivotValue))
- {
- Swap(vals, i, storeIndex);
- storeIndex++;
- }
- }
- Swap(vals, storeIndex, right);
- return storeIndex;
- }
- template<typename Comparer>
- void InsertionSort(T * vals, int startIndex, int endIndex, Comparer comparer)
+ template<typename Comparer>
+ void QuickSort(T * vals, int startIndex, int endIndex, Comparer comparer)
+ {
+ if(startIndex < endIndex)
{
- for (int i = startIndex + 1; i <= endIndex; i++)
+ if (endIndex - startIndex < MIN_QSORT_SIZE)
+ InsertionSort(vals, startIndex, endIndex, comparer);
+ else
{
- T insertValue = static_cast<T&&>(vals[i]);
- int insertIndex = i - 1;
- while (insertIndex >= startIndex && comparer(insertValue, vals[insertIndex]))
- {
- vals[insertIndex + 1] = static_cast<T&&>(vals[insertIndex]);
- insertIndex--;
- }
- vals[insertIndex + 1] = static_cast<T&&>(insertValue);
+ int pivotIndex = (startIndex + endIndex) >> 1;
+ int pivotNewIndex = Partition(vals, startIndex, endIndex, pivotIndex, comparer);
+ QuickSort(vals, startIndex, pivotNewIndex - 1, comparer);
+ QuickSort(vals, pivotNewIndex + 1, endIndex, comparer);
}
}
- inline void Swap(T * vals, int index1, int index2)
+ }
+ template<typename Comparer>
+ int Partition(T * vals, int left, int right, int pivotIndex, Comparer comparer)
+ {
+ T pivotValue = vals[pivotIndex];
+ Swap(vals, right, pivotIndex);
+ int storeIndex = left;
+ for (int i = left; i < right; i++)
{
- if (index1 != index2)
+ if (comparer(vals[i], pivotValue))
{
- T tmp = static_cast<T&&>(vals[index1]);
- vals[index1] = static_cast<T&&>(vals[index2]);
- vals[index2] = static_cast<T&&>(tmp);
+ Swap(vals, i, storeIndex);
+ storeIndex++;
}
}
-
- template<typename T2, typename Comparer>
- int BinarySearch(const T2 & obj, Comparer comparer)
+ Swap(vals, storeIndex, right);
+ return storeIndex;
+ }
+ template<typename Comparer>
+ void InsertionSort(T * vals, int startIndex, int endIndex, Comparer comparer)
+ {
+ for (int i = startIndex + 1; i <= endIndex; i++)
{
- int imin = 0, imax = _count - 1;
- while (imax >= imin)
+ T insertValue = static_cast<T&&>(vals[i]);
+ int insertIndex = i - 1;
+ while (insertIndex >= startIndex && comparer(insertValue, vals[insertIndex]))
{
- int imid = (imin + imax) >> 1;
- int compareResult = comparer(buffer[imid], obj);
- if (compareResult == 0)
- return imid;
- else if (compareResult < 0)
- imin = imid + 1;
- else
- imax = imid - 1;
+ vals[insertIndex + 1] = static_cast<T&&>(vals[insertIndex]);
+ insertIndex--;
}
- return -1;
+ vals[insertIndex + 1] = static_cast<T&&>(insertValue);
}
+ }
- template<typename T2>
- int BinarySearch(const T2 & obj)
+ inline void Swap(T * vals, int index1, int index2)
+ {
+ if (index1 != index2)
{
- return BinarySearch(obj,
- [](T & curObj, const T2 & thatObj)->int
- {
- if (curObj < thatObj)
- return -1;
- else if (curObj == thatObj)
- return 0;
- else
- return 1;
- });
+ T tmp = static_cast<T&&>(vals[index1]);
+ vals[index1] = static_cast<T&&>(vals[index2]);
+ vals[index2] = static_cast<T&&>(tmp);
}
- };
+ }
- template<typename T>
- T Min(const List<T> & list)
+ template<typename T2, typename Comparer>
+ int BinarySearch(const T2 & obj, Comparer comparer)
{
- T minVal = list.First();
- for (int i = 1; i<list.Count(); i++)
- if (list[i] < minVal)
- minVal = list[i];
- return minVal;
+ int imin = 0, imax = _count - 1;
+ while (imax >= imin)
+ {
+ int imid = (imin + imax) >> 1;
+ int compareResult = comparer(buffer[imid], obj);
+ if (compareResult == 0)
+ return imid;
+ else if (compareResult < 0)
+ imin = imid + 1;
+ else
+ imax = imid - 1;
+ }
+ return -1;
}
- template<typename T>
- T Max(const List<T> & list)
+ template<typename T2>
+ int BinarySearch(const T2 & obj)
{
- T maxVal = list.First();
- for (int i = 1; i<list.Count(); i++)
- if (list[i] > maxVal)
- maxVal = list[i];
- return maxVal;
+ return BinarySearch(obj,
+ [](T & curObj, const T2 & thatObj)->int
+ {
+ if (curObj < thatObj)
+ return -1;
+ else if (curObj == thatObj)
+ return 0;
+ else
+ return 1;
+ });
}
+ };
+
+ template<typename T>
+ T Min(const List<T> & list)
+ {
+ T minVal = list.First();
+ for (int i = 1; i<list.Count(); i++)
+ if (list[i] < minVal)
+ minVal = list[i];
+ return minVal;
+ }
+
+ template<typename T>
+ T Max(const List<T> & list)
+ {
+ T maxVal = list.First();
+ for (int i = 1; i<list.Count(); i++)
+ if (list[i] > maxVal)
+ maxVal = list[i];
+ return maxVal;
}
}
diff --git a/source/core/memory-pool.cpp b/source/core/memory-pool.cpp
index f9d29636b..aec1efc37 100644
--- a/source/core/memory-pool.cpp
+++ b/source/core/memory-pool.cpp
@@ -1,135 +1,132 @@
#include "memory-pool.h"
#include <cassert>
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ MemoryPool::MemoryPool(unsigned char * pBuffer, int pLog2BlockSize, int numBlocks)
{
- 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++)
{
- Init(pBuffer, pLog2BlockSize, numBlocks);
+ freeList[i] = nullptr;
}
- 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)
+ }
+ int MemoryPool::AllocBlock(int level)
+ {
+ if (level < 0)
+ return -1;
+ if (freeList[level] == nullptr)
{
- if (level < 0)
- return -1;
- if (freeList[level] == nullptr)
+ auto largeBlockAddr = AllocBlock(level - 1);
+ if (largeBlockAddr != -1)
{
- 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;
+ 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;
+ int blockIndex = (1 << level) + (largeBlockAddr >> (numLevels-level)) - 1;
+ used.Add(blockIndex);
+ return largeBlockAddr;
}
else
+ return -1;
+ }
+ else
+ {
+ auto node = freeList[level];
+ if (node->NextPtr)
{
- 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;
+ 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);
+ }
+ 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;
+ 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 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))
{
- 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)
{
- 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);
+ buddyPtr->PrevPtr->NextPtr = buddyPtr->NextPtr;
}
- else
+ if (buddyPtr->NextPtr)
+ {
+ buddyPtr->NextPtr->PrevPtr = buddyPtr->PrevPtr;
+ }
+ if (freeList[level] == buddyPtr)
{
- // insert to freelist
- auto freeNode = (FreeListNode *)ptr;
- freeNode->NextPtr = freeList[level];
- freeNode->PrevPtr = nullptr;
- if (freeList[level])
- freeList[level]->PrevPtr = freeNode;
- freeList[level] = freeNode;
+ freeList[level] = buddyPtr->NextPtr;
}
+ // recursively free parent blocks
+ auto parentPtr = Math::Min(buddyPtr, (FreeListNode*)ptr);
+ if (level > 0)
+ FreeBlock((unsigned char*)parentPtr, level - 1);
}
- void MemoryPool::Free(unsigned char * ptr, int size)
+ else
{
- 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);
+ // 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
index 6bfded060..e735fb11c 100644
--- a/source/core/memory-pool.h
+++ b/source/core/memory-pool.h
@@ -4,123 +4,119 @@
#include "basic.h"
#include "int-set.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ struct MemoryBlockFields
{
- 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);
- };
+ 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
- {};
+ class OutofPoolMemoryException : public Exception
+ {};
- template<typename T, int PoolSize>
- class ObjectPool
+ template<typename T, int PoolSize>
+ class ObjectPool
+ {
+ static const int ObjectSize = sizeof(T) < 8 ? 8 : sizeof(T);
+ private:
+ struct FreeList
{
- 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* Next;
+ };
+ FreeList * freeList = nullptr;
+ int allocPtr = 0;
+ int poolSize = 0;
+ void * buffer = 0;
+ T * GetFreeObject()
+ {
+ if (freeList)
{
- freeList = nullptr;
- allocPtr = 0;
- buffer = malloc(PoolSize * ObjectSize);
+ 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 Close()
+ {
+ free(buffer);
+ }
- void Free(T * obj)
- {
- auto newList = (FreeList*)obj;
- newList->Next = freeList;
- freeList = newList;
- }
+ void Free(T * obj)
+ {
+ auto newList = (FreeList*)obj;
+ newList->Next = freeList;
+ freeList = newList;
+ }
- void * Buffer()
- {
- return buffer;
- }
+ void * Buffer()
+ {
+ return buffer;
+ }
- T * Alloc()
+ T * Alloc()
+ {
+ auto rs = GetFreeObject();
+ if (!rs)
{
- auto rs = GetFreeObject();
- if (!rs)
- {
- if (allocPtr < PoolSize)
- {
- rs = (T*)buffer + allocPtr;
- allocPtr++;
- }
- }
- if (!rs)
+ if (allocPtr < PoolSize)
{
- throw OutofPoolMemoryException();
+ rs = (T*)buffer + allocPtr;
+ allocPtr++;
}
- return rs;
}
- };
+ if (!rs)
+ {
+ throw OutofPoolMemoryException();
+ }
+ return rs;
+ }
};
+};
#define USE_POOL_ALLOCATOR(T, PoolSize) \
- private:\
- static CoreLib::ObjectPool<T, PoolSize> _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();
+private:\
+ static Slang::ObjectPool<T, PoolSize> _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) \
- CoreLib::ObjectPool<T, PoolSize> T::_pool;\
- void T::ClosePool() { _pool.Close(); }
-
-}
+Slang::ObjectPool<T, PoolSize> 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 da36edbbf..5675bf24b 100644
--- a/source/core/slang-io.cpp
+++ b/source/core/slang-io.cpp
@@ -7,139 +7,135 @@
#ifdef _WIN32
#include <direct.h>
#endif
-namespace CoreLib
-{
- namespace IO
- {
- using namespace CoreLib::Basic;
- CommandLineWriter * currentCommandWriter = nullptr;
+namespace Slang
+{
+ CommandLineWriter * currentCommandWriter = nullptr;
- void SetCommandLineWriter(CommandLineWriter * writer)
- {
- currentCommandWriter = writer;
- }
+ void SetCommandLineWriter(CommandLineWriter * writer)
+ {
+ currentCommandWriter = writer;
+ }
- bool File::Exists(const String & fileName)
- {
+ bool File::Exists(const String & fileName)
+ {
#ifdef _WIN32
- struct _stat32 statVar;
- return ::_wstat32(((String)fileName).ToWString(), &statVar) != -1;
+ struct _stat32 statVar;
+ return ::_wstat32(((String)fileName).ToWString(), &statVar) != -1;
#else
- struct stat statVar;
- return ::stat(fileName.Buffer(), &statVar) == 0;
+ struct stat statVar;
+ return ::stat(fileName.Buffer(), &statVar) == 0;
#endif
- }
+ }
- String Path::TruncateExt(const String & path)
- {
- int dotPos = path.LastIndexOf('.');
- if (dotPos != -1)
- return path.SubString(0, dotPos);
- else
- return path;
- }
- String Path::ReplaceExt(const String & path, const char * newExt)
- {
- StringBuilder sb(path.Length()+10);
- int dotPos = path.LastIndexOf('.');
- if (dotPos == -1)
- dotPos = path.Length();
- sb.Append(path.Buffer(), dotPos);
- sb.Append('.');
- sb.Append(newExt);
- return sb.ProduceString();
- }
- String Path::GetFileName(const String & path)
- {
- int pos = path.LastIndexOf('/');
- pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
- return path.SubString(pos, path.Length()-pos);
- }
- String Path::GetFileNameWithoutEXT(const String & path)
- {
- int pos = path.LastIndexOf('/');
- pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
- int dotPos = path.LastIndexOf('.');
- if (dotPos <= pos)
- dotPos = path.Length();
- return path.SubString(pos, dotPos - pos);
- }
- String Path::GetFileExt(const String & path)
- {
- int dotPos = path.LastIndexOf('.');
- if (dotPos != -1)
- return path.SubString(dotPos+1, path.Length()-dotPos-1);
- else
- return "";
- }
- String Path::GetDirectoryName(const String & path)
- {
- int pos = path.LastIndexOf('/');
- pos = Math::Max(path.LastIndexOf('\\'), pos);
- if (pos != -1)
- return path.SubString(0, pos);
- else
- return "";
- }
- String Path::Combine(const String & path1, const String & path2)
- {
- if (path1.Length() == 0) return path2;
- StringBuilder sb(path1.Length()+path2.Length()+2);
- sb.Append(path1);
- if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
- sb.Append(PathDelimiter);
- sb.Append(path2);
- return sb.ProduceString();
- }
- String Path::Combine(const String & path1, const String & path2, const String & path3)
- {
- StringBuilder sb(path1.Length()+path2.Length()+path3.Length()+3);
- sb.Append(path1);
- if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
- sb.Append(PathDelimiter);
- sb.Append(path2);
- if (!path2.EndsWith('\\') && !path2.EndsWith('/'))
- sb.Append(PathDelimiter);
- sb.Append(path3);
- return sb.ProduceString();
- }
+ String Path::TruncateExt(const String & path)
+ {
+ int dotPos = path.LastIndexOf('.');
+ if (dotPos != -1)
+ return path.SubString(0, dotPos);
+ else
+ return path;
+ }
+ String Path::ReplaceExt(const String & path, const char * newExt)
+ {
+ StringBuilder sb(path.Length()+10);
+ int dotPos = path.LastIndexOf('.');
+ if (dotPos == -1)
+ dotPos = path.Length();
+ sb.Append(path.Buffer(), dotPos);
+ sb.Append('.');
+ sb.Append(newExt);
+ return sb.ProduceString();
+ }
+ String Path::GetFileName(const String & path)
+ {
+ int pos = path.LastIndexOf('/');
+ pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
+ return path.SubString(pos, path.Length()-pos);
+ }
+ String Path::GetFileNameWithoutEXT(const String & path)
+ {
+ int pos = path.LastIndexOf('/');
+ pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
+ int dotPos = path.LastIndexOf('.');
+ if (dotPos <= pos)
+ dotPos = path.Length();
+ return path.SubString(pos, dotPos - pos);
+ }
+ String Path::GetFileExt(const String & path)
+ {
+ int dotPos = path.LastIndexOf('.');
+ if (dotPos != -1)
+ return path.SubString(dotPos+1, path.Length()-dotPos-1);
+ else
+ return "";
+ }
+ String Path::GetDirectoryName(const String & path)
+ {
+ int pos = path.LastIndexOf('/');
+ pos = Math::Max(path.LastIndexOf('\\'), pos);
+ if (pos != -1)
+ return path.SubString(0, pos);
+ else
+ return "";
+ }
+ String Path::Combine(const String & path1, const String & path2)
+ {
+ if (path1.Length() == 0) return path2;
+ StringBuilder sb(path1.Length()+path2.Length()+2);
+ sb.Append(path1);
+ if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
+ sb.Append(PathDelimiter);
+ sb.Append(path2);
+ return sb.ProduceString();
+ }
+ String Path::Combine(const String & path1, const String & path2, const String & path3)
+ {
+ StringBuilder sb(path1.Length()+path2.Length()+path3.Length()+3);
+ sb.Append(path1);
+ if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
+ sb.Append(PathDelimiter);
+ sb.Append(path2);
+ if (!path2.EndsWith('\\') && !path2.EndsWith('/'))
+ sb.Append(PathDelimiter);
+ sb.Append(path3);
+ return sb.ProduceString();
+ }
- bool Path::CreateDir(const String & path)
- {
+ bool Path::CreateDir(const String & path)
+ {
#if defined(_WIN32)
- return _wmkdir(path.ToWString()) == 0;
+ return _wmkdir(path.ToWString()) == 0;
#else
- return mkdir(path.Buffer(), 0777) == 0;
+ return mkdir(path.Buffer(), 0777) == 0;
#endif
- }
+ }
- CoreLib::Basic::String File::ReadAllText(const CoreLib::Basic::String & fileName)
- {
- StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite));
- return reader.ReadToEnd();
- }
+ Slang::String File::ReadAllText(const Slang::String & fileName)
+ {
+ StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite));
+ return reader.ReadToEnd();
+ }
- CoreLib::Basic::List<unsigned char> File::ReadAllBytes(const CoreLib::Basic::String & fileName)
+ Slang::List<unsigned char> File::ReadAllBytes(const Slang::String & fileName)
+ {
+ RefPtr<FileStream> fs = new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
+ List<unsigned char> buffer;
+ while (!fs->IsEnd())
{
- RefPtr<FileStream> fs = new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
- List<unsigned char> buffer;
- while (!fs->IsEnd())
- {
- unsigned char ch;
- int read = (int)fs->Read(&ch, 1);
- if (read)
- buffer.Add(ch);
- else
- break;
- }
- return _Move(buffer);
+ unsigned char ch;
+ int read = (int)fs->Read(&ch, 1);
+ if (read)
+ buffer.Add(ch);
+ else
+ break;
}
+ return _Move(buffer);
+ }
- void File::WriteAllText(const CoreLib::Basic::String & fileName, const CoreLib::Basic::String & text)
- {
- StreamWriter writer(new FileStream(fileName, FileMode::Create));
- writer.Write(text);
- }
+ void File::WriteAllText(const Slang::String & fileName, const Slang::String & text)
+ {
+ StreamWriter writer(new FileStream(fileName, FileMode::Create));
+ writer.Write(text);
}
-} \ No newline at end of file
+}
diff --git a/source/core/slang-io.h b/source/core/slang-io.h
index 0471cd965..fb9583c67 100644
--- a/source/core/slang-io.h
+++ b/source/core/slang-io.h
@@ -6,56 +6,53 @@
#include "text-io.h"
#include "secure-crt.h"
-namespace CoreLib
+namespace Slang
{
- namespace IO
+ class File
{
- class File
- {
- public:
- static bool Exists(const CoreLib::Basic::String & fileName);
- static CoreLib::Basic::String ReadAllText(const CoreLib::Basic::String & fileName);
- static CoreLib::Basic::List<unsigned char> ReadAllBytes(const CoreLib::Basic::String & fileName);
- static void WriteAllText(const CoreLib::Basic::String & fileName, const CoreLib::Basic::String & text);
- };
+ public:
+ static bool Exists(const Slang::String & fileName);
+ static Slang::String ReadAllText(const Slang::String & fileName);
+ static Slang::List<unsigned char> ReadAllBytes(const Slang::String & fileName);
+ static void WriteAllText(const Slang::String & fileName, const Slang::String & text);
+ };
- class Path
- {
- public:
+ class Path
+ {
+ public:
#ifdef _WIN32
- static const char PathDelimiter = '\\';
+ static const char PathDelimiter = '\\';
#else
- static const char PathDelimiter = '/';
+ static const char PathDelimiter = '/';
#endif
- static String TruncateExt(const String & path);
- static String ReplaceExt(const String & path, const char * newExt);
- static String GetFileName(const String & path);
- static String GetFileNameWithoutEXT(const String & path);
- static String GetFileExt(const String & path);
- static String GetDirectoryName(const String & path);
- static String Combine(const String & path1, const String & path2);
- static String Combine(const String & path1, const String & path2, const String & path3);
- static bool CreateDir(const String & path);
- };
+ static String TruncateExt(const String & path);
+ static String ReplaceExt(const String & path, const char * newExt);
+ static String GetFileName(const String & path);
+ static String GetFileNameWithoutEXT(const String & path);
+ static String GetFileExt(const String & path);
+ static String GetDirectoryName(const String & path);
+ static String Combine(const String & path1, const String & path2);
+ 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;
- };
+ class CommandLineWriter : public Object
+ {
+ public:
+ virtual void Write(const String & text) = 0;
+ };
- void SetCommandLineWriter(CommandLineWriter * writer);
+ void SetCommandLineWriter(CommandLineWriter * writer);
- extern CommandLineWriter * currentCommandWriter;
- template<typename ...Args>
- void uiprintf(const wchar_t * format, Args... args)
+ extern CommandLineWriter * currentCommandWriter;
+ template<typename ...Args>
+ void uiprintf(const wchar_t * format, Args... args)
+ {
+ if (currentCommandWriter)
{
- if (currentCommandWriter)
- {
- char buffer[1024];
- snprintf(buffer, 1024, format, args...);
- currentCommandWriter->Write(buffer);
- }
+ char buffer[1024];
+ snprintf(buffer, 1024, format, args...);
+ currentCommandWriter->Write(buffer);
}
}
}
diff --git a/source/core/slang-math.cpp b/source/core/slang-math.cpp
index a57b0dac2..76be23f30 100644
--- a/source/core/slang-math.cpp
+++ b/source/core/slang-math.cpp
@@ -1,9 +1,8 @@
#include "slang-math.h"
-namespace CoreLib
+#if 0
+namespace Slang
{
- namespace Basic
- {
- const float Math::Pi = 3.141592654f;
- }
-} \ No newline at end of file
+ const float Math::Pi = 3.141592654f;
+}
+#endif
diff --git a/source/core/slang-math.h b/source/core/slang-math.h
index 454dfbfdf..e57cc68ff 100644
--- a/source/core/slang-math.h
+++ b/source/core/slang-math.h
@@ -3,218 +3,215 @@
#include <math.h>
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ class Math
{
- class Math
+ public:
+ static const float Pi;
+ template<typename T>
+ static T Min(const T& v1, const T&v2)
{
- public:
- static const float Pi;
- template<typename T>
- static T Min(const T& v1, const T&v2)
- {
- return v1<v2?v1:v2;
- }
- template<typename T>
- static T Max(const T& v1, const T&v2)
- {
- return v1>v2?v1:v2;
- }
- template<typename T>
- static T Min(const T& v1, const T&v2, const T&v3)
- {
- return Min(v1, Min(v2, v3));
- }
- template<typename T>
- static T Max(const T& v1, const T&v2, const T&v3)
- {
- return Max(v1, Max(v2, v3));
- }
- template<typename T>
- static T Clamp(const T& val, const T& vmin, const T&vmax)
- {
- if (val < vmin) return vmin;
- else if (val > vmax) return vmax;
- else return val;
- }
+ return v1<v2?v1:v2;
+ }
+ template<typename T>
+ static T Max(const T& v1, const T&v2)
+ {
+ return v1>v2?v1:v2;
+ }
+ template<typename T>
+ static T Min(const T& v1, const T&v2, const T&v3)
+ {
+ return Min(v1, Min(v2, v3));
+ }
+ template<typename T>
+ static T Max(const T& v1, const T&v2, const T&v3)
+ {
+ return Max(v1, Max(v2, v3));
+ }
+ template<typename T>
+ static T Clamp(const T& val, const T& vmin, const T&vmax)
+ {
+ if (val < vmin) return vmin;
+ else if (val > vmax) return vmax;
+ else return val;
+ }
- static inline int FastFloor(float x)
- {
- int i = (int)x;
- return i - (i > x);
- }
+ static inline int FastFloor(float x)
+ {
+ int i = (int)x;
+ return i - (i > x);
+ }
- static inline int FastFloor(double x)
- {
- int i = (int)x;
- return i - (i > x);
- }
+ static inline int FastFloor(double x)
+ {
+ int i = (int)x;
+ return i - (i > x);
+ }
- static inline int IsNaN(float x)
- {
+ static inline int IsNaN(float x)
+ {
#ifdef _M_X64
- return _isnanf(x);
+ return _isnanf(x);
#else
- return isnan(x);
+ return isnan(x);
#endif
- }
-
- static inline int IsInf(float x)
- {
- return isinf(x);
- }
+ }
- static inline unsigned int Ones32(register unsigned int x)
- {
- /* 32-bit recursive reduction using SWAR...
- but first step is mapping 2-bit values
- into sum of 2 1-bit values in sneaky way
- */
- x -= ((x >> 1) & 0x55555555);
- x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
- x = (((x >> 4) + x) & 0x0f0f0f0f);
- x += (x >> 8);
- x += (x >> 16);
- return(x & 0x0000003f);
- }
+ static inline int IsInf(float x)
+ {
+ return isinf(x);
+ }
- static inline unsigned int Log2Floor(register unsigned int x)
- {
- x |= (x >> 1);
- x |= (x >> 2);
- x |= (x >> 4);
- x |= (x >> 8);
- x |= (x >> 16);
- return(Ones32(x >> 1));
- }
+ static inline unsigned int Ones32(register unsigned int x)
+ {
+ /* 32-bit recursive reduction using SWAR...
+ but first step is mapping 2-bit values
+ into sum of 2 1-bit values in sneaky way
+ */
+ x -= ((x >> 1) & 0x55555555);
+ x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
+ x = (((x >> 4) + x) & 0x0f0f0f0f);
+ x += (x >> 8);
+ x += (x >> 16);
+ return(x & 0x0000003f);
+ }
- static inline unsigned int Log2Ceil(register unsigned int x)
- {
- int y = (x & (x - 1));
- y |= -y;
- y >>= (32 - 1);
- x |= (x >> 1);
- x |= (x >> 2);
- x |= (x >> 4);
- x |= (x >> 8);
- x |= (x >> 16);
- return(Ones32(x >> 1) - y);
- }
- /*
- static inline int Log2(float x)
- {
- unsigned int ix = (unsigned int&)x;
- unsigned int exp = (ix >> 23) & 0xFF;
- int log2 = (unsigned int)(exp) - 127;
+ static inline unsigned int Log2Floor(register unsigned int x)
+ {
+ x |= (x >> 1);
+ x |= (x >> 2);
+ x |= (x >> 4);
+ x |= (x >> 8);
+ x |= (x >> 16);
+ return(Ones32(x >> 1));
+ }
- return log2;
- }
- */
- };
- inline int FloatAsInt(float val)
+ static inline unsigned int Log2Ceil(register unsigned int x)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- } cast;
- cast.fvalue = val;
- return cast.ivalue;
+ int y = (x & (x - 1));
+ y |= -y;
+ y >>= (32 - 1);
+ x |= (x >> 1);
+ x |= (x >> 2);
+ x |= (x >> 4);
+ x |= (x >> 8);
+ x |= (x >> 16);
+ return(Ones32(x >> 1) - y);
}
- inline float IntAsFloat(int val)
+ /*
+ static inline int Log2(float x)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- } cast;
- cast.ivalue = val;
- return cast.fvalue;
+ unsigned int ix = (unsigned int&)x;
+ unsigned int exp = (ix >> 23) & 0xFF;
+ int log2 = (unsigned int)(exp) - 127;
+
+ return log2;
}
+ */
+ };
+ inline int FloatAsInt(float val)
+ {
+ union InterCast
+ {
+ float fvalue;
+ int ivalue;
+ } cast;
+ cast.fvalue = val;
+ return cast.ivalue;
+ }
+ inline float IntAsFloat(int val)
+ {
+ union InterCast
+ {
+ float fvalue;
+ int ivalue;
+ } cast;
+ cast.ivalue = val;
+ return cast.fvalue;
+ }
- inline unsigned short FloatToHalf(float val)
- {
- int x = *(int*)&val;
- unsigned short bits = (x >> 16) & 0x8000;
- unsigned short m = (x >> 12) & 0x07ff;
- unsigned int e = (x >> 23) & 0xff;
- if (e < 103)
- return bits;
- if (e > 142)
- {
- bits |= 0x7c00u;
- bits |= e == 255 && (x & 0x007fffffu);
- return bits;
- }
- if (e < 113)
- {
- m |= 0x0800u;
- bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
- return bits;
- }
- bits |= ((e - 112) << 10) | (m >> 1);
- bits += m & 1;
+ inline unsigned short FloatToHalf(float val)
+ {
+ int x = *(int*)&val;
+ unsigned short bits = (x >> 16) & 0x8000;
+ unsigned short m = (x >> 12) & 0x07ff;
+ unsigned int e = (x >> 23) & 0xff;
+ if (e < 103)
+ return bits;
+ if (e > 142)
+ {
+ bits |= 0x7c00u;
+ bits |= e == 255 && (x & 0x007fffffu);
return bits;
}
-
- inline float HalfToFloat(unsigned short input)
+ if (e < 113)
{
- union InterCast
- {
- float fvalue;
- int ivalue;
- InterCast() = default;
- InterCast(int ival)
- {
- ivalue = ival;
- }
- };
- static const InterCast magic = InterCast((127 + (127 - 15)) << 23);
- static const InterCast was_infnan = InterCast((127 + 16) << 23);
- InterCast o;
- o.ivalue = (input & 0x7fff) << 13; // exponent/mantissa bits
- o.fvalue *= magic.fvalue; // exponent adjust
- if (o.fvalue >= was_infnan.fvalue) // make sure Inf/NaN survive
- o.ivalue |= 255 << 23;
- o.ivalue |= (input & 0x8000) << 16; // sign bit
- return o.fvalue;
+ m |= 0x0800u;
+ bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
+ return bits;
}
+ bits |= ((e - 112) << 10) | (m >> 1);
+ bits += m & 1;
+ return bits;
+ }
- class Random
+ inline float HalfToFloat(unsigned short input)
+ {
+ union InterCast
{
- private:
- unsigned int seed;
- public:
- Random(int seed)
+ float fvalue;
+ int ivalue;
+ InterCast() = default;
+ InterCast(int ival)
{
- this->seed = seed;
- }
- int Next() // random between 0 and RandMax (currently 0x7fff)
- {
- return ((seed = ((seed << 12) + 150889L) % 714025) & 0x7fff);
- }
- int Next(int min, int max) // inclusive min, exclusive max
- {
- unsigned int a = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
- unsigned int b = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
- unsigned int r = (a << 16) + b;
- return min + r % (max - min);
- }
- float NextFloat()
- {
- return ((Next() << 15) + Next()) / ((float)(1 << 30));
- }
- float NextFloat(float valMin, float valMax)
- {
- return valMin + (valMax - valMin) * NextFloat();
- }
- static int RandMax()
- {
- return 0x7fff;
+ ivalue = ival;
}
};
+ static const InterCast magic = InterCast((127 + (127 - 15)) << 23);
+ static const InterCast was_infnan = InterCast((127 + 16) << 23);
+ InterCast o;
+ o.ivalue = (input & 0x7fff) << 13; // exponent/mantissa bits
+ o.fvalue *= magic.fvalue; // exponent adjust
+ if (o.fvalue >= was_infnan.fvalue) // make sure Inf/NaN survive
+ o.ivalue |= 255 << 23;
+ o.ivalue |= (input & 0x8000) << 16; // sign bit
+ return o.fvalue;
}
+
+ class Random
+ {
+ private:
+ unsigned int seed;
+ public:
+ Random(int seed)
+ {
+ this->seed = seed;
+ }
+ int Next() // random between 0 and RandMax (currently 0x7fff)
+ {
+ return ((seed = ((seed << 12) + 150889L) % 714025) & 0x7fff);
+ }
+ int Next(int min, int max) // inclusive min, exclusive max
+ {
+ unsigned int a = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
+ unsigned int b = ((seed = ((seed << 12) + 150889L) % 714025) & 0xFFFF);
+ unsigned int r = (a << 16) + b;
+ return min + r % (max - min);
+ }
+ float NextFloat()
+ {
+ return ((Next() << 15) + Next()) / ((float)(1 << 30));
+ }
+ float NextFloat(float valMin, float valMax)
+ {
+ return valMin + (valMax - valMin) * NextFloat();
+ }
+ static int RandMax()
+ {
+ return 0x7fff;
+ }
+ };
}
#endif
diff --git a/source/core/slang-string.cpp b/source/core/slang-string.cpp
index 1337ceaec..b7eaadcb3 100644
--- a/source/core/slang-string.cpp
+++ b/source/core/slang-string.cpp
@@ -1,166 +1,163 @@
#include "slang-string.h"
#include "text-io.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ _EndLine EndLine;
+ String StringConcat(const char * lhs, int leftLen, const char * rhs, int rightLen)
{
- _EndLine EndLine;
- String StringConcat(const char * lhs, int leftLen, const char * rhs, int rightLen)
- {
- String res;
- res.length = leftLen + rightLen;
- res.buffer = new char[res.length + 1];
- strcpy_s(res.buffer.Ptr(), res.length + 1, lhs);
- strcpy_s(res.buffer + leftLen, res.length + 1 - leftLen, rhs);
- return res;
- }
- String operator+(const char * op1, const String & op2)
- {
- if(!op2.buffer)
- return String(op1);
+ String res;
+ res.length = leftLen + rightLen;
+ res.buffer = new char[res.length + 1];
+ strcpy_s(res.buffer.Ptr(), res.length + 1, lhs);
+ strcpy_s(res.buffer + leftLen, res.length + 1 - leftLen, rhs);
+ return res;
+ }
+ String operator+(const char * op1, const String & op2)
+ {
+ if(!op2.buffer)
+ return String(op1);
- return StringConcat(op1, (int)strlen(op1), op2.buffer.Ptr(), op2.length);
- }
+ return StringConcat(op1, (int)strlen(op1), op2.buffer.Ptr(), op2.length);
+ }
- String operator+(const String & op1, const char * op2)
- {
- if(!op1.buffer)
- return String(op2);
+ String operator+(const String & op1, const char * op2)
+ {
+ if(!op1.buffer)
+ return String(op2);
- return StringConcat(op1.buffer.Ptr(), op1.length, op2, (int)strlen(op2));
- }
+ return StringConcat(op1.buffer.Ptr(), op1.length, op2, (int)strlen(op2));
+ }
- String operator+(const String & op1, const String & op2)
- {
- if(!op1.buffer && !op2.buffer)
- return String();
- else if(!op1.buffer)
- return String(op2);
- else if(!op2.buffer)
- return String(op1);
+ String operator+(const String & op1, const String & op2)
+ {
+ if(!op1.buffer && !op2.buffer)
+ return String();
+ else if(!op1.buffer)
+ return String(op2);
+ else if(!op2.buffer)
+ return String(op1);
- return StringConcat(op1.buffer.Ptr(), op1.length, op2.buffer.Ptr(), op2.length);
- }
+ return StringConcat(op1.buffer.Ptr(), op1.length, op2.buffer.Ptr(), op2.length);
+ }
- int StringToInt(const String & str, int radix)
- {
- if (str.StartsWith("0x"))
- return (int)strtoll(str.Buffer(), NULL, 16);
- else
- return (int)strtoll(str.Buffer(), NULL, radix);
- }
- unsigned int StringToUInt(const String & str, int radix)
- {
- if (str.StartsWith("0x"))
- return (unsigned int)strtoull(str.Buffer(), NULL, 16);
- else
- return (unsigned int)strtoull(str.Buffer(), NULL, radix);
- }
- double StringToDouble(const String & str)
- {
- return (double)strtod(str.Buffer(), NULL);
- }
- float StringToFloat(const String & str)
- {
- return strtof(str.Buffer(), NULL);
- }
+ int StringToInt(const String & str, int radix)
+ {
+ if (str.StartsWith("0x"))
+ return (int)strtoll(str.Buffer(), NULL, 16);
+ else
+ return (int)strtoll(str.Buffer(), NULL, radix);
+ }
+ unsigned int StringToUInt(const String & str, int radix)
+ {
+ if (str.StartsWith("0x"))
+ return (unsigned int)strtoull(str.Buffer(), NULL, 16);
+ else
+ return (unsigned int)strtoull(str.Buffer(), NULL, radix);
+ }
+ double StringToDouble(const String & str)
+ {
+ return (double)strtod(str.Buffer(), NULL);
+ }
+ float StringToFloat(const String & str)
+ {
+ return strtof(str.Buffer(), NULL);
+ }
- String String::ReplaceAll(String src, String dst) const
+ String String::ReplaceAll(String src, String dst) const
+ {
+ String rs = *this;
+ int index = 0;
+ int srcLen = src.length;
+ int len = rs.length;
+ while ((index = rs.IndexOf(src, index)) != -1)
{
- String rs = *this;
- int index = 0;
- int srcLen = src.length;
- int len = rs.length;
- while ((index = rs.IndexOf(src, index)) != -1)
- {
- rs = rs.SubString(0, index) + dst + rs.SubString(index + srcLen, len - index - srcLen);
- len = rs.length;
- }
- return rs;
+ rs = rs.SubString(0, index) + dst + rs.SubString(index + srcLen, len - index - srcLen);
+ len = rs.length;
}
+ return rs;
+ }
- String String::FromWString(const wchar_t * wstr)
- {
+ String String::FromWString(const wchar_t * wstr)
+ {
#ifdef _WIN32
- return CoreLib::IO::Encoding::UTF16->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
+ return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
#else
- return CoreLib::IO::Encoding::UTF32->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
+ return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
#endif
- }
+ }
- String String::FromWString(const wchar_t * wstr, const wchar_t * wend)
- {
+ String String::FromWString(const wchar_t * wstr, const wchar_t * wend)
+ {
#ifdef _WIN32
- return CoreLib::IO::Encoding::UTF16->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
+ return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#else
- return CoreLib::IO::Encoding::UTF32->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
+ return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#endif
- }
+ }
- String String::FromWChar(const wchar_t ch)
- {
+ String String::FromWChar(const wchar_t ch)
+ {
#ifdef _WIN32
- return CoreLib::IO::Encoding::UTF16->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
+ return Slang::Encoding::UTF16->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#else
- return CoreLib::IO::Encoding::UTF32->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
+ return Slang::Encoding::UTF32->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#endif
- }
+ }
- String String::FromUnicodePoint(unsigned int codePoint)
+ String String::FromUnicodePoint(unsigned int codePoint)
+ {
+ char buf[6];
+ int len = Slang::EncodeUnicodePointToUTF8(buf, (int)codePoint);
+ buf[len] = 0;
+ return String(buf);
+ }
+
+ const wchar_t * String::ToWString(int * len) const
+ {
+ if (!buffer)
{
- char buf[6];
- int len = CoreLib::IO::EncodeUnicodePointToUTF8(buf, (int)codePoint);
- buf[len] = 0;
- return String(buf);
+ if (len)
+ *len = 0;
+ return L"";
}
-
- const wchar_t * String::ToWString(int * len) const
+ else
{
- if (!buffer)
+ if (wcharBuffer)
{
if (len)
- *len = 0;
- return L"";
- }
- else
- {
- if (wcharBuffer)
- {
- if (len)
- *len = (int)wcslen(wcharBuffer);
- return wcharBuffer;
- }
- List<char> buf;
- CoreLib::IO::Encoding::UTF16->GetBytes(buf, *this);
- if (len)
- *len = buf.Count() / sizeof(wchar_t);
- buf.Add(0);
- buf.Add(0);
- const_cast<String*>(this)->wcharBuffer = (wchar_t*)buf.Buffer();
- buf.ReleaseBuffer();
+ *len = (int)wcslen(wcharBuffer);
return wcharBuffer;
}
+ List<char> buf;
+ Slang::Encoding::UTF16->GetBytes(buf, *this);
+ if (len)
+ *len = buf.Count() / sizeof(wchar_t);
+ buf.Add(0);
+ buf.Add(0);
+ const_cast<String*>(this)->wcharBuffer = (wchar_t*)buf.Buffer();
+ buf.ReleaseBuffer();
+ return wcharBuffer;
}
+ }
- String String::PadLeft(char ch, int pLen)
- {
- StringBuilder sb;
- for (int i = 0; i < pLen - this->length; i++)
- sb << ch;
- for (int i = 0; i < this->length; i++)
- sb << buffer[i];
- return sb.ProduceString();
- }
+ String String::PadLeft(char ch, int pLen)
+ {
+ StringBuilder sb;
+ for (int i = 0; i < pLen - this->length; i++)
+ sb << ch;
+ for (int i = 0; i < this->length; i++)
+ sb << buffer[i];
+ return sb.ProduceString();
+ }
- String String::PadRight(char ch, int pLen)
- {
- StringBuilder sb;
- for (int i = 0; i < this->length; i++)
- sb << buffer[i];
- for (int i = 0; i < pLen - this->length; i++)
- sb << ch;
- return sb.ProduceString();
- }
+ String String::PadRight(char ch, int pLen)
+ {
+ StringBuilder sb;
+ for (int i = 0; i < this->length; i++)
+ sb << buffer[i];
+ for (int i = 0; i < pLen - this->length; i++)
+ sb << ch;
+ return sb.ProduceString();
}
}
diff --git a/source/core/slang-string.h b/source/core/slang-string.h
index 3eb99e8e3..80eb00605 100644
--- a/source/core/slang-string.h
+++ b/source/core/slang-string.h
@@ -8,733 +8,730 @@
#include "hash.h"
#include "secure-crt.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ class _EndLine
+ {};
+ extern _EndLine EndLine;
+
+ // in-place reversion, works only for ascii string
+ inline void ReverseInternalAscii(char * buffer, int length)
+ {
+ int i, j;
+ char c;
+ for (i = 0, j = length - 1; i<j; i++, j--)
+ {
+ c = buffer[i];
+ buffer[i] = buffer[j];
+ buffer[j] = c;
+ }
+ }
+ template<typename IntType>
+ inline int IntToAscii(char * buffer, IntType val, int radix)
+ {
+ int i = 0;
+ IntType sign;
+ sign = val;
+ if (sign < 0)
+ val = (IntType)(0 - val);
+ do
+ {
+ int digit = (val % radix);
+ if (digit <= 9)
+ buffer[i++] = (char)(digit + '0');
+ else
+ buffer[i++] = (char)(digit - 10 + 'A');
+ } while ((val /= radix) > 0);
+ if (sign < 0)
+ buffer[i++] = '-';
+ buffer[i] = '\0';
+ return i;
+ }
+
+ inline bool IsUtf8LeadingByte(char ch)
+ {
+ return (((unsigned char)ch) & 0xC0) == 0xC0;
+ }
+
+ inline bool IsUtf8ContinuationByte(char ch)
{
- class _EndLine
- {};
- extern _EndLine EndLine;
+ return (((unsigned char)ch) & 0xC0) == 0x80;
+ }
+
+ /*!
+ @brief Represents a UTF-8 encoded string.
+ */
- // in-place reversion, works only for ascii string
- inline void ReverseInternalAscii(char * buffer, int length)
+ class String
+ {
+ friend class StringBuilder;
+ private:
+ RefPtr<char, RefPtrArrayDestructor> buffer;
+ wchar_t * wcharBuffer = nullptr;
+ int length = 0;
+ void Free()
{
- int i, j;
- char c;
- for (i = 0, j = length - 1; i<j; i++, j--)
- {
- c = buffer[i];
- buffer[i] = buffer[j];
- buffer[j] = c;
- }
+ if (buffer)
+ buffer = 0;
+ if (wcharBuffer)
+ delete[] wcharBuffer;
+ buffer = 0;
+ wcharBuffer = 0;
+ length = 0;
}
- template<typename IntType>
- inline int IntToAscii(char * buffer, IntType val, int radix)
+ public:
+ static String FromBuffer(RefPtr<char, RefPtrArrayDestructor> buffer, int len)
{
- int i = 0;
- IntType sign;
- sign = val;
- if (sign < 0)
- val = (IntType)(0 - val);
- do
- {
- int digit = (val % radix);
- if (digit <= 9)
- buffer[i++] = (char)(digit + '0');
- else
- buffer[i++] = (char)(digit - 10 + 'A');
- } while ((val /= radix) > 0);
- if (sign < 0)
- buffer[i++] = '-';
- buffer[i] = '\0';
- return i;
+ String rs;
+ rs.buffer = buffer;
+ rs.length = len;
+ return rs;
}
-
- inline bool IsUtf8LeadingByte(char ch)
+ static String FromWString(const wchar_t * wstr);
+ static String FromWString(const wchar_t * wstr, const wchar_t * wend);
+ static String FromWChar(const wchar_t ch);
+ static String FromUnicodePoint(unsigned int codePoint);
+ String()
{
- return (((unsigned char)ch) & 0xC0) == 0xC0;
}
-
- inline bool IsUtf8ContinuationByte(char ch)
+ const char * begin() const
{
- return (((unsigned char)ch) & 0xC0) == 0x80;
+ return buffer.Ptr();
}
-
- /*!
- @brief Represents a UTF-8 encoded string.
- */
-
- class String
+ const char * end() const
{
- friend class StringBuilder;
- private:
- RefPtr<char, RefPtrArrayDestructor> buffer;
- wchar_t * wcharBuffer = nullptr;
- int length = 0;
- void Free()
- {
- if (buffer)
- buffer = 0;
- if (wcharBuffer)
- delete[] wcharBuffer;
- buffer = 0;
- wcharBuffer = 0;
- length = 0;
- }
- public:
- static String FromBuffer(RefPtr<char, RefPtrArrayDestructor> buffer, int len)
- {
- String rs;
- rs.buffer = buffer;
- rs.length = len;
- return rs;
- }
- static String FromWString(const wchar_t * wstr);
- static String FromWString(const wchar_t * wstr, const wchar_t * wend);
- static String FromWChar(const wchar_t ch);
- static String FromUnicodePoint(unsigned int codePoint);
- String()
- {
- }
- const char * begin() const
- {
- return buffer.Ptr();
- }
- const char * end() const
- {
- return buffer.Ptr() + length;
- }
- String(int val, int radix = 10)
- {
- buffer = new char[33];
- length = IntToAscii(buffer.Ptr(), val, radix);
- ReverseInternalAscii(buffer.Ptr(), length);
- }
- String(unsigned int val, int radix = 10)
- {
- buffer = new char[33];
- length = IntToAscii(buffer.Ptr(), val, radix);
- ReverseInternalAscii(buffer.Ptr(), length);
- }
- String(long long val, int radix = 10)
- {
- buffer = new char[65];
- length = IntToAscii(buffer.Ptr(), val, radix);
- ReverseInternalAscii(buffer.Ptr(), length);
- }
- String(float val, const char * format = "%g")
- {
- buffer = new char[128];
- sprintf_s(buffer.Ptr(), 128, format, val);
- length = (int)strnlen_s(buffer.Ptr(), 128);
- }
- String(double val, const char * format = "%g")
- {
- buffer = new char[128];
- sprintf_s(buffer.Ptr(), 128, format, val);
- length = (int)strnlen_s(buffer.Ptr(), 128);
- }
- String(const char * str)
- {
- if (str)
- {
- length = (int)strlen(str);
- buffer = new char[length + 1];
- memcpy(buffer.Ptr(), str, length + 1);
- }
- }
- String(char chr)
- {
- if (chr)
- {
- length = 1;
- buffer = new char[2];
- buffer[0] = chr;
- buffer[1] = '\0';
- }
- }
- String(const String & str)
- {
- this->operator=(str);
- }
- String(String&& other)
+ return buffer.Ptr() + length;
+ }
+ String(int val, int radix = 10)
+ {
+ buffer = new char[33];
+ length = IntToAscii(buffer.Ptr(), val, radix);
+ ReverseInternalAscii(buffer.Ptr(), length);
+ }
+ String(unsigned int val, int radix = 10)
+ {
+ buffer = new char[33];
+ length = IntToAscii(buffer.Ptr(), val, radix);
+ ReverseInternalAscii(buffer.Ptr(), length);
+ }
+ String(long long val, int radix = 10)
+ {
+ buffer = new char[65];
+ length = IntToAscii(buffer.Ptr(), val, radix);
+ ReverseInternalAscii(buffer.Ptr(), length);
+ }
+ String(float val, const char * format = "%g")
+ {
+ buffer = new char[128];
+ sprintf_s(buffer.Ptr(), 128, format, val);
+ length = (int)strnlen_s(buffer.Ptr(), 128);
+ }
+ String(double val, const char * format = "%g")
+ {
+ buffer = new char[128];
+ sprintf_s(buffer.Ptr(), 128, format, val);
+ length = (int)strnlen_s(buffer.Ptr(), 128);
+ }
+ String(const char * str)
+ {
+ if (str)
{
- this->operator=(static_cast<String&&>(other));
+ length = (int)strlen(str);
+ buffer = new char[length + 1];
+ memcpy(buffer.Ptr(), str, length + 1);
}
- ~String()
+ }
+ String(char chr)
+ {
+ if (chr)
{
- Free();
+ length = 1;
+ buffer = new char[2];
+ buffer[0] = chr;
+ buffer[1] = '\0';
}
- String & operator=(const String & str)
- {
- if (str.buffer == buffer)
- return *this;
- Free();
- if (str.buffer)
- {
- length = str.length;
- buffer = str.buffer;
- wcharBuffer = 0;
- }
+ }
+ String(const String & str)
+ {
+ this->operator=(str);
+ }
+ String(String&& other)
+ {
+ this->operator=(static_cast<String&&>(other));
+ }
+ ~String()
+ {
+ Free();
+ }
+ String & operator=(const String & str)
+ {
+ if (str.buffer == buffer)
return *this;
- }
- String & operator=(String&& other)
+ Free();
+ if (str.buffer)
{
- if (this != &other)
- {
- Free();
- buffer = _Move(other.buffer);
- length = other.length;
- wcharBuffer = other.wcharBuffer;
- other.buffer = 0;
- other.length = 0;
- other.wcharBuffer = 0;
- }
- return *this;
+ length = str.length;
+ buffer = str.buffer;
+ wcharBuffer = 0;
}
- char operator[](int id) const
+ return *this;
+ }
+ String & operator=(String&& other)
+ {
+ if (this != &other)
{
+ Free();
+ buffer = _Move(other.buffer);
+ length = other.length;
+ wcharBuffer = other.wcharBuffer;
+ other.buffer = 0;
+ other.length = 0;
+ other.wcharBuffer = 0;
+ }
+ return *this;
+ }
+ char operator[](int id) const
+ {
#if _DEBUG
- if (id < 0 || id >= length)
- throw "Operator[]: index out of range.";
+ if (id < 0 || id >= length)
+ throw "Operator[]: index out of range.";
#endif
- return buffer.Ptr()[id];
- }
+ return buffer.Ptr()[id];
+ }
- friend String StringConcat(const char * lhs, int leftLen, const char * rhs, int rightLen);
- friend String operator+(const char*op1, const String & op2);
- friend String operator+(const String & op1, const char * op2);
- friend String operator+(const String & op1, const String & op2);
+ friend String StringConcat(const char * lhs, int leftLen, const char * rhs, int rightLen);
+ friend String operator+(const char*op1, const String & op2);
+ friend String operator+(const String & op1, const char * op2);
+ friend String operator+(const String & op1, const String & op2);
- String TrimStart() const
- {
- if (!buffer)
- return *this;
- int startIndex = 0;
- while (startIndex < length &&
- (buffer[startIndex] == ' ' || buffer[startIndex] == '\t' || buffer[startIndex] == '\r' || buffer[startIndex] == '\n'))
- startIndex++;
- return String(buffer + startIndex);
- }
+ String TrimStart() const
+ {
+ if (!buffer)
+ return *this;
+ int startIndex = 0;
+ while (startIndex < length &&
+ (buffer[startIndex] == ' ' || buffer[startIndex] == '\t' || buffer[startIndex] == '\r' || buffer[startIndex] == '\n'))
+ startIndex++;
+ return String(buffer + startIndex);
+ }
- String TrimEnd() const
- {
- if (!buffer)
- return *this;
-
- int endIndex = length - 1;
- while (endIndex >= 0 &&
- (buffer[endIndex] == ' ' || buffer[endIndex] == '\t' || buffer[endIndex] == '\r' || buffer[endIndex] == '\n'))
- endIndex--;
- String res;
- res.length = endIndex + 1;
- res.buffer = new char[endIndex + 2];
- strncpy_s(res.buffer.Ptr(), endIndex + 2, buffer.Ptr(), endIndex + 1);
- return res;
- }
+ String TrimEnd() const
+ {
+ if (!buffer)
+ return *this;
- String Trim() const
- {
- if (!buffer)
- return *this;
-
- int startIndex = 0;
- while (startIndex < length &&
- (buffer[startIndex] == ' ' || buffer[startIndex] == '\t'))
- startIndex++;
- int endIndex = length - 1;
- while (endIndex >= startIndex &&
- (buffer[endIndex] == ' ' || buffer[endIndex] == '\t'))
- endIndex--;
-
- String res;
- res.length = endIndex - startIndex + 1;
- res.buffer = new char[res.length + 1];
- memcpy(res.buffer.Ptr(), buffer + startIndex, res.length);
- res.buffer[res.length] = '\0';
- return res;
- }
+ int endIndex = length - 1;
+ while (endIndex >= 0 &&
+ (buffer[endIndex] == ' ' || buffer[endIndex] == '\t' || buffer[endIndex] == '\r' || buffer[endIndex] == '\n'))
+ endIndex--;
+ String res;
+ res.length = endIndex + 1;
+ res.buffer = new char[endIndex + 2];
+ strncpy_s(res.buffer.Ptr(), endIndex + 2, buffer.Ptr(), endIndex + 1);
+ return res;
+ }
- String SubString(int id, int len) const
- {
- if (len == 0)
- return "";
- if (id + len > length)
- len = length - id;
+ String Trim() const
+ {
+ if (!buffer)
+ return *this;
+
+ int startIndex = 0;
+ while (startIndex < length &&
+ (buffer[startIndex] == ' ' || buffer[startIndex] == '\t'))
+ startIndex++;
+ int endIndex = length - 1;
+ while (endIndex >= startIndex &&
+ (buffer[endIndex] == ' ' || buffer[endIndex] == '\t'))
+ endIndex--;
+
+ String res;
+ res.length = endIndex - startIndex + 1;
+ res.buffer = new char[res.length + 1];
+ memcpy(res.buffer.Ptr(), buffer + startIndex, res.length);
+ res.buffer[res.length] = '\0';
+ return res;
+ }
+
+ String SubString(int id, int len) const
+ {
+ if (len == 0)
+ return "";
+ if (id + len > length)
+ len = length - id;
#if _DEBUG
- if (id < 0 || id >= length || (id + len) > length)
- throw "SubString: index out of range.";
- if (len < 0)
- throw "SubString: length less than zero.";
+ if (id < 0 || id >= length || (id + len) > length)
+ throw "SubString: index out of range.";
+ if (len < 0)
+ throw "SubString: length less than zero.";
#endif
- String res;
- res.buffer = new char[len + 1];
- res.length = len;
- strncpy_s(res.buffer.Ptr(), len + 1, buffer + id, len);
- res.buffer[len] = 0;
- return res;
- }
+ String res;
+ res.buffer = new char[len + 1];
+ res.length = len;
+ strncpy_s(res.buffer.Ptr(), len + 1, buffer + id, len);
+ res.buffer[len] = 0;
+ return res;
+ }
- const char * Buffer() const
- {
- if (buffer)
- return buffer.Ptr();
- else
- return "";
- }
+ const char * Buffer() const
+ {
+ if (buffer)
+ return buffer.Ptr();
+ else
+ return "";
+ }
- const wchar_t * ToWString(int * len = 0) const;
+ const wchar_t * ToWString(int * len = 0) const;
- bool Equals(const String & str, bool caseSensitive = true)
+ bool Equals(const String & str, bool caseSensitive = true)
+ {
+ if (!buffer)
+ return (str.buffer == 0);
+ if (caseSensitive)
+ return (strcmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
+ else
{
- if (!buffer)
- return (str.buffer == 0);
- if (caseSensitive)
- return (strcmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
- else
- {
#ifdef _MSC_VER
- return (_stricmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
+ return (_stricmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
#else
- return (strcasecmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
+ return (strcasecmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
#endif
- }
- }
- bool operator==(const char * strbuffer) const
- {
- if (!buffer)
- return (strbuffer == 0 || strcmp(strbuffer, "") == 0);
- if (!strbuffer)
- return buffer == nullptr || strcmp(buffer.Ptr(), "") == 0;
- return (strcmp(buffer.Ptr(), strbuffer) == 0);
}
+ }
+ bool operator==(const char * strbuffer) const
+ {
+ if (!buffer)
+ return (strbuffer == 0 || strcmp(strbuffer, "") == 0);
+ if (!strbuffer)
+ return buffer == nullptr || strcmp(buffer.Ptr(), "") == 0;
+ return (strcmp(buffer.Ptr(), strbuffer) == 0);
+ }
- bool operator==(const String & str) const
- {
- if (!buffer)
- return (str.buffer == 0 || strcmp(str.buffer.Ptr(), "") == 0);
- if (!str.buffer)
- return buffer == nullptr || strcmp(buffer.Ptr(), "") == 0;
- return (strcmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
- }
- bool operator!=(const char * strbuffer) const
- {
- if (!buffer)
- return (strbuffer != 0 && strcmp(strbuffer, "") != 0);
- if (strbuffer == 0)
- return length != 0;
- return (strcmp(buffer.Ptr(), strbuffer) != 0);
- }
- bool operator!=(const String & str) const
- {
- if (!buffer)
- return (str.buffer != 0 && strcmp(str.buffer.Ptr(), "") != 0);
- if (str.buffer.Ptr() == 0)
- return length != 0;
- return (strcmp(buffer.Ptr(), str.buffer.Ptr()) != 0);
- }
- bool operator>(const String & str) const
- {
- if (!buffer)
- return false;
- if (!str.buffer)
- return buffer.Ptr() != nullptr && length != 0;
- return (strcmp(buffer.Ptr(), str.buffer.Ptr()) > 0);
- }
- bool operator<(const String & str) const
- {
- if (!buffer)
- return (str.buffer != 0);
- if (!str.buffer)
- return false;
- return (strcmp(buffer.Ptr(), str.buffer.Ptr()) < 0);
- }
- bool operator>=(const String & str) const
- {
- if (!buffer)
- return (str.buffer == 0);
- if (!str.buffer)
- return length == 0;
- int res = strcmp(buffer.Ptr(), str.buffer.Ptr());
- return (res > 0 || res == 0);
- }
- bool operator<=(const String & str) const
- {
- if (!buffer)
- return true;
- if (!str.buffer)
- return length > 0;
- int res = strcmp(buffer.Ptr(), str.buffer.Ptr());
- return (res < 0 || res == 0);
- }
+ bool operator==(const String & str) const
+ {
+ if (!buffer)
+ return (str.buffer == 0 || strcmp(str.buffer.Ptr(), "") == 0);
+ if (!str.buffer)
+ return buffer == nullptr || strcmp(buffer.Ptr(), "") == 0;
+ return (strcmp(buffer.Ptr(), str.buffer.Ptr()) == 0);
+ }
+ bool operator!=(const char * strbuffer) const
+ {
+ if (!buffer)
+ return (strbuffer != 0 && strcmp(strbuffer, "") != 0);
+ if (strbuffer == 0)
+ return length != 0;
+ return (strcmp(buffer.Ptr(), strbuffer) != 0);
+ }
+ bool operator!=(const String & str) const
+ {
+ if (!buffer)
+ return (str.buffer != 0 && strcmp(str.buffer.Ptr(), "") != 0);
+ if (str.buffer.Ptr() == 0)
+ return length != 0;
+ return (strcmp(buffer.Ptr(), str.buffer.Ptr()) != 0);
+ }
+ bool operator>(const String & str) const
+ {
+ if (!buffer)
+ return false;
+ if (!str.buffer)
+ return buffer.Ptr() != nullptr && length != 0;
+ return (strcmp(buffer.Ptr(), str.buffer.Ptr()) > 0);
+ }
+ bool operator<(const String & str) const
+ {
+ if (!buffer)
+ return (str.buffer != 0);
+ if (!str.buffer)
+ return false;
+ return (strcmp(buffer.Ptr(), str.buffer.Ptr()) < 0);
+ }
+ bool operator>=(const String & str) const
+ {
+ if (!buffer)
+ return (str.buffer == 0);
+ if (!str.buffer)
+ return length == 0;
+ int res = strcmp(buffer.Ptr(), str.buffer.Ptr());
+ return (res > 0 || res == 0);
+ }
+ bool operator<=(const String & str) const
+ {
+ if (!buffer)
+ return true;
+ if (!str.buffer)
+ return length > 0;
+ int res = strcmp(buffer.Ptr(), str.buffer.Ptr());
+ return (res < 0 || res == 0);
+ }
- String ToUpper() const
- {
- if (!buffer)
- return *this;
- String res;
- res.length = length;
- res.buffer = new char[length + 1];
- for (int i = 0; i <= length; i++)
- res.buffer[i] = (buffer[i] >= 'a' && buffer[i] <= 'z') ?
- (buffer[i] - 'a' + 'A') : buffer[i];
- return res;
- }
+ String ToUpper() const
+ {
+ if (!buffer)
+ return *this;
+ String res;
+ res.length = length;
+ res.buffer = new char[length + 1];
+ for (int i = 0; i <= length; i++)
+ res.buffer[i] = (buffer[i] >= 'a' && buffer[i] <= 'z') ?
+ (buffer[i] - 'a' + 'A') : buffer[i];
+ return res;
+ }
- String ToLower() const
- {
- if (!buffer)
- return *this;
- String res;
- res.length = length;
- res.buffer = new char[length + 1];
- for (int i = 0; i <= length; i++)
- res.buffer[i] = (buffer[i] >= 'A' && buffer[i] <= 'Z') ?
- (buffer[i] - 'A' + 'a') : buffer[i];
- return res;
- }
+ String ToLower() const
+ {
+ if (!buffer)
+ return *this;
+ String res;
+ res.length = length;
+ res.buffer = new char[length + 1];
+ for (int i = 0; i <= length; i++)
+ res.buffer[i] = (buffer[i] >= 'A' && buffer[i] <= 'Z') ?
+ (buffer[i] - 'A' + 'a') : buffer[i];
+ return res;
+ }
- int Length() const
- {
- return length;
- }
+ int Length() const
+ {
+ return length;
+ }
- int IndexOf(const char * str, int id) const // String str
- {
- if (!buffer)
- return -1;
- if (id < 0 || id >= length)
- return -1;
- auto findRs = strstr(buffer + id, str);
- int res = findRs ? (int)(findRs - buffer.Ptr()) : -1;
- if (res >= 0)
- return res;
- else
- return -1;
- }
+ int IndexOf(const char * str, int id) const // String str
+ {
+ if (!buffer)
+ return -1;
+ if (id < 0 || id >= length)
+ return -1;
+ auto findRs = strstr(buffer + id, str);
+ int res = findRs ? (int)(findRs - buffer.Ptr()) : -1;
+ if (res >= 0)
+ return res;
+ else
+ return -1;
+ }
- int IndexOf(const String & str, int id) const
- {
- return IndexOf(str.buffer.Ptr(), id);
- }
+ int IndexOf(const String & str, int id) const
+ {
+ return IndexOf(str.buffer.Ptr(), id);
+ }
- int IndexOf(const char * str) const
- {
- return IndexOf(str, 0);
- }
+ int IndexOf(const char * str) const
+ {
+ return IndexOf(str, 0);
+ }
- int IndexOf(const String & str) const
- {
- return IndexOf(str.buffer.Ptr(), 0);
- }
+ int IndexOf(const String & str) const
+ {
+ return IndexOf(str.buffer.Ptr(), 0);
+ }
- int IndexOf(char ch, int id) const
- {
+ int IndexOf(char ch, int id) const
+ {
#if _DEBUG
- if (id < 0 || id >= length)
- throw "SubString: index out of range.";
+ if (id < 0 || id >= length)
+ throw "SubString: index out of range.";
#endif
- if (!buffer)
- return -1;
- for (int i = id; i < length; i++)
- if (buffer[i] == ch)
- return i;
+ if (!buffer)
return -1;
- }
+ for (int i = id; i < length; i++)
+ if (buffer[i] == ch)
+ return i;
+ return -1;
+ }
- int IndexOf(char ch) const
- {
- return IndexOf(ch, 0);
- }
+ int IndexOf(char ch) const
+ {
+ return IndexOf(ch, 0);
+ }
- int LastIndexOf(char ch) const
- {
- for (int i = length - 1; i >= 0; i--)
- if (buffer[i] == ch)
- return i;
- return -1;
- }
+ int LastIndexOf(char ch) const
+ {
+ for (int i = length - 1; i >= 0; i--)
+ if (buffer[i] == ch)
+ return i;
+ return -1;
+ }
- bool StartsWith(const char * str) const // String str
- {
- if (!buffer)
- return false;
- int strLen = (int)strlen(str);
- if (strLen > length)
+ bool StartsWith(const char * str) const // String str
+ {
+ if (!buffer)
+ return false;
+ int strLen = (int)strlen(str);
+ if (strLen > length)
+ return false;
+ for (int i = 0; i < strLen; i++)
+ if (str[i] != buffer[i])
return false;
- for (int i = 0; i < strLen; i++)
- if (str[i] != buffer[i])
- return false;
- return true;
- }
+ return true;
+ }
- bool StartsWith(const String & str) const
- {
- return StartsWith(str.buffer.Ptr());
- }
+ bool StartsWith(const String & str) const
+ {
+ return StartsWith(str.buffer.Ptr());
+ }
- bool EndsWith(char * str) const // String str
- {
- if (!buffer)
- return false;
- int strLen = (int)strlen(str);
- if (strLen > length)
+ bool EndsWith(char * str) const // String str
+ {
+ if (!buffer)
+ return false;
+ int strLen = (int)strlen(str);
+ if (strLen > length)
+ return false;
+ for (int i = strLen - 1; i >= 0; i--)
+ if (str[i] != buffer[length - strLen + i])
return false;
- for (int i = strLen - 1; i >= 0; i--)
- if (str[i] != buffer[length - strLen + i])
- return false;
- return true;
- }
+ return true;
+ }
- bool EndsWith(const String & str) const
- {
- return EndsWith(str.buffer.Ptr());
- }
+ bool EndsWith(const String & str) const
+ {
+ return EndsWith(str.buffer.Ptr());
+ }
- bool Contains(const char * str) const // String str
- {
- if (!buffer)
- return false;
- return (IndexOf(str) >= 0) ? true : false;
- }
+ bool Contains(const char * str) const // String str
+ {
+ if (!buffer)
+ return false;
+ return (IndexOf(str) >= 0) ? true : false;
+ }
- bool Contains(const String & str) const
- {
- return Contains(str.buffer.Ptr());
- }
+ bool Contains(const String & str) const
+ {
+ return Contains(str.buffer.Ptr());
+ }
- int GetHashCode() const
- {
- return CoreLib::Basic::GetHashCode((const char*)buffer.Ptr());
- }
- String PadLeft(char ch, int length);
- String PadRight(char ch, int length);
- String ReplaceAll(String src, String dst) const;
- };
-
- class StringBuilder
- {
- private:
- char * buffer;
- int length;
- int bufferSize;
- static const int InitialSize = 512;
- public:
- StringBuilder(int bufferSize = 1024)
- :buffer(0), length(0), bufferSize(0)
- {
- buffer = new char[InitialSize]; // new a larger buffer
- buffer[0] = '\0';
- length = 0;
- bufferSize = InitialSize;
- }
- ~StringBuilder()
+ int GetHashCode() const
+ {
+ return Slang::GetHashCode((const char*)buffer.Ptr());
+ }
+ String PadLeft(char ch, int length);
+ String PadRight(char ch, int length);
+ String ReplaceAll(String src, String dst) const;
+ };
+
+ class StringBuilder
+ {
+ private:
+ char * buffer;
+ int length;
+ int bufferSize;
+ static const int InitialSize = 512;
+ public:
+ StringBuilder(int bufferSize = 1024)
+ :buffer(0), length(0), bufferSize(0)
+ {
+ buffer = new char[InitialSize]; // new a larger buffer
+ buffer[0] = '\0';
+ length = 0;
+ bufferSize = InitialSize;
+ }
+ ~StringBuilder()
+ {
+ if (buffer)
+ delete[] buffer;
+ }
+ void EnsureCapacity(int size)
+ {
+ if (bufferSize < size)
{
+ char * newBuffer = new char[size + 1];
if (buffer)
- delete[] buffer;
- }
- void EnsureCapacity(int size)
- {
- if (bufferSize < size)
{
- char * newBuffer = new char[size + 1];
- if (buffer)
- {
- strcpy_s(newBuffer, size + 1, buffer);
- delete[] buffer;
- }
- buffer = newBuffer;
- bufferSize = size;
+ strcpy_s(newBuffer, size + 1, buffer);
+ delete[] buffer;
}
+ buffer = newBuffer;
+ bufferSize = size;
}
- StringBuilder & operator << (char ch)
- {
- Append(&ch, 1);
- return *this;
- }
- StringBuilder & operator << (int val)
- {
- Append(val);
- return *this;
- }
- StringBuilder & operator << (unsigned int val)
- {
- Append(val);
- return *this;
- }
- StringBuilder & operator << (long long val)
- {
- Append(val);
- return *this;
- }
- StringBuilder & operator << (float val)
- {
- Append(val);
- return *this;
- }
- StringBuilder & operator << (double val)
- {
- Append(val);
- return *this;
- }
- StringBuilder & operator << (const char * str)
- {
- Append(str, (int)strlen(str));
- return *this;
- }
- StringBuilder & operator << (const String & str)
- {
- Append(str);
- return *this;
- }
- StringBuilder & operator << (const _EndLine)
- {
- Append('\n');
- return *this;
- }
- void Append(char ch)
- {
- Append(&ch, 1);
- }
- void Append(float val)
- {
- char buf[128];
- sprintf_s(buf, 128, "%g", val);
- int len = (int)strnlen_s(buf, 128);
- Append(buf, len);
- }
- void Append(double val)
- {
- char buf[128];
- sprintf_s(buf, 128, "%g", val);
- int len = (int)strnlen_s(buf, 128);
- Append(buf, len);
- }
- void Append(unsigned int value, int radix = 10)
- {
- char vBuffer[33];
- int len = IntToAscii(vBuffer, value, radix);
- ReverseInternalAscii(vBuffer, len);
- Append(vBuffer);
- }
- void Append(int value, int radix = 10)
- {
- char vBuffer[33];
- int len = IntToAscii(vBuffer, value, radix);
- ReverseInternalAscii(vBuffer, len);
- Append(vBuffer);
- }
- void Append(long long value, int radix = 10)
- {
- char vBuffer[65];
- int len = IntToAscii(vBuffer, value, radix);
- ReverseInternalAscii(vBuffer, len);
- Append(vBuffer);
- }
- void Append(const String & str)
- {
- Append(str.Buffer(), str.Length());
- }
- void Append(const char * str)
- {
- Append(str, (int)strlen(str));
- }
- void Append(const char * str, int strLen)
+ }
+ StringBuilder & operator << (char ch)
+ {
+ Append(&ch, 1);
+ return *this;
+ }
+ StringBuilder & operator << (int val)
+ {
+ Append(val);
+ return *this;
+ }
+ StringBuilder & operator << (unsigned int val)
+ {
+ Append(val);
+ return *this;
+ }
+ StringBuilder & operator << (long long val)
+ {
+ Append(val);
+ return *this;
+ }
+ StringBuilder & operator << (float val)
+ {
+ Append(val);
+ return *this;
+ }
+ StringBuilder & operator << (double val)
+ {
+ Append(val);
+ return *this;
+ }
+ StringBuilder & operator << (const char * str)
+ {
+ Append(str, (int)strlen(str));
+ return *this;
+ }
+ StringBuilder & operator << (const String & str)
+ {
+ Append(str);
+ return *this;
+ }
+ StringBuilder & operator << (const _EndLine)
+ {
+ Append('\n');
+ return *this;
+ }
+ void Append(char ch)
+ {
+ Append(&ch, 1);
+ }
+ void Append(float val)
+ {
+ char buf[128];
+ sprintf_s(buf, 128, "%g", val);
+ int len = (int)strnlen_s(buf, 128);
+ Append(buf, len);
+ }
+ void Append(double val)
+ {
+ char buf[128];
+ sprintf_s(buf, 128, "%g", val);
+ int len = (int)strnlen_s(buf, 128);
+ Append(buf, len);
+ }
+ void Append(unsigned int value, int radix = 10)
+ {
+ char vBuffer[33];
+ int len = IntToAscii(vBuffer, value, radix);
+ ReverseInternalAscii(vBuffer, len);
+ Append(vBuffer);
+ }
+ void Append(int value, int radix = 10)
+ {
+ char vBuffer[33];
+ int len = IntToAscii(vBuffer, value, radix);
+ ReverseInternalAscii(vBuffer, len);
+ Append(vBuffer);
+ }
+ void Append(long long value, int radix = 10)
+ {
+ char vBuffer[65];
+ int len = IntToAscii(vBuffer, value, radix);
+ ReverseInternalAscii(vBuffer, len);
+ Append(vBuffer);
+ }
+ void Append(const String & str)
+ {
+ Append(str.Buffer(), str.Length());
+ }
+ void Append(const char * str)
+ {
+ Append(str, (int)strlen(str));
+ }
+ void Append(const char * str, int strLen)
+ {
+ int newLength = length + strLen;
+ if (bufferSize < newLength + 1)
{
- int newLength = length + strLen;
- if (bufferSize < newLength + 1)
- {
- int newBufferSize = InitialSize;
- while (newBufferSize < newLength + 1)
- newBufferSize <<= 1;
- char * newBuffer = new char[newBufferSize];
- if (buffer)
- {
- memcpy(newBuffer, buffer, length);
- delete[] buffer;
- }
- memcpy(newBuffer + length, str, strLen);
- newBuffer[newLength] = '\0';
- buffer = newBuffer;
- bufferSize = newBufferSize;
- }
- else
+ int newBufferSize = InitialSize;
+ while (newBufferSize < newLength + 1)
+ newBufferSize <<= 1;
+ char * newBuffer = new char[newBufferSize];
+ if (buffer)
{
- memcpy(buffer + length, str, strLen);
- buffer[newLength] = '\0';
+ memcpy(newBuffer, buffer, length);
+ delete[] buffer;
}
- length = newLength;
+ memcpy(newBuffer + length, str, strLen);
+ newBuffer[newLength] = '\0';
+ buffer = newBuffer;
+ bufferSize = newBufferSize;
}
-
- int Capacity()
+ else
{
- return bufferSize;
+ memcpy(buffer + length, str, strLen);
+ buffer[newLength] = '\0';
}
+ length = newLength;
+ }
- char * Buffer()
- {
- return buffer;
- }
+ int Capacity()
+ {
+ return bufferSize;
+ }
- int Length()
- {
- return length;
- }
+ char * Buffer()
+ {
+ return buffer;
+ }
- String ToString()
- {
- return String(buffer);
- }
+ int Length()
+ {
+ return length;
+ }
- String ProduceString()
- {
- String rs;
- rs.buffer = buffer;
- rs.length = length;
- buffer = 0;
- bufferSize = 0;
- length = 0;
- return rs;
+ String ToString()
+ {
+ return String(buffer);
+ }
- }
+ String ProduceString()
+ {
+ String rs;
+ rs.buffer = buffer;
+ rs.length = length;
+ buffer = 0;
+ bufferSize = 0;
+ length = 0;
+ return rs;
- String GetSubString(int start, int count)
- {
- String rs;
- rs.buffer = new char[count + 1];
- rs.length = count;
- strncpy_s(rs.buffer.Ptr(), count + 1, buffer + start, count);
- rs.buffer[count] = 0;
- return rs;
- }
+ }
- void Remove(int id, int len)
- {
+ String GetSubString(int start, int count)
+ {
+ String rs;
+ rs.buffer = new char[count + 1];
+ rs.length = count;
+ strncpy_s(rs.buffer.Ptr(), count + 1, buffer + start, count);
+ rs.buffer[count] = 0;
+ return rs;
+ }
+
+ void Remove(int id, int len)
+ {
#if _DEBUG
- if (id >= length || id < 0)
- throw "Remove: Index out of range.";
- if (len < 0)
- throw "Remove: remove length smaller than zero.";
+ if (id >= length || id < 0)
+ throw "Remove: Index out of range.";
+ if (len < 0)
+ throw "Remove: remove length smaller than zero.";
#endif
- int actualDelLength = ((id + len) >= length) ? (length - id) : len;
- for (int i = id + actualDelLength; i <= length; i++)
- buffer[i - actualDelLength] = buffer[i];
- length -= actualDelLength;
- }
+ int actualDelLength = ((id + len) >= length) ? (length - id) : len;
+ for (int i = id + actualDelLength; i <= length; i++)
+ buffer[i - actualDelLength] = buffer[i];
+ length -= actualDelLength;
+ }
- void Clear()
- {
- length = 0;
- if (buffer)
- buffer[0] = 0;
- }
- };
+ void Clear()
+ {
+ length = 0;
+ if (buffer)
+ buffer[0] = 0;
+ }
+ };
- int StringToInt(const String & str, int radix = 10);
- unsigned int StringToUInt(const String & str, int radix = 10);
- double StringToDouble(const String & str);
- float StringToFloat(const String & str);
- }
+ int StringToInt(const String & str, int radix = 10);
+ unsigned int StringToUInt(const String & str, int radix = 10);
+ double StringToDouble(const String & str);
+ float StringToFloat(const String & str);
}
#endif
diff --git a/source/core/smart-pointer.h b/source/core/smart-pointer.h
index f1fece2e5..d307e4e13 100644
--- a/source/core/smart-pointer.h
+++ b/source/core/smart-pointer.h
@@ -3,466 +3,463 @@
#include "type-traits.h"
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ class RefPtrDefaultDestructor
{
- class RefPtrDefaultDestructor
+ public:
+ template<typename T>
+ void operator ()(T * ptr)
{
- public:
- template<typename T>
- void operator ()(T * ptr)
- {
- delete ptr;
- }
- };
+ delete ptr;
+ }
+ };
- class RefPtrArrayDestructor
+ class RefPtrArrayDestructor
+ {
+ public:
+ template<typename T>
+ void operator() (T * ptr)
{
- public:
- template<typename T>
- void operator() (T * ptr)
- {
- delete [] ptr;
- }
- };
+ delete [] ptr;
+ }
+ };
- class ReferenceCounted
- {
- template<typename T, bool b, typename Destructor>
- friend class RefPtrImpl;
- private:
- int _refCount = 0;
- public:
- ReferenceCounted() {}
- ReferenceCounted(const ReferenceCounted &)
- {
- _refCount = 0;
- }
- };
+ class ReferenceCounted
+ {
+ template<typename T, bool b, typename Destructor>
+ friend class RefPtrImpl;
+ private:
+ int _refCount = 0;
+ public:
+ ReferenceCounted() {}
+ ReferenceCounted(const ReferenceCounted &)
+ {
+ _refCount = 0;
+ }
+ };
- class RefObject : public ReferenceCounted
- {
- public:
- virtual ~RefObject()
- {}
- };
+ class RefObject : public ReferenceCounted
+ {
+ public:
+ virtual ~RefObject()
+ {}
+ };
- template<typename T, bool HasBuiltInCounter, typename Destructor>
- class RefPtrImpl
- {
- };
+ template<typename T, bool HasBuiltInCounter, typename Destructor>
+ class RefPtrImpl
+ {
+ };
- template<typename T, typename Destructor = RefPtrDefaultDestructor>
- using RefPtr = RefPtrImpl<T, IsBaseOf<ReferenceCounted, T>::Value, Destructor>;
+ template<typename T, typename Destructor = RefPtrDefaultDestructor>
+ using RefPtr = RefPtrImpl<T, IsBaseOf<ReferenceCounted, T>::Value, Destructor>;
- template<typename T, typename Destructor>
- class RefPtrImpl<T, 0, Destructor>
- {
- template<typename T1, bool b, typename Destructor1>
- friend class RefPtrImpl;
- private:
- T * pointer;
- int * refCount;
+ template<typename T, typename Destructor>
+ class RefPtrImpl<T, 0, Destructor>
+ {
+ template<typename T1, bool b, typename Destructor1>
+ friend class RefPtrImpl;
+ private:
+ T * pointer;
+ int * refCount;
- public:
- RefPtrImpl()
- {
- pointer = 0;
- refCount = 0;
- }
- RefPtrImpl(T * ptr)
- : pointer(0), refCount(0)
- {
- this->operator=(ptr);
- }
- RefPtrImpl(const RefPtrImpl<T, 0, Destructor> & ptr)
- : pointer(0), refCount(0)
+ public:
+ RefPtrImpl()
+ {
+ pointer = 0;
+ refCount = 0;
+ }
+ RefPtrImpl(T * ptr)
+ : pointer(0), refCount(0)
+ {
+ this->operator=(ptr);
+ }
+ RefPtrImpl(const RefPtrImpl<T, 0, Destructor> & ptr)
+ : pointer(0), refCount(0)
+ {
+ this->operator=(ptr);
+ }
+ RefPtrImpl(RefPtrImpl<T, 0, Destructor> && str)
+ : pointer(0), refCount(0)
+ {
+ this->operator=(static_cast<RefPtrImpl<T, 0, Destructor> &&>(str));
+ }
+
+ template <typename U>
+ RefPtrImpl(const RefPtrImpl<U, 0, Destructor>& ptr,
+ typename EnableIf<IsConvertible<T*, U*>::Value, void>::type * = 0)
+ : pointer(0), refCount(0)
+ {
+ pointer = ptr.pointer;
+ if (ptr)
{
- this->operator=(ptr);
+ refCount = ptr.refCount;
+ (*refCount)++;
}
- RefPtrImpl(RefPtrImpl<T, 0, Destructor> && str)
- : pointer(0), refCount(0)
+ else
+ refCount = 0;
+ }
+
+ template <typename U>
+ typename EnableIf<IsConvertible<T*, U*>::value, RefPtrImpl<T, 0, Destructor>>::type&
+ operator=(const RefPtrImpl<U,0,Destructor> & ptr)
+ {
+ Unreference();
+
+ pointer = ptr;
+ if (ptr)
{
- this->operator=(static_cast<RefPtrImpl<T, 0, Destructor> &&>(str));
+ refCount = ptr.refCount;
+ (*refCount)++;
}
+ else
+ refCount = 0;
+ return *this;
+ }
- template <typename U>
- RefPtrImpl(const RefPtrImpl<U, 0, Destructor>& ptr,
- typename EnableIf<IsConvertible<T*, U*>::Value, void>::type * = 0)
- : pointer(0), refCount(0)
+ RefPtrImpl<T, 0, Destructor>& operator=(const RefPtrImpl<T, 0, Destructor> & ptr)
+ {
+ Unreference();
+ pointer = ptr.pointer;
+ if (ptr)
{
- pointer = ptr.pointer;
- if (ptr)
- {
- refCount = ptr.refCount;
- (*refCount)++;
- }
- else
- refCount = 0;
+ refCount = ptr.refCount;
+ (*refCount)++;
}
+ else
+ refCount = 0;
+ return *this;
+ }
- template <typename U>
- typename EnableIf<IsConvertible<T*, U*>::value, RefPtrImpl<T, 0, Destructor>>::type&
- operator=(const RefPtrImpl<U,0,Destructor> & ptr)
+ RefPtrImpl<T, 0, Destructor>& operator=(T * ptr)
+ {
+ if (ptr != pointer)
{
Unreference();
pointer = ptr;
if (ptr)
{
- refCount = ptr.refCount;
- (*refCount)++;
+ refCount = new int;
+ (*refCount) = 1;
}
else
refCount = 0;
- return *this;
}
-
- RefPtrImpl<T, 0, Destructor>& operator=(const RefPtrImpl<T, 0, Destructor> & ptr)
+ return *this;
+ }
+ int GetHashCode()
+ {
+ return (int)(long long)(void*)pointer;
+ }
+ bool operator == (const T * ptr) const
+ {
+ return pointer == ptr;
+ }
+ bool operator != (const T * ptr) const
+ {
+ return pointer != ptr;
+ }
+ template<typename U>
+ bool operator == (const RefPtr<U, Destructor> & ptr) const
+ {
+ return pointer == ptr.pointer;
+ }
+ template<typename U>
+ bool operator != (const RefPtr<U, Destructor> & ptr) const
+ {
+ return pointer != ptr.pointer;
+ }
+ template<typename U>
+ RefPtrImpl<U, 0, Destructor> As() const
+ {
+ RefPtrImpl<U, 0, Destructor> result;
+ if (pointer)
{
- Unreference();
- pointer = ptr.pointer;
- if (ptr)
+ result.pointer = dynamic_cast<U*>(pointer);
+ if (result.pointer)
{
- refCount = ptr.refCount;
+ result.refCount = refCount;
(*refCount)++;
}
- else
- refCount = 0;
- return *this;
}
+ return result;
+ }
- RefPtrImpl<T, 0, Destructor>& operator=(T * ptr)
- {
- if (ptr != pointer)
- {
- Unreference();
-
- pointer = ptr;
- if (ptr)
- {
- refCount = new int;
- (*refCount) = 1;
- }
- else
- refCount = 0;
- }
- return *this;
- }
- int GetHashCode()
- {
- return (int)(long long)(void*)pointer;
- }
- bool operator == (const T * ptr) const
- {
- return pointer == ptr;
- }
- bool operator != (const T * ptr) const
- {
- return pointer != ptr;
- }
- template<typename U>
- bool operator == (const RefPtr<U, Destructor> & ptr) const
- {
- return pointer == ptr.pointer;
- }
- template<typename U>
- bool operator != (const RefPtr<U, Destructor> & ptr) const
- {
- return pointer != ptr.pointer;
- }
- template<typename U>
- RefPtrImpl<U, 0, Destructor> As() const
- {
- RefPtrImpl<U, 0, Destructor> result;
- if (pointer)
- {
- result.pointer = dynamic_cast<U*>(pointer);
- if (result.pointer)
- {
- result.refCount = refCount;
- (*refCount)++;
- }
- }
- return result;
- }
-
- T* operator +(int offset) const
- {
- return pointer+offset;
- }
- T& operator [](int idx) const
+ T* operator +(int offset) const
+ {
+ return pointer+offset;
+ }
+ T& operator [](int idx) const
+ {
+ return *(pointer + idx);
+ }
+ RefPtrImpl<T, 0, Destructor>& operator=(RefPtrImpl<T, 0, Destructor> && ptr)
+ {
+ if(ptr.pointer != pointer)
{
- return *(pointer + idx);
+ Unreference();
+ pointer = ptr.pointer;
+ refCount = ptr.refCount;
+ ptr.pointer = 0;
+ ptr.refCount = 0;
}
- RefPtrImpl<T, 0, Destructor>& operator=(RefPtrImpl<T, 0, Destructor> && ptr)
+ return *this;
+ }
+ T* Release()
+ {
+ if(pointer)
{
- if(ptr.pointer != pointer)
+ if((*refCount) > 1)
{
- Unreference();
- pointer = ptr.pointer;
- refCount = ptr.refCount;
- ptr.pointer = 0;
- ptr.refCount = 0;
+ (*refCount)--;
}
- return *this;
- }
- T* Release()
- {
- if(pointer)
+ else
{
- if((*refCount) > 1)
- {
- (*refCount)--;
- }
- else
- {
- delete refCount;
- }
+ delete refCount;
}
- auto rs = pointer;
- refCount = 0;
- pointer = 0;
- return rs;
- }
- ~RefPtrImpl()
- {
- Unreference();
}
+ auto rs = pointer;
+ refCount = 0;
+ pointer = 0;
+ return rs;
+ }
+ ~RefPtrImpl()
+ {
+ Unreference();
+ }
- void Unreference()
+ void Unreference()
+ {
+ if(pointer)
{
- if(pointer)
+ if((*refCount) > 1)
{
- if((*refCount) > 1)
- {
- (*refCount)--;
- }
- else
- {
- Destructor destructor;
- destructor(pointer);
- delete refCount;
- }
+ (*refCount)--;
}
- }
- T & operator *() const
- {
- return *pointer;
- }
- T * operator->() const
- {
- return pointer;
- }
- T * Ptr() const
- {
- return pointer;
- }
- public:
- explicit operator bool() const
- {
- if (pointer)
- return true;
else
- return false;
+ {
+ Destructor destructor;
+ destructor(pointer);
+ delete refCount;
+ }
}
- };
+ }
+ T & operator *() const
+ {
+ return *pointer;
+ }
+ T * operator->() const
+ {
+ return pointer;
+ }
+ T * Ptr() const
+ {
+ return pointer;
+ }
+ public:
+ explicit operator bool() const
+ {
+ if (pointer)
+ return true;
+ else
+ return false;
+ }
+ };
- template<typename T, typename Destructor>
- class RefPtrImpl<T, 1, Destructor>
- {
- template<typename T1, bool b, typename Destructor1>
- friend class RefPtrImpl;
+ template<typename T, typename Destructor>
+ class RefPtrImpl<T, 1, Destructor>
+ {
+ template<typename T1, bool b, typename Destructor1>
+ friend class RefPtrImpl;
- private:
- T * pointer;
- public:
- RefPtrImpl()
- {
- pointer = 0;
- }
- RefPtrImpl(T * ptr)
- : pointer(0)
- {
- this->operator=(ptr);
- }
- RefPtrImpl(const RefPtrImpl<T, 1, Destructor> & ptr)
- : pointer(0)
- {
- this->operator=(ptr);
- }
- RefPtrImpl(RefPtrImpl<T, 1, Destructor> && str)
- : pointer(0)
+ private:
+ T * pointer;
+ public:
+ RefPtrImpl()
+ {
+ pointer = 0;
+ }
+ RefPtrImpl(T * ptr)
+ : pointer(0)
+ {
+ this->operator=(ptr);
+ }
+ RefPtrImpl(const RefPtrImpl<T, 1, Destructor> & ptr)
+ : pointer(0)
+ {
+ this->operator=(ptr);
+ }
+ RefPtrImpl(RefPtrImpl<T, 1, Destructor> && str)
+ : pointer(0)
+ {
+ this->operator=(static_cast<RefPtrImpl<T, 1, Destructor> &&>(str));
+ }
+ template <typename U>
+ RefPtrImpl(const RefPtrImpl<U, 1, Destructor>& ptr,
+ typename EnableIf<IsConvertible<T*, U*>::Value, void>::type * = 0)
+ : pointer(0)
+ {
+ pointer = ptr.pointer;
+ if (ptr)
{
- this->operator=(static_cast<RefPtrImpl<T, 1, Destructor> &&>(str));
+ ptr->_refCount++;
}
- template <typename U>
- RefPtrImpl(const RefPtrImpl<U, 1, Destructor>& ptr,
- typename EnableIf<IsConvertible<T*, U*>::Value, void>::type * = 0)
- : pointer(0)
+ }
+
+ template <typename U>
+ typename EnableIf<IsConvertible<T*, U*>::value, RefPtrImpl<T, 1, Destructor>&>::type
+ operator=(const RefPtrImpl<U, 1, Destructor> & ptr)
+ {
+ Unreference();
+
+ pointer = ptr.pointer;
+ if (ptr)
{
- pointer = ptr.pointer;
- if (ptr)
- {
- ptr->_refCount++;
- }
+ ptr->_refCount++;
}
-
- template <typename U>
- typename EnableIf<IsConvertible<T*, U*>::value, RefPtrImpl<T, 1, Destructor>&>::type
- operator=(const RefPtrImpl<U, 1, Destructor> & ptr)
+ return *this;
+ }
+ RefPtrImpl<T, 1, Destructor>& operator=(T * ptr)
+ {
+ if (ptr != pointer)
{
Unreference();
- pointer = ptr.pointer;
+ pointer = ptr;
if (ptr)
{
ptr->_refCount++;
}
- return *this;
}
- RefPtrImpl<T, 1, Destructor>& operator=(T * ptr)
- {
- if (ptr != pointer)
- {
- Unreference();
-
- pointer = ptr;
- if (ptr)
- {
- ptr->_refCount++;
- }
- }
- return *this;
- }
- RefPtrImpl<T, 1, Destructor>& operator=(const RefPtrImpl<T, 1, Destructor> & ptr)
- {
- // Note: It is possible that the object this pointer references owns
- // (directly or indirectly) the storage for the argument `ptr`. If
- // that is the case and the `Unreference()` call below frees this
- // object, then the argument would become invalid.
- //
- // We copy the pointer value out of the argument first, in order
- // to protected against this case.
- T* ptrPointer = ptr.pointer;
- if (ptrPointer != pointer)
- {
- if (ptrPointer)
- ptrPointer->_refCount++;
- Unreference();
- pointer = ptrPointer;
- }
- return *this;
- }
- int GetHashCode()
- {
- return (int)(long long)(void*)pointer;
- }
- bool operator == (const T * ptr) const
- {
- return pointer == ptr;
- }
- bool operator != (const T * ptr) const
- {
- return pointer != ptr;
- }
- template<typename U>
- bool operator == (const RefPtr<U, Destructor> & ptr) const
- {
- return pointer == ptr.pointer;
- }
- template<typename U>
- bool operator != (const RefPtr<U, Destructor> & ptr) const
- {
- return pointer != ptr.pointer;
+ return *this;
+ }
+ RefPtrImpl<T, 1, Destructor>& operator=(const RefPtrImpl<T, 1, Destructor> & ptr)
+ {
+ // Note: It is possible that the object this pointer references owns
+ // (directly or indirectly) the storage for the argument `ptr`. If
+ // that is the case and the `Unreference()` call below frees this
+ // object, then the argument would become invalid.
+ //
+ // We copy the pointer value out of the argument first, in order
+ // to protected against this case.
+ T* ptrPointer = ptr.pointer;
+ if (ptrPointer != pointer)
+ {
+ if (ptrPointer)
+ ptrPointer->_refCount++;
+ Unreference();
+ pointer = ptrPointer;
}
- template<typename U>
- RefPtrImpl<U, 1, Destructor> As() const
+ return *this;
+ }
+ int GetHashCode()
+ {
+ return (int)(long long)(void*)pointer;
+ }
+ bool operator == (const T * ptr) const
+ {
+ return pointer == ptr;
+ }
+ bool operator != (const T * ptr) const
+ {
+ return pointer != ptr;
+ }
+ template<typename U>
+ bool operator == (const RefPtr<U, Destructor> & ptr) const
+ {
+ return pointer == ptr.pointer;
+ }
+ template<typename U>
+ bool operator != (const RefPtr<U, Destructor> & ptr) const
+ {
+ return pointer != ptr.pointer;
+ }
+ template<typename U>
+ RefPtrImpl<U, 1, Destructor> As() const
+ {
+ RefPtrImpl<U, 1, Destructor> result;
+ if (pointer)
{
- RefPtrImpl<U, 1, Destructor> result;
- if (pointer)
+ result.pointer = dynamic_cast<U*>(pointer);
+ if (result.pointer)
{
- result.pointer = dynamic_cast<U*>(pointer);
- if (result.pointer)
- {
- result.pointer->_refCount++;
- }
+ result.pointer->_refCount++;
}
- return result;
- }
- T* operator +(int offset) const
- {
- return pointer + offset;
}
- T& operator [](int idx) const
+ return result;
+ }
+ T* operator +(int offset) const
+ {
+ return pointer + offset;
+ }
+ T& operator [](int idx) const
+ {
+ return *(pointer + idx);
+ }
+ RefPtrImpl<T, 1, Destructor>& operator=(RefPtrImpl<T, 1, Destructor> && ptr)
+ {
+ if (ptr.pointer != pointer)
{
- return *(pointer + idx);
+ Unreference();
+ pointer = ptr.pointer;
+ ptr.pointer = nullptr;
}
- RefPtrImpl<T, 1, Destructor>& operator=(RefPtrImpl<T, 1, Destructor> && ptr)
+ return *this;
+ }
+ T* Release()
+ {
+ if (pointer)
{
- if (ptr.pointer != pointer)
- {
- Unreference();
- pointer = ptr.pointer;
- ptr.pointer = nullptr;
- }
- return *this;
+ pointer->_refCount--;
}
- T* Release()
+ auto rs = pointer;
+ pointer = 0;
+ return rs;
+ }
+ ~RefPtrImpl()
+ {
+ Unreference();
+ }
+
+ void Unreference()
+ {
+ if (pointer)
{
- if (pointer)
+ if (pointer->_refCount > 1)
{
pointer->_refCount--;
}
- auto rs = pointer;
- pointer = 0;
- return rs;
- }
- ~RefPtrImpl()
- {
- Unreference();
- }
-
- void Unreference()
- {
- if (pointer)
+ else
{
- if (pointer->_refCount > 1)
- {
- pointer->_refCount--;
- }
- else
- {
- Destructor destructor;
- destructor(pointer);
- }
+ Destructor destructor;
+ destructor(pointer);
}
}
- T & operator *() const
- {
- return *pointer;
- }
- T * operator->() const
- {
- return pointer;
- }
- T * Ptr() const
- {
- return pointer;
- }
- public:
- explicit operator bool() const
- {
- if (pointer)
- return true;
- else
- return false;
- }
- };
- }
+ }
+ T & operator *() const
+ {
+ return *pointer;
+ }
+ T * operator->() const
+ {
+ return pointer;
+ }
+ T * Ptr() const
+ {
+ return pointer;
+ }
+ public:
+ explicit operator bool() const
+ {
+ if (pointer)
+ return true;
+ else
+ return false;
+ }
+ };
}
#endif \ No newline at end of file
diff --git a/source/core/stream.cpp b/source/core/stream.cpp
index 580ea5884..62e9092af 100644
--- a/source/core/stream.cpp
+++ b/source/core/stream.cpp
@@ -4,217 +4,213 @@
#endif
#include "slang-io.h"
-namespace CoreLib
+namespace Slang
{
- namespace IO
+ FileStream::FileStream(const Slang::String & fileName, FileMode fileMode)
{
- using namespace CoreLib::Basic;
- FileStream::FileStream(const CoreLib::Basic::String & fileName, FileMode fileMode)
- {
- Init(fileName, fileMode, fileMode==FileMode::Open?FileAccess::Read:FileAccess::Write, FileShare::None);
- }
- FileStream::FileStream(const CoreLib::Basic::String & fileName, FileMode fileMode, FileAccess access, FileShare share)
- {
- Init(fileName, fileMode, access, share);
- }
- void FileStream::Init(const CoreLib::Basic::String & fileName, FileMode fileMode, FileAccess access, FileShare share)
+ Init(fileName, fileMode, fileMode==FileMode::Open?FileAccess::Read:FileAccess::Write, FileShare::None);
+ }
+ FileStream::FileStream(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share)
+ {
+ Init(fileName, fileMode, access, share);
+ }
+ void FileStream::Init(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share)
+ {
+ const wchar_t * mode = L"rt";
+ const char* modeMBCS = "rt";
+ switch (fileMode)
{
- const wchar_t * mode = L"rt";
- const char* modeMBCS = "rt";
- switch (fileMode)
+ case Slang::FileMode::Create:
+ if (access == FileAccess::Read)
+ throw ArgumentException("Read-only access is incompatible with Create mode.");
+ else if (access == FileAccess::ReadWrite)
{
- case CoreLib::IO::FileMode::Create:
- if (access == FileAccess::Read)
- throw ArgumentException("Read-only access is incompatible with Create mode.");
- else if (access == FileAccess::ReadWrite)
- {
- mode = L"w+b";
- modeMBCS = "w+b";
- this->fileAccess = FileAccess::ReadWrite;
- }
- else
- {
- mode = L"wb";
- modeMBCS = "wb";
- this->fileAccess = FileAccess::Write;
- }
- break;
- case CoreLib::IO::FileMode::Open:
- if (access == FileAccess::Read)
- {
- mode = L"rb";
- modeMBCS = "rb";
- this->fileAccess = FileAccess::Read;
- }
- else if (access == FileAccess::ReadWrite)
- {
- mode = L"r+b";
- modeMBCS = "r+b";
- this->fileAccess = FileAccess::ReadWrite;
- }
- else
- {
- mode = L"wb";
- modeMBCS = "wb";
- this->fileAccess = FileAccess::Write;
- }
- break;
- case CoreLib::IO::FileMode::CreateNew:
- if (File::Exists(fileName))
- {
- throw IOException("Failed opening '" + fileName + "', file already exists.");
- }
- if (access == FileAccess::Read)
- throw ArgumentException("Read-only access is incompatible with Create mode.");
- else if (access == FileAccess::ReadWrite)
- {
- mode = L"w+b";
- this->fileAccess = FileAccess::ReadWrite;
- }
- else
- {
- mode = L"wb";
- this->fileAccess = FileAccess::Write;
- }
- break;
- case CoreLib::IO::FileMode::Append:
- if (access == FileAccess::Read)
- throw ArgumentException("Read-only access is incompatible with Append mode.");
- else if (access == FileAccess::ReadWrite)
- {
- mode = L"a+b";
- this->fileAccess = FileAccess::ReadWrite;
- }
- else
- {
- mode = L"ab";
- this->fileAccess = FileAccess::Write;
- }
- break;
- default:
- break;
+ mode = L"w+b";
+ modeMBCS = "w+b";
+ this->fileAccess = FileAccess::ReadWrite;
}
- int shFlag;
-#ifdef _WIN32
- switch (share)
+ else
{
- case CoreLib::IO::FileShare::None:
- shFlag = _SH_DENYRW;
- break;
- case CoreLib::IO::FileShare::ReadOnly:
- shFlag = _SH_DENYWR;
- break;
- case CoreLib::IO::FileShare::WriteOnly:
- shFlag = _SH_DENYRD;
- break;
- case CoreLib::IO::FileShare::ReadWrite:
- shFlag = _SH_DENYNO;
- break;
- default:
- throw ArgumentException("Invalid file share mode.");
- break;
+ mode = L"wb";
+ modeMBCS = "wb";
+ this->fileAccess = FileAccess::Write;
}
- if (share == CoreLib::IO::FileShare::None)
-#pragma warning(suppress:4996)
- handle = _wfopen(fileName.ToWString(), mode);
- else
- handle = _wfsopen(fileName.ToWString(), mode, shFlag);
-#else
- handle = fopen(fileName.Buffer(), modeMBCS);
-#endif
- if (!handle)
+ break;
+ case Slang::FileMode::Open:
+ if (access == FileAccess::Read)
+ {
+ mode = L"rb";
+ modeMBCS = "rb";
+ this->fileAccess = FileAccess::Read;
+ }
+ else if (access == FileAccess::ReadWrite)
+ {
+ mode = L"r+b";
+ modeMBCS = "r+b";
+ this->fileAccess = FileAccess::ReadWrite;
+ }
+ else
{
- throw IOException("Cannot open file '" + fileName + "'");
+ mode = L"wb";
+ modeMBCS = "wb";
+ this->fileAccess = FileAccess::Write;
}
+ break;
+ case Slang::FileMode::CreateNew:
+ if (File::Exists(fileName))
+ {
+ throw IOException("Failed opening '" + fileName + "', file already exists.");
+ }
+ if (access == FileAccess::Read)
+ throw ArgumentException("Read-only access is incompatible with Create mode.");
+ else if (access == FileAccess::ReadWrite)
+ {
+ mode = L"w+b";
+ this->fileAccess = FileAccess::ReadWrite;
+ }
+ else
+ {
+ mode = L"wb";
+ this->fileAccess = FileAccess::Write;
+ }
+ break;
+ case Slang::FileMode::Append:
+ if (access == FileAccess::Read)
+ throw ArgumentException("Read-only access is incompatible with Append mode.");
+ else if (access == FileAccess::ReadWrite)
+ {
+ mode = L"a+b";
+ this->fileAccess = FileAccess::ReadWrite;
+ }
+ else
+ {
+ mode = L"ab";
+ this->fileAccess = FileAccess::Write;
+ }
+ break;
+ default:
+ break;
}
- FileStream::~FileStream()
+ int shFlag;
+#ifdef _WIN32
+ switch (share)
{
- Close();
+ case Slang::FileShare::None:
+ shFlag = _SH_DENYRW;
+ break;
+ case Slang::FileShare::ReadOnly:
+ shFlag = _SH_DENYWR;
+ break;
+ case Slang::FileShare::WriteOnly:
+ shFlag = _SH_DENYRD;
+ break;
+ case Slang::FileShare::ReadWrite:
+ shFlag = _SH_DENYNO;
+ break;
+ default:
+ throw ArgumentException("Invalid file share mode.");
+ break;
}
- Int64 FileStream::GetPosition()
- {
-#ifdef _WIN32
- fpos_t pos;
- fgetpos(handle, &pos);
- return pos;
+ if (share == Slang::FileShare::None)
+#pragma warning(suppress:4996)
+ handle = _wfopen(fileName.ToWString(), mode);
+ else
+ handle = _wfsopen(fileName.ToWString(), mode, shFlag);
#else
- fpos64_t pos;
- fgetpos64(handle, &pos);
- return *(Int64*)(&pos);
+ handle = fopen(fileName.Buffer(), modeMBCS);
#endif
- }
- void FileStream::Seek(SeekOrigin origin, Int64 offset)
+ if (!handle)
{
- int _origin;
- switch (origin)
- {
- case CoreLib::IO::SeekOrigin::Start:
- _origin = SEEK_SET;
- endReached = false;
- break;
- case CoreLib::IO::SeekOrigin::End:
- _origin = SEEK_END;
- endReached = true;
- break;
- case CoreLib::IO::SeekOrigin::Current:
- _origin = SEEK_CUR;
- endReached = false;
- break;
- default:
- throw NotSupportedException("Unsupported seek origin.");
- break;
- }
+ throw IOException("Cannot open file '" + fileName + "'");
+ }
+ }
+ FileStream::~FileStream()
+ {
+ Close();
+ }
+ Int64 FileStream::GetPosition()
+ {
#ifdef _WIN32
- int rs = _fseeki64(handle, offset, _origin);
+ fpos_t pos;
+ fgetpos(handle, &pos);
+ return pos;
#else
- int rs = fseek(handle, (int)offset, _origin);
+ fpos64_t pos;
+ fgetpos64(handle, &pos);
+ return *(Int64*)(&pos);
#endif
- if (rs != 0)
- {
- throw IOException("FileStream seek failed.");
- }
- }
- Int64 FileStream::Read(void * buffer, Int64 length)
- {
- auto bytes = fread_s(buffer, (size_t)length, 1, (size_t)length, handle);
- if (bytes == 0 && length > 0)
- {
- if (!feof(handle))
- throw IOException("FileStream read failed.");
- else if (endReached)
- throw EndOfStreamException("End of file is reached.");
- endReached = true;
- }
- return (int)bytes;
- }
- Int64 FileStream::Write(const void * buffer, Int64 length)
+ }
+ void FileStream::Seek(SeekOrigin origin, Int64 offset)
+ {
+ int _origin;
+ switch (origin)
{
- auto bytes = (Int64)fwrite(buffer, 1, (size_t)length, handle);
- if (bytes < length)
- {
- throw IOException("FileStream write failed.");
- }
- return bytes;
+ case Slang::SeekOrigin::Start:
+ _origin = SEEK_SET;
+ endReached = false;
+ break;
+ case Slang::SeekOrigin::End:
+ _origin = SEEK_END;
+ endReached = true;
+ break;
+ case Slang::SeekOrigin::Current:
+ _origin = SEEK_CUR;
+ endReached = false;
+ break;
+ default:
+ throw NotSupportedException("Unsupported seek origin.");
+ break;
}
- bool FileStream::CanRead()
+#ifdef _WIN32
+ int rs = _fseeki64(handle, offset, _origin);
+#else
+ int rs = fseek(handle, (int)offset, _origin);
+#endif
+ if (rs != 0)
{
- return ((int)fileAccess & (int)FileAccess::Read) != 0;
+ throw IOException("FileStream seek failed.");
}
- bool FileStream::CanWrite()
+ }
+ Int64 FileStream::Read(void * buffer, Int64 length)
+ {
+ auto bytes = fread_s(buffer, (size_t)length, 1, (size_t)length, handle);
+ if (bytes == 0 && length > 0)
{
- return ((int)fileAccess & (int)FileAccess::Write) != 0;
+ if (!feof(handle))
+ throw IOException("FileStream read failed.");
+ else if (endReached)
+ throw EndOfStreamException("End of file is reached.");
+ endReached = true;
}
- void FileStream::Close()
+ return (int)bytes;
+ }
+ Int64 FileStream::Write(const void * buffer, Int64 length)
+ {
+ auto bytes = (Int64)fwrite(buffer, 1, (size_t)length, handle);
+ if (bytes < length)
{
- if (handle)
- {
- fclose(handle);
- handle = 0;
- }
+ throw IOException("FileStream write failed.");
}
- bool FileStream::IsEnd()
+ return bytes;
+ }
+ bool FileStream::CanRead()
+ {
+ return ((int)fileAccess & (int)FileAccess::Read) != 0;
+ }
+ bool FileStream::CanWrite()
+ {
+ return ((int)fileAccess & (int)FileAccess::Write) != 0;
+ }
+ void FileStream::Close()
+ {
+ if (handle)
{
- return endReached;
+ fclose(handle);
+ handle = 0;
}
}
+ bool FileStream::IsEnd()
+ {
+ return endReached;
+ }
}
diff --git a/source/core/stream.h b/source/core/stream.h
index 30d1cc992..1a5f96307 100644
--- a/source/core/stream.h
+++ b/source/core/stream.h
@@ -3,331 +3,324 @@
#include "Basic.h"
-namespace CoreLib
+namespace Slang
{
- namespace IO
+ class IOException : public Exception
{
- using CoreLib::Basic::Exception;
- using CoreLib::Basic::String;
- using CoreLib::Basic::RefPtr;
-
- class IOException : public Exception
+ public:
+ IOException()
+ {}
+ IOException(const String & message)
+ : Slang::Exception(message)
{
- public:
- IOException()
- {}
- IOException(const String & message)
- : CoreLib::Basic::Exception(message)
- {
- }
- };
+ }
+ };
- class EndOfStreamException : public IOException
+ class EndOfStreamException : public IOException
+ {
+ public:
+ EndOfStreamException()
+ {}
+ EndOfStreamException(const String & message)
+ : IOException(message)
{
- public:
- EndOfStreamException()
- {}
- EndOfStreamException(const String & message)
- : IOException(message)
- {
- }
- };
+ }
+ };
- enum class SeekOrigin
- {
- Start, End, Current
- };
+ enum class SeekOrigin
+ {
+ Start, End, Current
+ };
- class Stream : public CoreLib::Basic::Object
- {
- public:
- virtual Int64 GetPosition()=0;
- virtual void Seek(SeekOrigin origin, Int64 offset)=0;
- virtual Int64 Read(void * buffer, Int64 length) = 0;
- virtual Int64 Write(const void * buffer, Int64 length) = 0;
- virtual bool IsEnd() = 0;
- virtual bool CanRead() = 0;
- virtual bool CanWrite() = 0;
- virtual void Close() = 0;
- };
+ class Stream : public Slang::Object
+ {
+ public:
+ virtual Int64 GetPosition()=0;
+ virtual void Seek(SeekOrigin origin, Int64 offset)=0;
+ virtual Int64 Read(void * buffer, Int64 length) = 0;
+ virtual Int64 Write(const void * buffer, Int64 length) = 0;
+ virtual bool IsEnd() = 0;
+ virtual bool CanRead() = 0;
+ virtual bool CanWrite() = 0;
+ virtual void Close() = 0;
+ };
- class BinaryReader
+ class BinaryReader
+ {
+ private:
+ RefPtr<Stream> stream;
+ inline void Throw(Int64 val)
{
- private:
- RefPtr<Stream> stream;
- inline void Throw(Int64 val)
- {
- if (val == 0)
- throw IOException("read operation failed.");
- }
- public:
- BinaryReader(RefPtr<Stream> stream)
- {
- this->stream = stream;
- }
- Stream * GetStream()
- {
- return stream.Ptr();
- }
- void ReleaseStream()
- {
- stream.Release();
- }
- template<typename T>
- void Read(T * buffer, int count)
- {
- stream->Read(buffer, sizeof(T)*(Int64)count);
- }
- template<typename T>
- void Read(T & buffer)
- {
- Throw(stream->Read(&buffer, sizeof(T)));
- }
- template<typename T>
- void Read(List<T> & 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
+ if (val == 0)
+ throw IOException("read operation failed.");
+ }
+ public:
+ BinaryReader(RefPtr<Stream> stream)
{
- private:
- RefPtr<Stream> stream;
- public:
- BinaryWriter(RefPtr<Stream> stream)
- {
- this->stream = stream;
- }
- Stream * GetStream()
- {
- return stream.Ptr();
- }
- template<typename T>
- void Write(const T& val)
- {
- stream->Write(&val, sizeof(T));
- }
- template<typename T>
- void Write(T * buffer, int count)
- {
- stream->Write(buffer, sizeof(T)*(Int64)count);
- }
- template<typename T>
- void Write(const List<T> & 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()
+ this->stream = stream;
+ }
+ Stream * GetStream()
+ {
+ return stream.Ptr();
+ }
+ void ReleaseStream()
+ {
+ stream.Release();
+ }
+ template<typename T>
+ void Read(T * buffer, int count)
+ {
+ stream->Read(buffer, sizeof(T)*(Int64)count);
+ }
+ template<typename T>
+ void Read(T & buffer)
+ {
+ Throw(stream->Read(&buffer, sizeof(T)));
+ }
+ template<typename T>
+ void Read(List<T> & 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
{
- stream.Release();
+ Throw(stream->Read(buffer, len));
}
- void Close()
+ catch(IOException & e)
{
- stream->Close();
+ delete [] buffer;
+ throw e;
}
- };
+ buffer[len] = 0;
+ return String::FromBuffer(buffer, len);
+ }
+ };
- enum class FileMode
+ class BinaryWriter
+ {
+ private:
+ RefPtr<Stream> stream;
+ public:
+ BinaryWriter(RefPtr<Stream> stream)
{
- Create, Open, CreateNew, Append
- };
-
- enum class FileAccess
+ this->stream = stream;
+ }
+ Stream * GetStream()
{
- Read = 1, Write = 2, ReadWrite = 3
- };
-
- enum class FileShare
+ return stream.Ptr();
+ }
+ template<typename T>
+ void Write(const T& val)
+ {
+ stream->Write(&val, sizeof(T));
+ }
+ template<typename T>
+ void Write(T * buffer, int count)
+ {
+ stream->Write(buffer, sizeof(T)*(Int64)count);
+ }
+ template<typename T>
+ void Write(const List<T> & list)
{
- None, ReadOnly, WriteOnly, ReadWrite
- };
+ 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();
+ }
+ };
- class FileStream : public Stream
- {
- private:
- FILE * handle;
- FileAccess fileAccess;
- bool endReached = false;
- void Init(const CoreLib::Basic::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
- public:
- FileStream(const CoreLib::Basic::String & fileName, FileMode fileMode = FileMode::Open);
- FileStream(const CoreLib::Basic::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
- ~FileStream();
- public:
- virtual Int64 GetPosition();
- virtual void Seek(SeekOrigin origin, Int64 offset);
- virtual Int64 Read(void * buffer, Int64 length);
- virtual Int64 Write(const void * buffer, Int64 length);
- virtual bool CanRead();
- virtual bool CanWrite();
- virtual void Close();
- virtual bool IsEnd();
- };
+ enum class FileMode
+ {
+ Create, Open, CreateNew, Append
+ };
- class MemoryStream : public Stream
- {
- private:
- CoreLib::List<unsigned char> writeBuffer;
- CoreLib::ArrayView<unsigned char> readBuffer;
- int ptr = 0;
- bool isReadStream;
- public:
- MemoryStream()
- {
- isReadStream = false;
- }
- MemoryStream(unsigned char * mem, int length)
- {
- isReadStream = true;
- readBuffer = MakeArrayView(mem, length);
- }
- MemoryStream(CoreLib::ArrayView<unsigned char> 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()
+ enum class FileAccess
+ {
+ Read = 1, Write = 2, ReadWrite = 3
+ };
+
+ enum class FileShare
+ {
+ None, ReadOnly, WriteOnly, ReadWrite
+ };
+
+ class FileStream : public Stream
+ {
+ private:
+ FILE * handle;
+ FileAccess fileAccess;
+ bool endReached = false;
+ void Init(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
+ public:
+ FileStream(const Slang::String & fileName, FileMode fileMode = FileMode::Open);
+ FileStream(const Slang::String & fileName, FileMode fileMode, FileAccess access, FileShare share);
+ ~FileStream();
+ public:
+ virtual Int64 GetPosition();
+ virtual void Seek(SeekOrigin origin, Int64 offset);
+ virtual Int64 Read(void * buffer, Int64 length);
+ virtual Int64 Write(const void * buffer, Int64 length);
+ virtual bool CanRead();
+ virtual bool CanWrite();
+ virtual void Close();
+ virtual bool IsEnd();
+ };
+
+ class MemoryStream : public Stream
+ {
+ private:
+ List<unsigned char> writeBuffer;
+ ArrayView<unsigned char> readBuffer;
+ int ptr = 0;
+ bool isReadStream;
+ public:
+ MemoryStream()
+ {
+ isReadStream = false;
+ }
+ MemoryStream(unsigned char * mem, int length)
+ {
+ isReadStream = true;
+ readBuffer = MakeArrayView(mem, length);
+ }
+ MemoryStream(ArrayView<unsigned char> 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)
- return readBuffer.Buffer();
+ ptr = readBuffer.Count() + (int)offset;
else
- return writeBuffer.Buffer();
+ ptr = writeBuffer.Count() + (int)offset;
}
- int GetBufferSize()
+ }
+ virtual Int64 Read(void * pbuffer, Int64 length)
+ {
+ Int64 i;
+ for (i = 0; i < length; i++)
{
- if (isReadStream)
- return readBuffer.Count();
+ if (ptr + i < readBuffer.Count())
+ {
+ ((unsigned char*)pbuffer)[i] = readBuffer[(int)(ptr + i)];
+ }
else
- return writeBuffer.Count();
+ 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.cpp b/source/core/text-io.cpp
index cf75c4762..3e5013cf3 100644
--- a/source/core/text-io.cpp
+++ b/source/core/text-io.cpp
@@ -8,337 +8,332 @@
#define CONVERT_END_OF_LINE
#endif
-namespace CoreLib
+namespace Slang
{
- namespace IO
+ class Utf8Encoding : public Encoding
{
- using namespace CoreLib::Basic;
-
- class Utf8Encoding : public Encoding
+ public:
+ virtual void GetBytes(List<char> & result, const String & str) override
{
- public:
- virtual void GetBytes(List<char> & result, const String & str) override
- {
- result.AddRange(str.Buffer(), str.Length());
- }
- virtual String ToString(const char * bytes, int /*length*/) override
- {
- return String(bytes);
- }
- };
+ result.AddRange(str.Buffer(), str.Length());
+ }
+ virtual String ToString(const char * bytes, int /*length*/) override
+ {
+ return String(bytes);
+ }
+ };
- class Utf32Encoding : public Encoding
+ class Utf32Encoding : public Encoding
+ {
+ public:
+ virtual void GetBytes(List<char> & result, const String & str) override
{
- public:
- virtual void GetBytes(List<char> & result, const String & str) override
+ int ptr = 0;
+ while (ptr < str.Length())
{
- int ptr = 0;
- while (ptr < str.Length())
+ int codePoint = GetUnicodePointFromUTF8([&](int)
{
- int codePoint = GetUnicodePointFromUTF8([&](int)
- {
- if (ptr < str.Length())
- return str[ptr++];
- else
- return '\0';
- });
- result.AddRange((char*)&codePoint, 4);
- }
+ if (ptr < str.Length())
+ return str[ptr++];
+ else
+ return '\0';
+ });
+ result.AddRange((char*)&codePoint, 4);
}
- virtual String ToString(const char * bytes, int length) override
+ }
+ virtual String ToString(const char * bytes, int length) override
+ {
+ StringBuilder sb;
+ int * content = (int*)bytes;
+ for (int i = 0; i < (length >> 2); i++)
{
- StringBuilder sb;
- int * content = (int*)bytes;
- for (int i = 0; i < (length >> 2); i++)
- {
- char buf[5];
- int count = EncodeUnicodePointToUTF8(buf, content[i]);
- for (int j = 0; j < count; j++)
- sb.Append(buf[j]);
- }
- return sb.ProduceString();
+ char buf[5];
+ int count = EncodeUnicodePointToUTF8(buf, content[i]);
+ for (int j = 0; j < count; j++)
+ sb.Append(buf[j]);
}
- };
+ return sb.ProduceString();
+ }
+ };
- class Utf16Encoding : public Encoding //UTF16
+ class Utf16Encoding : public Encoding //UTF16
+ {
+ private:
+ bool reverseOrder = false;
+ public:
+ Utf16Encoding(bool pReverseOrder)
+ : reverseOrder(pReverseOrder)
+ {}
+ virtual void GetBytes(List<char> & result, const String & str) override
{
- private:
- bool reverseOrder = false;
- public:
- Utf16Encoding(bool pReverseOrder)
- : reverseOrder(pReverseOrder)
- {}
- virtual void GetBytes(List<char> & result, const String & str) override
+ int ptr = 0;
+ while (ptr < str.Length())
{
- int ptr = 0;
- while (ptr < str.Length())
+ int codePoint = GetUnicodePointFromUTF8([&](int)
{
- int codePoint = GetUnicodePointFromUTF8([&](int)
- {
- if (ptr < str.Length())
- return str[ptr++];
- else
- return '\0';
- });
- unsigned short buffer[2];
- int count;
- if (!reverseOrder)
- count = EncodeUnicodePointToUTF16(buffer, codePoint);
+ if (ptr < str.Length())
+ return str[ptr++];
else
- count = EncodeUnicodePointToUTF16Reversed(buffer, codePoint);
- result.AddRange((char*)buffer, count * 2);
- }
+ return '\0';
+ });
+ unsigned short buffer[2];
+ int count;
+ if (!reverseOrder)
+ count = EncodeUnicodePointToUTF16(buffer, codePoint);
+ else
+ count = EncodeUnicodePointToUTF16Reversed(buffer, codePoint);
+ result.AddRange((char*)buffer, count * 2);
}
- virtual String ToString(const char * bytes, int length) override
+ }
+ virtual String ToString(const char * bytes, int length) override
+ {
+ int ptr = 0;
+ StringBuilder sb;
+ while (ptr < length)
{
- int ptr = 0;
- StringBuilder sb;
- while (ptr < length)
+ int codePoint = GetUnicodePointFromUTF16([&](int)
{
- int codePoint = GetUnicodePointFromUTF16([&](int)
- {
- if (ptr < length)
- return bytes[ptr++];
- else
- return '\0';
- });
- char buf[5];
- int count = EncodeUnicodePointToUTF8(buf, codePoint);
- for (int i = 0; i < count; i++)
- sb.Append(buf[i]);
- }
- return sb.ProduceString();
+ if (ptr < length)
+ return bytes[ptr++];
+ else
+ return '\0';
+ });
+ char buf[5];
+ int count = EncodeUnicodePointToUTF8(buf, codePoint);
+ for (int i = 0; i < count; i++)
+ sb.Append(buf[i]);
}
- };
+ return sb.ProduceString();
+ }
+ };
- Utf8Encoding __utf8Encoding;
- Utf16Encoding __utf16Encoding(false);
- Utf16Encoding __utf16EncodingReversed(true);
- Utf32Encoding __utf32Encoding;
+ Utf8Encoding __utf8Encoding;
+ Utf16Encoding __utf16Encoding(false);
+ Utf16Encoding __utf16EncodingReversed(true);
+ Utf32Encoding __utf32Encoding;
- Encoding * Encoding::UTF8 = &__utf8Encoding;
- Encoding * Encoding::UTF16 = &__utf16Encoding;
- Encoding * Encoding::UTF16Reversed = &__utf16EncodingReversed;
- Encoding * Encoding::UTF32 = &__utf32Encoding;
+ Encoding * Encoding::UTF8 = &__utf8Encoding;
+ Encoding * Encoding::UTF16 = &__utf16Encoding;
+ Encoding * Encoding::UTF16Reversed = &__utf16EncodingReversed;
+ Encoding * Encoding::UTF32 = &__utf32Encoding;
- const unsigned short Utf16Header = 0xFEFF;
- const unsigned short Utf16ReversedHeader = 0xFFFE;
+ const unsigned short Utf16Header = 0xFEFF;
+ const unsigned short Utf16ReversedHeader = 0xFFFE;
- StreamWriter::StreamWriter(const String & path, Encoding * encoding)
+ StreamWriter::StreamWriter(const String & path, Encoding * encoding)
+ {
+ this->stream = new FileStream(path, FileMode::Create);
+ this->encoding = encoding;
+ if (encoding == Encoding::UTF16)
{
- this->stream = new FileStream(path, FileMode::Create);
- this->encoding = encoding;
- if (encoding == Encoding::UTF16)
- {
- this->stream->Write(&Utf16Header, 2);
- }
- else if (encoding == Encoding::UTF16Reversed)
- {
- this->stream->Write(&Utf16ReversedHeader, 2);
- }
+ this->stream->Write(&Utf16Header, 2);
}
- StreamWriter::StreamWriter(RefPtr<Stream> stream, Encoding * encoding)
+ else if (encoding == Encoding::UTF16Reversed)
{
- this->stream = stream;
- this->encoding = encoding;
- if (encoding == Encoding::UTF16)
- {
- this->stream->Write(&Utf16Header, 2);
- }
- else if (encoding == Encoding::UTF16Reversed)
- {
- this->stream->Write(&Utf16ReversedHeader, 2);
- }
+ this->stream->Write(&Utf16ReversedHeader, 2);
}
- void StreamWriter::Write(const String & str)
+ }
+ StreamWriter::StreamWriter(RefPtr<Stream> stream, Encoding * encoding)
+ {
+ this->stream = stream;
+ this->encoding = encoding;
+ if (encoding == Encoding::UTF16)
{
- encodingBuffer.Clear();
- StringBuilder sb;
- String newLine;
+ this->stream->Write(&Utf16Header, 2);
+ }
+ else if (encoding == Encoding::UTF16Reversed)
+ {
+ this->stream->Write(&Utf16ReversedHeader, 2);
+ }
+ }
+ void StreamWriter::Write(const String & str)
+ {
+ encodingBuffer.Clear();
+ StringBuilder sb;
+ String newLine;
#ifdef _WIN32
- newLine = "\r\n";
+ newLine = "\r\n";
#else
- newLine = "\n";
+ newLine = "\n";
#endif
- for (int i = 0; i < str.Length(); i++)
+ for (int i = 0; i < str.Length(); i++)
+ {
+ if (str[i] == '\r')
+ sb << newLine;
+ else if (str[i] == '\n')
{
- if (str[i] == '\r')
+ if (i > 0 && str[i - 1] != '\r')
sb << newLine;
- else if (str[i] == '\n')
- {
- if (i > 0 && str[i - 1] != '\r')
- sb << newLine;
- }
- else
- sb << str[i];
}
- encoding->GetBytes(encodingBuffer, sb.ProduceString());
- stream->Write(encodingBuffer.Buffer(), encodingBuffer.Count());
+ else
+ sb << str[i];
}
- void StreamWriter::Write(const char * str)
+ encoding->GetBytes(encodingBuffer, sb.ProduceString());
+ stream->Write(encodingBuffer.Buffer(), encodingBuffer.Count());
+ }
+ void StreamWriter::Write(const char * str)
+ {
+ Write(String(str));
+ }
+
+ StreamReader::StreamReader(const String & path)
+ {
+ stream = new FileStream(path, FileMode::Open);
+ ReadBuffer();
+ encoding = DetermineEncoding();
+ if (encoding == 0)
+ encoding = Encoding::UTF8;
+ }
+ StreamReader::StreamReader(RefPtr<Stream> stream, Encoding * encoding)
+ {
+ this->stream = stream;
+ this->encoding = encoding;
+ ReadBuffer();
+ auto determinedEncoding = DetermineEncoding();
+ if (this->encoding == nullptr)
+ this->encoding = determinedEncoding;
+ }
+
+ Encoding * StreamReader::DetermineEncoding()
+ {
+ if (buffer.Count() >= 3 && (unsigned char)(buffer[0]) == 0xEF && (unsigned char)(buffer[1]) == 0xBB && (unsigned char)(buffer[2]) == 0xBF)
{
- Write(String(str));
+ ptr += 3;
+ return Encoding::UTF8;
}
-
- StreamReader::StreamReader(const String & path)
+ else if (*((unsigned short*)(buffer.Buffer())) == 0xFEFF)
{
- stream = new FileStream(path, FileMode::Open);
- ReadBuffer();
- encoding = DetermineEncoding();
- if (encoding == 0)
- encoding = Encoding::UTF8;
+ ptr += 2;
+ return Encoding::UTF16;
}
- StreamReader::StreamReader(RefPtr<Stream> stream, Encoding * encoding)
+ else if (*((unsigned short*)(buffer.Buffer())) == 0xFFFE)
{
- this->stream = stream;
- this->encoding = encoding;
- ReadBuffer();
- auto determinedEncoding = DetermineEncoding();
- if (this->encoding == nullptr)
- this->encoding = determinedEncoding;
+ ptr += 2;
+ return Encoding::UTF16Reversed;
}
-
- Encoding * StreamReader::DetermineEncoding()
+ else
{
- if (buffer.Count() >= 3 && (unsigned char)(buffer[0]) == 0xEF && (unsigned char)(buffer[1]) == 0xBB && (unsigned char)(buffer[2]) == 0xBF)
- {
- ptr += 3;
- return Encoding::UTF8;
- }
- else if (*((unsigned short*)(buffer.Buffer())) == 0xFEFF)
- {
- ptr += 2;
- return Encoding::UTF16;
- }
- else if (*((unsigned short*)(buffer.Buffer())) == 0xFFFE)
+#ifdef _WIN32
+ int flag = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_ASCII16;
+ int rs = IsTextUnicode(buffer.Buffer(), buffer.Count(), &flag);
+ if (rs)
{
- ptr += 2;
- return Encoding::UTF16Reversed;
+ if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS))
+ return Encoding::UTF16;
+ else if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS))
+ return Encoding::UTF16Reversed;
+ else if (flag & IS_TEXT_UNICODE_ASCII16)
+ return Encoding::UTF8;
}
- else
- {
-#ifdef _WIN32
- int flag = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_ASCII16;
- int rs = IsTextUnicode(buffer.Buffer(), buffer.Count(), &flag);
- if (rs)
- {
- if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS))
- return Encoding::UTF16;
- else if (flag & (IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_STATISTICS))
- return Encoding::UTF16Reversed;
- else if (flag & IS_TEXT_UNICODE_ASCII16)
- return Encoding::UTF8;
- }
#endif
- return Encoding::UTF8;
- }
+ return Encoding::UTF8;
}
+ }
- void StreamReader::ReadBuffer()
+ void StreamReader::ReadBuffer()
+ {
+ buffer.SetSize(4096);
+ auto len = stream->Read(buffer.Buffer(), buffer.Count());
+ buffer.SetSize((int)len);
+ ptr = 0;
+ }
+
+ char StreamReader::ReadBufferChar()
+ {
+ if (ptr<buffer.Count())
{
- buffer.SetSize(4096);
- auto len = stream->Read(buffer.Buffer(), buffer.Count());
- buffer.SetSize((int)len);
- ptr = 0;
+ return buffer[ptr++];
}
-
- char StreamReader::ReadBufferChar()
+ if (!stream->IsEnd())
+ ReadBuffer();
+ if (ptr<buffer.Count())
{
- if (ptr<buffer.Count())
- {
- return buffer[ptr++];
- }
- if (!stream->IsEnd())
- ReadBuffer();
- if (ptr<buffer.Count())
- {
- return buffer[ptr++];
- }
- return 0;
+ return buffer[ptr++];
}
- int TextReader::Read(char * destBuffer, int length)
+ return 0;
+ }
+ int TextReader::Read(char * destBuffer, int length)
+ {
+ int i = 0;
+ for (i = 0; i<length; i++)
{
- int i = 0;
- for (i = 0; i<length; i++)
+ try
{
- try
+ auto ch = Read();
+ if (IsEnd())
+ break;
+ if (ch == '\r')
{
- auto ch = Read();
- if (IsEnd())
- break;
- if (ch == '\r')
- {
- if (Peak() == '\n')
- Read();
- break;
- }
- else if (ch == '\n')
- {
- break;
- }
- destBuffer[i] = ch;
+ if (Peak() == '\n')
+ Read();
+ break;
}
- catch (EndOfStreamException)
+ else if (ch == '\n')
{
break;
}
+ destBuffer[i] = ch;
+ }
+ catch (EndOfStreamException)
+ {
+ break;
}
- return i;
}
- String StreamReader::ReadLine()
+ return i;
+ }
+ String StreamReader::ReadLine()
+ {
+ StringBuilder sb(256);
+ while (!IsEnd())
{
- StringBuilder sb(256);
- while (!IsEnd())
+ try
{
- try
+ auto ch = Read();
+ if (IsEnd())
+ break;
+ if (ch == '\r')
{
- auto ch = Read();
- if (IsEnd())
- break;
- if (ch == '\r')
- {
- if (Peak() == '\n')
- Read();
- break;
- }
- else if (ch == '\n')
- {
- break;
- }
- sb.Append(ch);
+ if (Peak() == '\n')
+ Read();
+ break;
}
- catch (EndOfStreamException)
+ else if (ch == '\n')
{
break;
}
+ sb.Append(ch);
+ }
+ catch (EndOfStreamException)
+ {
+ break;
}
- return sb.ProduceString();
}
- String StreamReader::ReadToEnd()
+ return sb.ProduceString();
+ }
+ String StreamReader::ReadToEnd()
+ {
+ StringBuilder sb(16384);
+ while (!IsEnd())
{
- StringBuilder sb(16384);
- while (!IsEnd())
+ try
{
- try
- {
- auto ch = Read();
- if (IsEnd())
- break;
- if (ch == '\r')
- {
- sb.Append('\n');
- if (Peak() == '\n')
- Read();
- }
- else
- sb.Append(ch);
- }
- catch (EndOfStreamException)
- {
+ auto ch = Read();
+ if (IsEnd())
break;
+ if (ch == '\r')
+ {
+ sb.Append('\n');
+ if (Peak() == '\n')
+ Read();
}
+ else
+ sb.Append(ch);
+ }
+ catch (EndOfStreamException)
+ {
+ break;
}
- return sb.ProduceString();
}
+ return sb.ProduceString();
}
-} \ No newline at end of file
+}
diff --git a/source/core/text-io.h b/source/core/text-io.h
index 8c82177cb..e0881cb91 100644
--- a/source/core/text-io.h
+++ b/source/core/text-io.h
@@ -4,317 +4,314 @@
#include "secure-crt.h"
#include "stream.h"
-namespace CoreLib
+namespace Slang
{
- namespace IO
- {
- using CoreLib::Basic::List;
- using CoreLib::Basic::_EndLine;
+ using Slang::List;
+ using Slang::_EndLine;
- class TextReader : public CoreLib::Basic::Object
+ class TextReader : public Slang::Object
+ {
+ protected:
+ char decodedChar[5];
+ int decodedCharPtr = 0, decodedCharSize = 0;
+ virtual void ReadChar() = 0;
+ public:
+ ~TextReader()
{
- protected:
- char decodedChar[5];
- int decodedCharPtr = 0, decodedCharSize = 0;
- virtual void ReadChar() = 0;
- public:
- ~TextReader()
- {
- Close();
- }
- virtual void Close(){}
- virtual String ReadLine()=0;
- virtual String ReadToEnd()=0;
- virtual bool IsEnd() = 0;
- int Read(char * buffer, int count);
- char Read()
- {
- if (decodedCharPtr == decodedCharSize)
- ReadChar();
- if (decodedCharPtr < decodedCharSize)
- return decodedChar[decodedCharPtr++];
- else
- return 0;
- }
- char Peak()
- {
- if (decodedCharPtr == decodedCharSize)
- ReadChar();
- if (decodedCharPtr < decodedCharSize)
- return decodedChar[decodedCharPtr];
- else
- return 0;
- }
- };
+ Close();
+ }
+ virtual void Close(){}
+ virtual String ReadLine()=0;
+ virtual String ReadToEnd()=0;
+ virtual bool IsEnd() = 0;
+ int Read(char * buffer, int count);
+ char Read()
+ {
+ if (decodedCharPtr == decodedCharSize)
+ ReadChar();
+ if (decodedCharPtr < decodedCharSize)
+ return decodedChar[decodedCharPtr++];
+ else
+ return 0;
+ }
+ char Peak()
+ {
+ if (decodedCharPtr == decodedCharSize)
+ ReadChar();
+ if (decodedCharPtr < decodedCharSize)
+ return decodedChar[decodedCharPtr];
+ else
+ return 0;
+ }
+ };
- class TextWriter : public CoreLib::Basic::Object
+ class TextWriter : public Slang::Object
+ {
+ public:
+ ~TextWriter()
+ {
+ Close();
+ }
+ virtual void Write(const String & str)=0;
+ virtual void Write(const char * str)=0;
+ virtual void Close(){}
+ template<typename T>
+ TextWriter & operator << (const T& val)
+ {
+ Write(val.ToString());
+ return *this;
+ }
+ TextWriter & operator << (int value)
+ {
+ Write(String(value));
+ return *this;
+ }
+ TextWriter & operator << (float value)
+ {
+ Write(String(value));
+ return *this;
+ }
+ TextWriter & operator << (double value)
+ {
+ Write(String(value));
+ return *this;
+ }
+ TextWriter & operator << (const char* value)
+ {
+ Write(value);
+ return *this;
+ }
+ TextWriter & operator << (const String & val)
+ {
+ Write(val);
+ return *this;
+ }
+ TextWriter & operator << (const _EndLine &)
{
- public:
- ~TextWriter()
- {
- Close();
- }
- virtual void Write(const String & str)=0;
- virtual void Write(const char * str)=0;
- virtual void Close(){}
- template<typename T>
- TextWriter & operator << (const T& val)
- {
- Write(val.ToString());
- return *this;
- }
- TextWriter & operator << (int value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (float value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (double value)
- {
- Write(String(value));
- return *this;
- }
- TextWriter & operator << (const char* value)
- {
- Write(value);
- return *this;
- }
- TextWriter & operator << (const String & val)
- {
- Write(val);
- return *this;
- }
- TextWriter & operator << (const _EndLine &)
- {
#ifdef _WIN32
- Write("\r\n");
+ Write("\r\n");
#else
- Write("\n");
+ Write("\n");
#endif
- return *this;
- }
- };
+ return *this;
+ }
+ };
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF8(const ReadCharFunc & get)
+ template <typename ReadCharFunc>
+ int GetUnicodePointFromUTF8(const ReadCharFunc & get)
+ {
+ int codePoint = 0;
+ int leading = get(0);
+ int mask = 0x80;
+ int count = 0;
+ while (leading & mask)
{
- int codePoint = 0;
- int leading = get(0);
- int mask = 0x80;
- int count = 0;
- while (leading & mask)
- {
- count++;
- mask >>= 1;
- }
- codePoint = (leading & (mask - 1));
- for (int i = 1; i <= count - 1; i++)
- {
- codePoint <<= 6;
- codePoint += (get(i) & 0x3F);
- }
- return codePoint;
+ count++;
+ mask >>= 1;
}
-
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF16(const ReadCharFunc & get)
+ codePoint = (leading & (mask - 1));
+ for (int i = 1; i <= count - 1; i++)
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
- int word0 = byte0 + (byte1 << 8);
- if (word0 >= 0xD800 && word0 <= 0xDFFF)
- {
- int byte2 = (unsigned char)get(2);
- int byte3 = (unsigned char)get(3);
- int word1 = byte2 + (byte3 << 8);
- return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF) + 0x10000;
- }
- else
- return word0;
+ codePoint <<= 6;
+ codePoint += (get(i) & 0x3F);
}
+ return codePoint;
+ }
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF16Reversed(const ReadCharFunc & get)
+ template <typename ReadCharFunc>
+ int GetUnicodePointFromUTF16(const ReadCharFunc & get)
+ {
+ int byte0 = (unsigned char)get(0);
+ int byte1 = (unsigned char)get(1);
+ int word0 = byte0 + (byte1 << 8);
+ if (word0 >= 0xD800 && word0 <= 0xDFFF)
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
- int word0 = (byte0 << 8) + byte1;
- if (word0 >= 0xD800 && word0 <= 0xDFFF)
- {
- int byte2 = (unsigned char)get(2);
- int byte3 = (unsigned char)get(3);
- int word1 = (byte2 << 8) + byte3;
- return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF);
- }
- else
- return word0;
+ int byte2 = (unsigned char)get(2);
+ int byte3 = (unsigned char)get(3);
+ int word1 = byte2 + (byte3 << 8);
+ return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF) + 0x10000;
}
+ else
+ return word0;
+ }
- template <typename ReadCharFunc>
- int GetUnicodePointFromUTF32(const ReadCharFunc & get)
+ template <typename ReadCharFunc>
+ int GetUnicodePointFromUTF16Reversed(const ReadCharFunc & get)
+ {
+ int byte0 = (unsigned char)get(0);
+ int byte1 = (unsigned char)get(1);
+ int word0 = (byte0 << 8) + byte1;
+ if (word0 >= 0xD800 && word0 <= 0xDFFF)
{
- int byte0 = (unsigned char)get(0);
- int byte1 = (unsigned char)get(1);
int byte2 = (unsigned char)get(2);
int byte3 = (unsigned char)get(3);
- return byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
+ int word1 = (byte2 << 8) + byte3;
+ return ((word0 & 0x3FF) << 10) + (word1 & 0x3FF);
}
+ else
+ return word0;
+ }
+
+ template <typename ReadCharFunc>
+ int GetUnicodePointFromUTF32(const ReadCharFunc & get)
+ {
+ int byte0 = (unsigned char)get(0);
+ int byte1 = (unsigned char)get(1);
+ int byte2 = (unsigned char)get(2);
+ int byte3 = (unsigned char)get(3);
+ return byte0 + (byte1 << 8) + (byte2 << 16) + (byte3 << 24);
+ }
- inline int EncodeUnicodePointToUTF8(char * buffer, int codePoint)
+ inline int EncodeUnicodePointToUTF8(char * buffer, int codePoint)
+ {
+ int count = 0;
+ if (codePoint <= 0x7F)
+ buffer[count++] = ((char)codePoint);
+ else if (codePoint <= 0x7FF)
{
- int count = 0;
- if (codePoint <= 0x7F)
- buffer[count++] = ((char)codePoint);
- else if (codePoint <= 0x7FF)
- {
- unsigned char byte = (unsigned char)(0xC0 + (codePoint >> 6));
- buffer[count++] = ((char)byte);
- byte = 0x80 + (codePoint & 0x3F);
- buffer[count++] = ((char)byte);
- }
- else if (codePoint <= 0xFFFF)
- {
- unsigned char byte = (unsigned char)(0xE0 + (codePoint >> 12));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 6) & (0x3F)));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + (codePoint & 0x3F));
- buffer[count++] = ((char)byte);
- }
- else
- {
- unsigned char byte = (unsigned char)(0xF0 + (codePoint >> 18));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 12) & 0x3F));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + ((codePoint >> 6) & 0x3F));
- buffer[count++] = ((char)byte);
- byte = (unsigned char)(0x80 + (codePoint & 0x3F));
- buffer[count++] = ((char)byte);
- }
- return count;
+ unsigned char byte = (unsigned char)(0xC0 + (codePoint >> 6));
+ buffer[count++] = ((char)byte);
+ byte = 0x80 + (codePoint & 0x3F);
+ buffer[count++] = ((char)byte);
}
-
- inline int EncodeUnicodePointToUTF16(unsigned short * buffer, int codePoint)
+ else if (codePoint <= 0xFFFF)
{
- int count = 0;
- if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
- buffer[count++] = (unsigned short)codePoint;
- else
- {
- int sub = codePoint - 0x10000;
- int high = (sub >> 10) + 0xD800;
- int low = (sub & 0x3FF) + 0xDC00;
- buffer[count++] = (unsigned short)high;
- buffer[count++] = (unsigned short)low;
- }
- return count;
+ unsigned char byte = (unsigned char)(0xE0 + (codePoint >> 12));
+ buffer[count++] = ((char)byte);
+ byte = (unsigned char)(0x80 + ((codePoint >> 6) & (0x3F)));
+ buffer[count++] = ((char)byte);
+ byte = (unsigned char)(0x80 + (codePoint & 0x3F));
+ buffer[count++] = ((char)byte);
}
-
- inline unsigned short ReverseBitOrder(unsigned short val)
+ else
{
- int byte0 = val & 0xFF;
- int byte1 = val >> 8;
- return (unsigned short)(byte1 + (byte0 << 8));
+ unsigned char byte = (unsigned char)(0xF0 + (codePoint >> 18));
+ buffer[count++] = ((char)byte);
+ byte = (unsigned char)(0x80 + ((codePoint >> 12) & 0x3F));
+ buffer[count++] = ((char)byte);
+ byte = (unsigned char)(0x80 + ((codePoint >> 6) & 0x3F));
+ buffer[count++] = ((char)byte);
+ byte = (unsigned char)(0x80 + (codePoint & 0x3F));
+ buffer[count++] = ((char)byte);
}
+ return count;
+ }
- inline int EncodeUnicodePointToUTF16Reversed(unsigned short * buffer, int codePoint)
+ inline int EncodeUnicodePointToUTF16(unsigned short * buffer, int codePoint)
+ {
+ int count = 0;
+ if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
+ buffer[count++] = (unsigned short)codePoint;
+ else
{
- int count = 0;
- if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
- buffer[count++] = ReverseBitOrder((unsigned short)codePoint);
- else
- {
- int sub = codePoint - 0x10000;
- int high = (sub >> 10) + 0xD800;
- int low = (sub & 0x3FF) + 0xDC00;
- buffer[count++] = ReverseBitOrder((unsigned short)high);
- buffer[count++] = ReverseBitOrder((unsigned short)low);
- }
- return count;
+ int sub = codePoint - 0x10000;
+ int high = (sub >> 10) + 0xD800;
+ int low = (sub & 0x3FF) + 0xDC00;
+ buffer[count++] = (unsigned short)high;
+ buffer[count++] = (unsigned short)low;
}
+ return count;
+ }
- class Encoding
- {
- public:
- static Encoding * UTF8, * UTF16, *UTF16Reversed, * UTF32;
- virtual void GetBytes(List<char>& buffer, const String & str) = 0;
- virtual String ToString(const char * buffer, int length) = 0;
- virtual ~Encoding()
- {}
- };
+ inline unsigned short ReverseBitOrder(unsigned short val)
+ {
+ int byte0 = val & 0xFF;
+ int byte1 = val >> 8;
+ return (unsigned short)(byte1 + (byte0 << 8));
+ }
- class StreamWriter : public TextWriter
+ inline int EncodeUnicodePointToUTF16Reversed(unsigned short * buffer, int codePoint)
+ {
+ int count = 0;
+ if (codePoint <= 0xD7FF || (codePoint >= 0xE000 && codePoint <= 0xFFFF))
+ buffer[count++] = ReverseBitOrder((unsigned short)codePoint);
+ else
{
- private:
- List<char> encodingBuffer;
- RefPtr<Stream> stream;
- Encoding * encoding;
- public:
- StreamWriter(const String & path, Encoding * encoding = Encoding::UTF8);
- StreamWriter(RefPtr<Stream> stream, Encoding * encoding = Encoding::UTF8);
- virtual void Write(const String & str);
- virtual void Write(const char * str);
- virtual void Close()
- {
- stream->Close();
- }
- void ReleaseStream()
- {
- stream.Release();
- }
- };
+ int sub = codePoint - 0x10000;
+ int high = (sub >> 10) + 0xD800;
+ int low = (sub & 0x3FF) + 0xDC00;
+ buffer[count++] = ReverseBitOrder((unsigned short)high);
+ buffer[count++] = ReverseBitOrder((unsigned short)low);
+ }
+ return count;
+ }
+
+ class Encoding
+ {
+ public:
+ static Encoding * UTF8, * UTF16, *UTF16Reversed, * UTF32;
+ virtual void GetBytes(List<char>& buffer, const String & str) = 0;
+ virtual String ToString(const char * buffer, int length) = 0;
+ virtual ~Encoding()
+ {}
+ };
- class StreamReader : public TextReader
+ class StreamWriter : public TextWriter
+ {
+ private:
+ List<char> encodingBuffer;
+ RefPtr<Stream> stream;
+ Encoding * encoding;
+ public:
+ StreamWriter(const String & path, Encoding * encoding = Encoding::UTF8);
+ StreamWriter(RefPtr<Stream> stream, Encoding * encoding = Encoding::UTF8);
+ virtual void Write(const String & str);
+ virtual void Write(const char * str);
+ virtual void Close()
{
- private:
- RefPtr<Stream> stream;
- List<char> buffer;
- Encoding * encoding;
- int ptr;
- char ReadBufferChar();
- void ReadBuffer();
+ stream->Close();
+ }
+ void ReleaseStream()
+ {
+ stream.Release();
+ }
+ };
+
+ class StreamReader : public TextReader
+ {
+ private:
+ RefPtr<Stream> stream;
+ List<char> buffer;
+ Encoding * encoding;
+ int ptr;
+ char ReadBufferChar();
+ void ReadBuffer();
- Encoding * DetermineEncoding();
- protected:
- virtual void ReadChar()
- {
- decodedCharPtr = 0;
- int codePoint = 0;
- if (encoding == Encoding::UTF8)
- codePoint = GetUnicodePointFromUTF8([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF16)
- codePoint = GetUnicodePointFromUTF16([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF16Reversed)
- codePoint = GetUnicodePointFromUTF16Reversed([&](int) {return ReadBufferChar(); });
- else if (encoding == Encoding::UTF32)
- codePoint = GetUnicodePointFromUTF32([&](int) {return ReadBufferChar(); });
- decodedCharSize = EncodeUnicodePointToUTF8(decodedChar, codePoint);
- }
- public:
- StreamReader(const String & path);
- StreamReader(RefPtr<Stream> stream, Encoding * encoding = nullptr);
- virtual String ReadLine();
- virtual String ReadToEnd();
- virtual bool IsEnd()
- {
- return ptr == buffer.Count() && stream->IsEnd();
- }
- virtual void Close()
- {
- stream->Close();
- }
- void ReleaseStream()
- {
- stream.Release();
- }
- };
+ Encoding * DetermineEncoding();
+ protected:
+ virtual void ReadChar()
+ {
+ decodedCharPtr = 0;
+ int codePoint = 0;
+ if (encoding == Encoding::UTF8)
+ codePoint = GetUnicodePointFromUTF8([&](int) {return ReadBufferChar(); });
+ else if (encoding == Encoding::UTF16)
+ codePoint = GetUnicodePointFromUTF16([&](int) {return ReadBufferChar(); });
+ else if (encoding == Encoding::UTF16Reversed)
+ codePoint = GetUnicodePointFromUTF16Reversed([&](int) {return ReadBufferChar(); });
+ else if (encoding == Encoding::UTF32)
+ codePoint = GetUnicodePointFromUTF32([&](int) {return ReadBufferChar(); });
+ decodedCharSize = EncodeUnicodePointToUTF8(decodedChar, codePoint);
+ }
+ public:
+ StreamReader(const String & path);
+ StreamReader(RefPtr<Stream> stream, Encoding * encoding = nullptr);
+ virtual String ReadLine();
+ virtual String ReadToEnd();
+ virtual bool IsEnd()
+ {
+ return ptr == buffer.Count() && stream->IsEnd();
+ }
+ virtual void Close()
+ {
+ stream->Close();
+ }
+ void ReleaseStream()
+ {
+ stream.Release();
+ }
+ };
- }
}
#endif
diff --git a/source/core/type-traits.h b/source/core/type-traits.h
index 5dd81965d..5a05538d3 100644
--- a/source/core/type-traits.h
+++ b/source/core/type-traits.h
@@ -1,49 +1,46 @@
#ifndef CORELIB_TYPETRAITS_H
#define CORELIB_TYPETRAITS_H
-namespace CoreLib
+namespace Slang
{
- namespace Basic
+ struct TraitResultYes
{
- struct TraitResultYes
- {
- char x;
- };
- struct TraitResultNo
- {
- char x[2];
- };
+ char x;
+ };
+ struct TraitResultNo
+ {
+ char x[2];
+ };
- template <typename B, typename D>
- struct IsBaseOfTraitHost
- {
- operator B*() const { return nullptr; }
- operator D*() { return nullptr; }
- };
+ template <typename B, typename D>
+ struct IsBaseOfTraitHost
+ {
+ operator B*() const { return nullptr; }
+ operator D*() { return nullptr; }
+ };
- template <typename B, typename D>
- struct IsBaseOf
- {
- template <typename T>
- static TraitResultYes Check(D*, T) { return TraitResultYes(); }
- static TraitResultNo Check(B*, int) { return TraitResultNo(); }
- enum { Value = sizeof(Check(IsBaseOfTraitHost<B, D>(), int())) == sizeof(TraitResultYes) };
- };
+ template <typename B, typename D>
+ struct IsBaseOf
+ {
+ template <typename T>
+ static TraitResultYes Check(D*, T) { return TraitResultYes(); }
+ static TraitResultNo Check(B*, int) { return TraitResultNo(); }
+ enum { Value = sizeof(Check(IsBaseOfTraitHost<B, D>(), int())) == sizeof(TraitResultYes) };
+ };
- template<bool B, class T = void>
- struct EnableIf {};
+ template<bool B, class T = void>
+ struct EnableIf {};
- template<class T>
- struct EnableIf<true, T> { typedef T type; };
+ template<class T>
+ struct EnableIf<true, T> { typedef T type; };
- template <typename B, typename D>
- struct IsConvertible
- {
- static TraitResultYes Use(B) {};
- static TraitResultNo Use(...) {};
- enum { Value = sizeof(Use(*(D*)(nullptr))) == sizeof(TraitResultYes) };
- };
- }
+ template <typename B, typename D>
+ struct IsConvertible
+ {
+ static TraitResultYes Use(B) {};
+ static TraitResultNo Use(...) {};
+ enum { Value = sizeof(Use(*(D*)(nullptr))) == sizeof(TraitResultYes) };
+ };
}
#endif
diff --git a/source/slang/compiler.cpp b/source/slang/compiler.cpp
index d8298c604..6db04b900 100644
--- a/source/slang/compiler.cpp
+++ b/source/slang/compiler.cpp
@@ -30,9 +30,6 @@
#undef CreateDirectory
#endif
-using namespace CoreLib::Basic;
-using namespace CoreLib::IO;
-
namespace Slang
{
//
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index fe69a7bf8..3b6696647 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -154,7 +154,7 @@ namespace Slang
#if 0
- class ShaderCompiler : public CoreLib::Basic::Object
+ class ShaderCompiler : public Slang::Object
{
public:
virtual void Compile(
diff --git a/source/slang/diagnostics.cpp b/source/slang/diagnostics.cpp
index 11f54e096..e9c7e965a 100644
--- a/source/slang/diagnostics.cpp
+++ b/source/slang/diagnostics.cpp
@@ -26,7 +26,7 @@ void printDiagnosticArg(StringBuilder& sb, int str)
sb << str;
}
-void printDiagnosticArg(StringBuilder& sb, CoreLib::Basic::String const& str)
+void printDiagnosticArg(StringBuilder& sb, Slang::String const& str)
{
sb << str;
}
diff --git a/source/slang/diagnostics.h b/source/slang/diagnostics.h
index c4a9532ce..4c698f95b 100644
--- a/source/slang/diagnostics.h
+++ b/source/slang/diagnostics.h
@@ -10,8 +10,6 @@
namespace Slang
{
- using namespace CoreLib::Basic;
-
enum class Severity
{
Note,
@@ -79,7 +77,7 @@ namespace Slang
void printDiagnosticArg(StringBuilder& sb, char const* str);
void printDiagnosticArg(StringBuilder& sb, int val);
- void printDiagnosticArg(StringBuilder& sb, CoreLib::Basic::String const& str);
+ void printDiagnosticArg(StringBuilder& sb, Slang::String const& str);
void printDiagnosticArg(StringBuilder& sb, Decl* decl);
void printDiagnosticArg(StringBuilder& sb, Type* type);
void printDiagnosticArg(StringBuilder& sb, ExpressionType* type);
diff --git a/source/slang/emit.h b/source/slang/emit.h
index 1cb8d2d81..4f3e98c8d 100644
--- a/source/slang/emit.h
+++ b/source/slang/emit.h
@@ -8,8 +8,6 @@
namespace Slang
{
- using namespace CoreLib::Basic;
-
class ProgramSyntaxNode;
class ProgramLayout;
diff --git a/source/slang/lexer.h b/source/slang/lexer.h
index d599b3b7f..5bcfc0f4a 100644
--- a/source/slang/lexer.h
+++ b/source/slang/lexer.h
@@ -6,8 +6,6 @@
namespace Slang
{
- using namespace CoreLib::Basic;
-
struct TokenList
{
Token* begin() const;
diff --git a/source/slang/preprocessor.cpp b/source/slang/preprocessor.cpp
index 3bba307db..b8a42d46d 100644
--- a/source/slang/preprocessor.cpp
+++ b/source/slang/preprocessor.cpp
@@ -10,8 +10,6 @@
#include <assert.h>
-using namespace CoreLib;
-
// This file provides an implementation of a simple C-style preprocessor.
// It does not aim for 100% compatibility with any particular preprocessor
// specification, but the goal is to have it accept the most common
@@ -207,7 +205,7 @@ static void DestroyInputStream(Preprocessor* /*preprocessor*/, PreprocessorInput
// Create an input stream to represent a pre-tokenized input file.
// TODO(tfoley): pre-tokenizing files isn't going to work in the long run.
-static PreprocessorInputStream* CreateInputStreamForSource(Preprocessor* preprocessor, CoreLib::String const& source, CoreLib::String const& fileName)
+static PreprocessorInputStream* CreateInputStreamForSource(Preprocessor* preprocessor, String const& source, String const& fileName)
{
SourceTextInputStream* inputStream = new SourceTextInputStream();
InitializeInputStream(preprocessor, inputStream);
@@ -2009,11 +2007,11 @@ static TokenList ReadAllTokens(
}
TokenList preprocessSource(
- CoreLib::String const& source,
- CoreLib::String const& fileName,
+ String const& source,
+ String const& fileName,
DiagnosticSink* sink,
IncludeHandler* includeHandler,
- CoreLib::Dictionary<CoreLib::String, CoreLib::String> defines,
+ Dictionary<String, String> defines,
ProgramSyntaxNode* syntax)
{
Preprocessor preprocessor;
diff --git a/source/slang/preprocessor.h b/source/slang/preprocessor.h
index cb5a8baae..a3c5336f5 100644
--- a/source/slang/preprocessor.h
+++ b/source/slang/preprocessor.h
@@ -15,19 +15,19 @@ class ProgramSyntaxNode;
struct IncludeHandler
{
virtual bool TryToFindIncludeFile(
- CoreLib::String const& pathToInclude,
- CoreLib::String const& pathIncludedFrom,
- CoreLib::String* outFoundPath,
- CoreLib::String* outFoundSource) = 0;
+ String const& pathToInclude,
+ String const& pathIncludedFrom,
+ String* outFoundPath,
+ String* outFoundSource) = 0;
};
// Take a string of source code and preprocess it into a list of tokens.
TokenList preprocessSource(
- CoreLib::String const& source,
- CoreLib::String const& fileName,
+ String const& source,
+ String const& fileName,
DiagnosticSink* sink,
IncludeHandler* includeHandler,
- CoreLib::Dictionary<CoreLib::String, CoreLib::String> defines,
+ Dictionary<String, String> defines,
ProgramSyntaxNode* syntax);
} // namespace Slang
diff --git a/source/slang/slang-stdlib.cpp b/source/slang/slang-stdlib.cpp
index 85c876105..9ff8a1078 100644
--- a/source/slang/slang-stdlib.cpp
+++ b/source/slang/slang-stdlib.cpp
@@ -947,8 +947,6 @@ typedef Texture2D texture2D;
)=" };
-using namespace CoreLib::Basic;
-
namespace Slang
{
static String stdlibPath;
diff --git a/source/slang/slang-stdlib.h b/source/slang/slang-stdlib.h
index fd41565d1..e4ee8cee3 100644
--- a/source/slang/slang-stdlib.h
+++ b/source/slang/slang-stdlib.h
@@ -8,13 +8,13 @@ namespace Slang
class SlangStdLib
{
private:
- static CoreLib::String code;
+ static String code;
public:
- static CoreLib::String GetCode();
+ static String GetCode();
static void Finalize();
};
- CoreLib::String getGLSLLibraryCode();
+ String getGLSLLibraryCode();
}
#endif \ No newline at end of file
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index 9f64edc7f..ce48f9032 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -17,9 +17,6 @@
#undef NOMINMAX
#endif
-using namespace CoreLib::Basic;
-using namespace CoreLib::IO;
-
namespace Slang {
static void stdlibDiagnosticCallback(
@@ -37,7 +34,7 @@ class Session
{
public:
bool useCache = false;
- CoreLib::String cacheDir;
+ String cacheDir;
RefPtr<Scope> slangLanguageScope;
RefPtr<Scope> hlslLanguageScope;
@@ -46,7 +43,7 @@ public:
List<RefPtr<ProgramSyntaxNode>> loadedModuleCode;
- Session(bool /*pUseCache*/, CoreLib::String /*pCacheDir*/)
+ Session(bool /*pUseCache*/, String /*pCacheDir*/)
{
// Initialize global state
// TODO: move this into the session instead
@@ -136,10 +133,10 @@ struct CompileRequest
List<String> searchDirs;
virtual bool TryToFindIncludeFile(
- CoreLib::String const& pathToInclude,
- CoreLib::String const& pathIncludedFrom,
- CoreLib::String* outFoundPath,
- CoreLib::String* outFoundSource) override
+ String const& pathToInclude,
+ String const& pathIncludedFrom,
+ String* outFoundPath,
+ String* outFoundSource) override
{
String path = Path::Combine(Path::GetDirectoryName(pathIncludedFrom), pathToInclude);
if (File::Exists(path))
diff --git a/source/slang/source-loc.h b/source/slang/source-loc.h
index 123da8c16..59f2f10ea 100644
--- a/source/slang/source-loc.h
+++ b/source/slang/source-loc.h
@@ -6,8 +6,6 @@
namespace Slang {
-using namespace CoreLib::Basic;
-
class CodePosition
{
public:
diff --git a/source/slang/syntax.cpp b/source/slang/syntax.cpp
index 38323e2b3..19f3939ce 100644
--- a/source/slang/syntax.cpp
+++ b/source/slang/syntax.cpp
@@ -21,9 +21,9 @@ namespace Slang
return this;
}
- CoreLib::Basic::String BasicExpressionType::ToString()
+ Slang::String BasicExpressionType::ToString()
{
- CoreLib::Basic::StringBuilder res;
+ Slang::StringBuilder res;
switch (BaseType)
{
@@ -360,7 +360,7 @@ namespace Slang
else
return BaseType->GetHashCode();
}
- CoreLib::Basic::String ArrayExpressionType::ToString()
+ Slang::String ArrayExpressionType::ToString()
{
if (ArrayLength)
return BaseType->ToString() + "[" + ArrayLength->ToString() + "]";
diff --git a/source/slang/syntax.h b/source/slang/syntax.h
index 8d56cd28e..3cd46914f 100644
--- a/source/slang/syntax.h
+++ b/source/slang/syntax.h
@@ -11,7 +11,6 @@
namespace Slang
{
- using namespace CoreLib::Basic;
class SyntaxVisitor;
class FunctionSyntaxNode;
@@ -850,7 +849,7 @@ namespace Slang
{
BaseType = baseType;
}
- virtual CoreLib::Basic::String ToString() override;
+ virtual Slang::String ToString() override;
protected:
virtual BasicExpressionType* GetScalarType() override;
virtual bool EqualsImpl(ExpressionType * type) override;
@@ -1034,7 +1033,7 @@ namespace Slang
public:
RefPtr<ExpressionType> BaseType;
RefPtr<IntVal> ArrayLength;
- virtual CoreLib::Basic::String ToString() override;
+ virtual Slang::String ToString() override;
protected:
virtual bool EqualsImpl(ExpressionType * type) override;
virtual ExpressionType* CreateCanonicalType() override;
diff --git a/source/slang/token.h b/source/slang/token.h
index 08eafccae..f177eb512 100644
--- a/source/slang/token.h
+++ b/source/slang/token.h
@@ -8,8 +8,6 @@
namespace Slang {
-using namespace CoreLib::Basic;
-
enum class TokenType
{
#define TOKEN(NAME, DESC) NAME,
diff --git a/source/slangc/main.cpp b/source/slangc/main.cpp
index 39be1a1fd..ead286434 100644
--- a/source/slangc/main.cpp
+++ b/source/slangc/main.cpp
@@ -4,15 +4,14 @@
#include "core/slang-io.h"
+using namespace Slang;
+
#include <assert.h>
// Currently only used for looking up `Profile::` values that aren't
// exported by the public API
#include "../slang/profile.h"
-using namespace CoreLib::Basic;
-using namespace CoreLib::IO;
-
// Try to read an argument for a command-line option.
wchar_t const* tryReadCommandLineArgumentRaw(wchar_t const* option, wchar_t***ioCursor, wchar_t**end)
{
diff --git a/tools/slang-test/main.cpp b/tools/slang-test/main.cpp
index 4940228fe..3d193dad4 100644
--- a/tools/slang-test/main.cpp
+++ b/tools/slang-test/main.cpp
@@ -2,8 +2,7 @@
#include "../../source/core/slang-io.h"
-using namespace CoreLib::Basic;
-using namespace CoreLib::IO;
+using namespace Slang;
#include "os.h"
@@ -509,9 +508,9 @@ TestResult gatherTestsForFile(
String fileContents;
try
{
- fileContents = CoreLib::IO::File::ReadAllText(filePath);
+ fileContents = Slang::File::ReadAllText(filePath);
}
- catch (CoreLib::IO::IOException)
+ catch (Slang::IOException)
{
return kTestResult_Fail;
}
@@ -629,9 +628,9 @@ TestResult runSimpleTest(TestInput& input)
String expectedOutput;
try
{
- expectedOutput = CoreLib::IO::File::ReadAllText(expectedOutputPath);
+ expectedOutput = Slang::File::ReadAllText(expectedOutputPath);
}
- catch (CoreLib::IO::IOException)
+ catch (Slang::IOException)
{
}
@@ -656,7 +655,7 @@ TestResult runSimpleTest(TestInput& input)
if (result == kTestResult_Fail)
{
String actualOutputPath = outputStem + ".actual";
- CoreLib::IO::File::WriteAllText(actualOutputPath, actualOutput);
+ Slang::File::WriteAllText(actualOutputPath, actualOutput);
}
return result;
@@ -691,9 +690,9 @@ TestResult generateHLSLBaseline(TestInput& input)
String expectedOutputPath = outputStem + ".expected";
try
{
- CoreLib::IO::File::WriteAllText(expectedOutputPath, expectedOutput);
+ Slang::File::WriteAllText(expectedOutputPath, expectedOutput);
}
- catch (CoreLib::IO::IOException)
+ catch (Slang::IOException)
{
return kTestResult_Fail;
}
@@ -758,9 +757,9 @@ TestResult runHLSLComparisonTest(TestInput& input)
String expectedOutput;
try
{
- expectedOutput = CoreLib::IO::File::ReadAllText(expectedOutputPath);
+ expectedOutput = Slang::File::ReadAllText(expectedOutputPath);
}
- catch (CoreLib::IO::IOException)
+ catch (Slang::IOException)
{
}
@@ -786,7 +785,7 @@ TestResult runHLSLComparisonTest(TestInput& input)
if (result == kTestResult_Fail)
{
String actualOutputPath = outputStem + ".actual";
- CoreLib::IO::File::WriteAllText(actualOutputPath, actualOutput);
+ Slang::File::WriteAllText(actualOutputPath, actualOutput);
}
return result;
@@ -869,8 +868,8 @@ TestResult runGLSLComparisonTest(TestInput& input)
TestResult hlslResult = doGLSLComparisonTestRun(input, "__GLSL__", "glslang", ".expected", &expectedOutput);
TestResult slangResult = doGLSLComparisonTestRun(input, "__SLANG__", nullptr, ".actual", &actualOutput);
- CoreLib::IO::File::WriteAllText(outputStem + ".expected", expectedOutput);
- CoreLib::IO::File::WriteAllText(outputStem + ".actual", actualOutput);
+ Slang::File::WriteAllText(outputStem + ".expected", expectedOutput);
+ Slang::File::WriteAllText(outputStem + ".actual", actualOutput);
if( hlslResult == kTestResult_Fail ) return kTestResult_Fail;
if( slangResult == kTestResult_Fail ) return kTestResult_Fail;
@@ -1014,8 +1013,8 @@ TestResult runHLSLRenderComparisonTestImpl(
TestResult hlslResult = doRenderComparisonTestRun(input, expectedArg, ".expected", &expectedOutput);
TestResult slangResult = doRenderComparisonTestRun(input, actualArg, ".actual", &actualOutput);
- CoreLib::IO::File::WriteAllText(outputStem + ".expected", expectedOutput);
- CoreLib::IO::File::WriteAllText(outputStem + ".actual", actualOutput);
+ Slang::File::WriteAllText(outputStem + ".expected", expectedOutput);
+ Slang::File::WriteAllText(outputStem + ".actual", actualOutput);
if( hlslResult == kTestResult_Fail ) return kTestResult_Fail;
if( slangResult == kTestResult_Fail ) return kTestResult_Fail;
diff --git a/tools/slang-test/os.cpp b/tools/slang-test/os.cpp
index 4bc3d8e6d..63db4f9ad 100644
--- a/tools/slang-test/os.cpp
+++ b/tools/slang-test/os.cpp
@@ -5,7 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
-using namespace CoreLib::Basic;
+using namespace Slang;
// Platform-specific code follows
@@ -54,8 +54,8 @@ bool OSFindFilesResult::findNextFile()
}
OSFindFilesResult osFindFilesInDirectoryMatchingPattern(
- CoreLib::Basic::String directoryPath,
- CoreLib::Basic::String pattern)
+ Slang::String directoryPath,
+ Slang::String pattern)
{
// TODO: add separator to end of directory path if needed
@@ -87,13 +87,13 @@ OSFindFilesResult osFindFilesInDirectoryMatchingPattern(
}
OSFindFilesResult osFindFilesInDirectory(
- CoreLib::Basic::String directoryPath)
+ Slang::String directoryPath)
{
return osFindFilesInDirectoryMatchingPattern(directoryPath, "*");
}
OSFindFilesResult osFindChildDirectories(
- CoreLib::Basic::String directoryPath)
+ Slang::String directoryPath)
{
// TODO: add separator to end of directory path if needed
@@ -189,7 +189,7 @@ static DWORD WINAPI osReaderThreadProc(LPVOID threadParam)
}
bytesRead = (DWORD)(writeCursor - buffer);
- // Note: Current Slang CoreLib gives no way to know
+ // Note: Current "core" implementation gives no way to know
// the length of the buffer, so we ultimately have
// to just assume null termination...
outputBuilder.Append(buffer, bytesRead);
@@ -201,7 +201,7 @@ static DWORD WINAPI osReaderThreadProc(LPVOID threadParam)
}
void OSProcessSpawner::pushExecutableName(
- CoreLib::Basic::String executableName)
+ Slang::String executableName)
{
executableName_ = executableName;
commandLine_.Append(executableName);
@@ -209,7 +209,7 @@ void OSProcessSpawner::pushExecutableName(
}
void OSProcessSpawner::pushExecutablePath(
- CoreLib::Basic::String executablePath)
+ Slang::String executablePath)
{
executableName_ = executablePath;
commandLine_.Append(executablePath);
@@ -217,7 +217,7 @@ void OSProcessSpawner::pushExecutablePath(
}
void OSProcessSpawner::pushArgument(
- CoreLib::Basic::String argument)
+ Slang::String argument)
{
// TODO(tfoley): handle cases where arguments need some escaping
commandLine_.Append(" ");
diff --git a/tools/slang-test/os.h b/tools/slang-test/os.h
index 5471e7e1a..c6eb8f410 100644
--- a/tools/slang-test/os.h
+++ b/tools/slang-test/os.h
@@ -32,8 +32,8 @@ enum OSError
// A helper type used during enumeration of files in a directory.
struct OSFindFilesResult
{
- CoreLib::Basic::String directoryPath_;
- CoreLib::Basic::String filePath_;
+ Slang::String directoryPath_;
+ Slang::String filePath_;
#ifdef WIN32
HANDLE findHandle_;
WIN32_FIND_DATAW fileData_;
@@ -57,7 +57,7 @@ struct OSFindFilesResult
context_ = NULL;
}
}
- CoreLib::Basic::String const& operator*() const
+ Slang::String const& operator*() const
{
return context_->filePath_;
}
@@ -86,10 +86,10 @@ struct OSFindFilesResult
// for( auto subdir : osFindChildDirectories(dir))
// { ... }
//
-// Each element in the range is a `CoreLib::Basic::String` representing the
+// Each element in the range is a `Slang::String` representing the
// path to a subdirecotry of the directory.
OSFindFilesResult osFindChildDirectories(
- CoreLib::Basic::String directoryPath);
+ Slang::String directoryPath);
// Enumerate files in the given `directoryPath` that match the provided
// `pattern` as a simplified regex for files to return (e.g., "*.txt")
@@ -99,11 +99,11 @@ OSFindFilesResult osFindChildDirectories(
// for( auto file : osFindFilesInDirectoryMatchingPattern(dir, "*.txt"))
// { ... }
//
-// Each element in the range is a `CoreLib::Basic::String` representing the
+// Each element in the range is a `Slang::String` representing the
// path to a file in the directory.
OSFindFilesResult osFindFilesInDirectoryMatchingPattern(
- CoreLib::Basic::String directoryPath,
- CoreLib::Basic::String pattern);
+ Slang::String directoryPath,
+ Slang::String pattern);
// Enumerate files in the given `directoryPath` and return a logical
// collection of the results that can be iterated with a range-based
@@ -112,10 +112,10 @@ OSFindFilesResult osFindFilesInDirectoryMatchingPattern(
// for( auto file : osFindFilesInDirectory(dir))
// { ... }
//
-// Each element in the range is a `CoreLib::Basic::String` representing the
+// Each element in the range is a `Slang::String` representing the
// path to a file in the directory.
OSFindFilesResult osFindFilesInDirectory(
- CoreLib::Basic::String directoryPath);
+ Slang::String directoryPath);
// An `OSProcessSpawner` can be used to launch a process, and handles
@@ -127,16 +127,16 @@ struct OSProcessSpawner
// Set the executable name for the process to be spawned.
// Note: this call must be made before any arguments are pushed.
void pushExecutableName(
- CoreLib::Basic::String executableName);
+ Slang::String executableName);
// Set the executable name for the process to be spawned.
// Note: this call must be made before any arguments are pushed.
void pushExecutablePath(
- CoreLib::Basic::String executablePath);
+ Slang::String executablePath);
// Append an argument for the process to be spawned.
void pushArgument(
- CoreLib::Basic::String argument);
+ Slang::String argument);
// Attempt to spawn the process, and wait for it to complete.
// Returns an error if the attempt to spawn and/or wait fails,
@@ -150,16 +150,16 @@ struct OSProcessSpawner
// on exit, along with the output it wrote to stdout and stderr.
typedef int ResultCode;
ResultCode getResultCode() { return resultCode_; }
- CoreLib::Basic::String const& getStandardOutput() { return standardOutput_; }
- CoreLib::Basic::String const& getStandardError() { return standardError_; }
+ Slang::String const& getStandardOutput() { return standardOutput_; }
+ Slang::String const& getStandardError() { return standardError_; }
// "private" data follows
- CoreLib::Basic::String standardOutput_;
- CoreLib::Basic::String standardError_;
+ Slang::String standardOutput_;
+ Slang::String standardError_;
ResultCode resultCode_;
#ifdef WIN32
- CoreLib::Basic::String executableName_;
- CoreLib::Basic::StringBuilder commandLine_;
+ Slang::String executableName_;
+ Slang::StringBuilder commandLine_;
// Is the executable specified by path, rather than just by name?
bool isExecutablePath_;