summaryrefslogtreecommitdiffstats
path: root/source/core/array-view.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-04-29 17:03:46 -0400
committerTim Foley <tfoleyNV@users.noreply.github.com>2019-04-29 14:03:46 -0700
commit4880789e3003441732cca4471091563f36531635 (patch)
tree8e0d3ed58a561373b35729d24787afe6b39732e3 /source/core/array-view.h
parentded340beb4b5197b559626acc39920abb2d39e77 (diff)
String/List closer to conventions, and use Index type (#959)
* List made members m_ Tweaked types to closer match conventions. * Use asserts for checking conditions on List. Other small improvements. * List<T>.Count() -> getSize() * List<T> Add -> add First -> getFirst Last -> getLast RemoveLast -> removeLast ReleaseBuffer -> detachBuffer GetArrayView -> getArrayView * List<T>:: AddRange -> addRange Capacity -> getCapacity Insert -> insert InsertRange -> insertRange AddRange -> addRange RemoveRange -> removeRange RemoveAt -> removeAt Remove -> remove Reverse -> reverse FastRemove -> fastRemove FastRemoveAt -> fastRemoveAt Clear -> clear * List<T> FreeBuffer -> _deallocateBuffer Free -> clearAndDeallocate SwapWith -> swapWith * List<T> SetSize -> setSize Reserve -> reserve GrowToSize growToSize * UnsafeShrinkToSize -> unsafeShrinkToSize Compress -> compress FindLast -> findLastIndex FindLast -> findLastIndex Simplify Contains * List<T> Removed m_allocator (wasn't used) Swap -> swapElements Sort -> sort Contains -> contains ForEach -> forEach QuickSort -> quickSort InsertionSort -> insertionSort BinarySearch -> binarySearch Max -> calcMax Min -> calcMin * Initializer::Initialize -> initialize List<T>:: Allocate -> _allocate Init -> _init IndexOf -> indexOf * * Put #include <assert.h> in common.h, and remove unneeded inclusions * Small refactor of ArrayView - remove stride as not used * getSize -> getCount setSize -> setCount unsafeShrinkToSize->unsafeShrinkToCount growToSize -> growToCount m_size -> m_count * Some tidy up around Allocator. * Use Index type on List. * Refactor of IntSet. First tentative look at using Index. * Made Index an Int Did preliminary fixes. Made String use Index. * Partial refactor of String. * String::Buffer -> getBuffer ToWString -> toWString * Small improvements to String. String:: Buffer() -> getBuffer() Equals() -> equals * Try to use Index where appropriate. * Fix warnings on windows x86 builds.
Diffstat (limited to 'source/core/array-view.h')
-rw-r--r--source/core/array-view.h111
1 files changed, 47 insertions, 64 deletions
diff --git a/source/core/array-view.h b/source/core/array-view.h
index 193aedbbb..ad9673e2e 100644
--- a/source/core/array-view.h
+++ b/source/core/array-view.h
@@ -1,7 +1,7 @@
#ifndef CORE_LIB_ARRAY_VIEW_H
#define CORE_LIB_ARRAY_VIEW_H
-#include "exception.h"
+#include "common.h"
namespace Slang
{
@@ -9,104 +9,87 @@ namespace Slang
class ArrayView
{
private:
- T * _buffer;
- int _count;
- int stride;
+ T* m_buffer;
+ int m_count;
public:
- T* begin() const
- {
- return _buffer;
- }
- T* end() const
- {
- return (T*)((char*)_buffer + _count*stride);
- }
+ const T* begin() const { return m_buffer; }
+ T* begin() { return m_buffer; }
+
+ const T* end() const { return m_buffer + m_count; }
+ T* end() { return m_buffer + m_count; }
+
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():
+ m_buffer(nullptr),
+ m_count(0)
+ {
}
- 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;
+ ArrayView(T& singleObj):
+ m_buffer(&singleObj),
+ m_count(1)
+ {
}
- inline int Count() const
+ ArrayView(T* buffer, int size):
+ m_buffer(buffer),
+ m_count(size)
{
- return _count;
}
+
+ inline int getCount() const { return m_count; }
- inline T & operator [](int id) const
+ inline const T& operator [](int idx) const
{
-#if _DEBUG
- if (id >= _count || id < 0)
- throw IndexOutofRangeException("Operator[]: Index out of Range.");
-#endif
- return *(T*)((char*)_buffer+id*stride);
+ SLANG_ASSERT(idx >= 0 && idx <= m_count);
+ return m_buffer[idx];
}
+ inline T& operator [](int idx)
+ {
+ SLANG_ASSERT(idx >= 0 && idx <= m_count);
+ return m_buffer[idx];
+ }
- inline T* Buffer() const
- {
- return _buffer;
- }
+ inline const T* getBuffer() const { return m_buffer; }
+ inline T* getBuffer() { return m_buffer; }
template<typename T2>
- int IndexOf(const T2 & val) const
+ int indexOf(const T2 & val) const
{
- for (int i = 0; i < _count; i++)
+ for (int i = 0; i < m_count; i++)
{
- if (*(T*)((char*)_buffer + i*stride) == val)
+ if (m_buffer[i] == val)
return i;
}
return -1;
}
template<typename T2>
- int LastIndexOf(const T2 & val) const
+ int lastIndexOf(const T2 & val) const
{
- for (int i = _count - 1; i >= 0; i--)
+ for (int i = m_count - 1; i >= 0; i--)
{
- if (*(T*)((char*)_buffer + i*stride) == val)
+ if (m_buffer[i] == val)
return i;
}
return -1;
}
template<typename Func>
- int FindFirst(const Func & predicate) const
+ int findFirstIndex(const Func& predicate) const
{
- for (int i = 0; i < _count; i++)
+ for (int i = 0; i < m_count; i++)
{
- if (predicate(_buffer[i]))
+ if (predicate(m_buffer[i]))
return i;
}
return -1;
}
template<typename Func>
- int FindLast(const Func & predicate) const
+ int findLastIndex(const Func& predicate) const
{
- for (int i = _count - 1; i >= 0; i--)
+ for (int i = m_count - 1; i >= 0; i--)
{
- if (predicate(_buffer[i]))
+ if (predicate(m_buffer[i]))
return i;
}
return -1;
@@ -114,16 +97,16 @@ namespace Slang
};
template<typename T>
- ArrayView<T> MakeArrayView(const T & obj)
+ ArrayView<T> makeArrayView(T& obj)
{
return ArrayView<T>(obj);
}
template<typename T>
- ArrayView<T> MakeArrayView(T * buffer, int count)
+ ArrayView<T> makeArrayView(T* buffer, int count)
{
return ArrayView<T>(buffer, count);
}
}
-#endif \ No newline at end of file
+#endif