summaryrefslogtreecommitdiffstats
path: root/source/core/slang-array.h
blob: 2647e163fc1f3e1068f1e5b12d89837539ab9413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef SLANG_CORE_ARRAY_H
#define SLANG_CORE_ARRAY_H

#include "slang-exception.h"
#include "slang-array-view.h"

namespace Slang
{
    /* An array container with fixed maximum size defined by COUNT. */
	template<typename T, Index COUNT>
	class Array
	{
	public:
        T* begin() { return m_buffer; }
        const T* begin() const { return m_buffer; }

        const T* end() const { return m_buffer + m_count; }
        T* end() { return m_buffer + m_count; }

		inline Index getCapacity() const { return COUNT; }
		inline Index getCount() const { return m_count; }
		inline const T& getFirst() const
		{
            SLANG_ASSERT(m_count > 0);
			return m_buffer[0];
		}
        inline T& getFirst()
        {
            SLANG_ASSERT(m_count > 0);
            return m_buffer[0];
        }
		inline const T& getLast() const
		{
            SLANG_ASSERT(m_count > 0);
			return m_buffer[m_count - 1];
		}
        inline T& getLast()
        {
            SLANG_ASSERT(m_count > 0);
            return m_buffer[m_count - 1];
        }
		inline void setCount(Index newCount)
		{
            SLANG_ASSERT(newCount >= 0 && newCount <= COUNT);
			m_count = newCount;
		}
		inline void add(const T& item)
		{
            SLANG_ASSERT(m_count < COUNT);
			m_buffer[m_count++] = item;
		}
		inline void add(T&& item)
		{
            SLANG_ASSERT(m_count < COUNT);
			m_buffer[m_count++] = _Move(item);
		}

		inline const T& operator [](Index idx) const
		{
            SLANG_ASSERT(idx >= 0 && idx < m_count);
			return m_buffer[idx];
		}
        inline T& operator [](Index idx)
        {
            SLANG_ASSERT(idx >= 0 && idx < m_count);
            return m_buffer[idx];
        }

		inline const T* getBuffer() const { return m_buffer; }
        inline T* getBuffer() { return m_buffer; }

		inline void clear() { m_count = 0; }

		template<typename T2>
		Index indexOf(const T2& val) const { return getView().indexOf(val);	}
		template<typename T2>
		Index lastIndexOf(const T2& val) const { return getView().lastIndexOf(val); }
        template<typename Func>
        Index findFirstIndex(const Func& predicate) const { return getView().findFirstIndex(predicate); }
        template<typename Func>
        Index findLastIndex(const Func& predicate) const { return getView().findLastIndex(predicate); }

		inline ConstArrayView<T> getView() const { return ConstArrayView<T>(m_buffer, m_count); }
		inline ConstArrayView<T> getView(Index start, Index count) const
		{
            SLANG_ASSERT(start >= 0 && count >= 0);
            SLANG_ASSERT(start <= m_count && start + count < m_count);
			return ConstArrayView<T>(m_buffer + start, count);
		}

        inline ArrayView<T> getView() { return ArrayView<T>(m_buffer, m_count); }
        inline ArrayView<T> getView(Index start, Index count) 
        {
            SLANG_ASSERT(start >= 0 && count >= 0);
            SLANG_ASSERT(start <= m_count && start + count < m_count);
            return ArrayView<T>(m_buffer + start, count);
        }

    private:
        T m_buffer[COUNT];
        Index m_count = 0;
	};

	template<typename T, typename ...TArgs>
	struct FirstType
	{
		typedef T Type;
	};


	template<typename T, Index SIZE>
	void insertArray(Array<T, SIZE>&) {}

	template<typename T, typename ...TArgs, Index 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, Index(sizeof...(args))> rs;
		insertArray(rs, args...);
		return rs;
	}
}

#endif