summaryrefslogtreecommitdiffstats
path: root/src/LazyArray.h
blob: 310a1e2c6fab8a7ee67ae28325e17a94c4435114 (plain) (blame)
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
131
132
133
134

#pragma once

/** A dynamic array that defers allocation to the first modifying access.
Const references from the array before allocation will all be to the same default constructed value.
It is therefore important that default constructed values are indistinguishable from each other. */
template <typename T>
class cLazyArray
{
	static_assert((!std::is_reference<T>::value && !std::is_array<T>::value),
		"cLazyArray<T>: T must be a value type");
	static_assert(std::is_default_constructible<T>::value,
		"cLazyArray<T>: T must be default constructible");
public:
	using value_type = T;
	using pointer = T *;
	using const_pointer = const T *;
	using reference = T &;
	using const_reference = const T &;
	using size_type = int;
	using iterator = pointer;
	using const_iterator = const_pointer;

	cLazyArray(size_type a_Size) NOEXCEPT:
		m_Size{ a_Size }
	{
		ASSERT(a_Size > 0);
	}

	cLazyArray(const cLazyArray & a_Other):
		m_Size{ a_Other.m_Size }
	{
		if (a_Other.IsStorageAllocated())
		{
			// Note that begin() will allocate the array to copy into
			std::copy(a_Other.begin(), a_Other.end(), begin());
		}
	}

	cLazyArray(cLazyArray && a_Other) NOEXCEPT:
		m_Array{ std::move(a_Other.m_Array) },
		m_Size{ a_Other.m_Size }
	{
	}

	cLazyArray & operator = (const cLazyArray & a_Other)
	{
		cLazyArray(a_Other).swap(*this);
		return *this;
	}

	cLazyArray & operator = (cLazyArray && a_Other) NOEXCEPT
	{
		m_Array = std::move(a_Other.m_Array);
		m_Size = a_Other.m_Size;
		return *this;
	}

	T & operator [] (size_type a_Idx)
	{
		ASSERT((0 <= a_Idx) && (a_Idx < m_Size));
		return data()[a_Idx];
	}

	const T & operator [] (size_type a_Idx) const
	{
		return GetAt(a_Idx);
	}

	// STL style interface

	const_iterator cbegin() const { return data(); }
	iterator        begin()       { return data(); }
	const_iterator  begin() const { return cbegin(); }

	const_iterator cend() const { return data() + m_Size; }
	iterator        end()       { return data() + m_Size; }
	const_iterator  end() const { return cend(); }

	size_type size() const NOEXCEPT { return m_Size; }

	const T * data() const
	{
		if (m_Array == nullptr)
		{
			m_Array.reset(new T[ToUnsigned(m_Size)]);
		}
		return m_Array.get();
	}

	T * data()
	{
		static_assert(!std::is_const<typename decltype(m_Array)::element_type>::value, "");
		const cLazyArray * const_this = this;
		return const_cast<T *>(const_this->data());
	}

	void swap(cLazyArray & a_Other) NOEXCEPT
	{
		std::swap(m_Array, a_Other.m_Array);
		std::swap(m_Size, a_Other.m_Size);
	}

	friend void swap(cLazyArray & a_Lhs, cLazyArray & a_Rhs) NOEXCEPT
	{
		a_Lhs.swap(a_Rhs);
	}

	// Extra functions to help avoid allocation

	/** A const view of an element of the array. Never causes the array to allocate. */
	const T & GetAt(size_type a_Idx) const
	{
		ASSERT((0 <= a_Idx) && (a_Idx < m_Size));
		if (IsStorageAllocated())
		{
			return data()[a_Idx];
		}
		else
		{
			static const T DefaultValue;
			return DefaultValue;
		}
	}

	/** Returns true if the array has already been allocated. */
	bool IsStorageAllocated() const NOEXCEPT { return (m_Array != nullptr); }

private:
	// Mutable so const data() can allocate the array
	mutable std::unique_ptr<T[]> m_Array;
	size_type m_Size;
};