3
3
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
4
4
//
5
5
#pragma once
6
+ #include < cstdlib>
6
7
#include < cstring>
7
8
#include < vector>
8
9
#include < functional>
@@ -20,7 +21,7 @@ namespace Trinity
20
21
21
22
Array (size_t count)
22
23
{
23
- allocate (count);
24
+ allocate (count);
24
25
initialize ();
25
26
}
26
27
@@ -49,9 +50,9 @@ namespace Trinity
49
50
_copy_from (il.begin (), il.size ());
50
51
}
51
52
52
- Array (_Myt&& arr)
53
- {
54
- _move_from (std::forward<_Myt>(arr));
53
+ Array (_Myt&& arr)
54
+ {
55
+ _move_from (std::forward<_Myt>(arr));
55
56
}
56
57
57
58
~Array ()
@@ -60,36 +61,36 @@ namespace Trinity
60
61
deallocate ();
61
62
}
62
63
63
- Array& operator = (const _Myt & arr)
64
+ Array& operator = (const _Myt & arr)
64
65
{
65
66
destroy ();
66
- deallocate ();
67
- _copy_from (arr._array , arr._count );
68
- return *this ;
67
+ deallocate ();
68
+ _copy_from (arr._array , arr._count );
69
+ return *this ;
69
70
}
70
71
71
- template <class _VecAlloc > Array& operator = (const std::vector<T, _VecAlloc> & vec)
72
- {
72
+ template <class _VecAlloc > Array& operator = (const std::vector<T, _VecAlloc> & vec)
73
+ {
73
74
destroy ();
74
75
deallocate ();
75
76
_copy_from (vec.data (), vec.size ());
76
77
return *this ;
77
78
}
78
79
79
- Array& operator = (std::initializer_list<T> il)
80
- {
80
+ Array& operator = (std::initializer_list<T> il)
81
+ {
81
82
destroy ();
82
83
deallocate ();
83
- _copy_from (il.begin (), il.size ());
84
- return *this ;
84
+ _copy_from (il.begin (), il.size ());
85
+ return *this ;
85
86
}
86
87
87
- Array& operator = (_Myt && arr)
88
+ Array& operator = (_Myt && arr)
88
89
{
89
90
destroy ();
90
- deallocate ();
91
+ deallocate ();
91
92
_move_from (std::forward<_Myt>(arr));
92
- return *this ;
93
+ return *this ;
93
94
}
94
95
95
96
std::vector<T> ToList () const { std::vector<T> ret; ret.insert (ret.begin (), this ->begin (), this ->end ()); return ret; }
@@ -111,17 +112,17 @@ namespace Trinity
111
112
// As our primary usage is interop, it doesn't make sense
112
113
// either to return std::unique_ptr.
113
114
// Therefore the best thing to do is to malloc a new buffer, and move the elements
114
- // into the new buffer.
115
- T* detach_data ()
116
- {
115
+ // into the new buffer.
116
+ T* detach_data ()
117
+ {
117
118
auto len = _count * sizeof (T);
118
119
auto ret = (T*)malloc (len);
119
120
120
121
std::move (_array, _array + _count, ret);
121
122
122
123
// ! deallocate, not destroy
123
124
deallocate ();
124
- return ret;
125
+ return ret;
125
126
}
126
127
127
128
// since we're just simply arrays, we don't care much about the logic of iterator..
@@ -139,17 +140,17 @@ namespace Trinity
139
140
140
141
private:
141
142
142
- inline void allocate (size_t count)
143
- {
144
- _array = _allocator.allocate (count);
143
+ inline void allocate (size_t count)
144
+ {
145
+ _array = _allocator.allocate (count);
145
146
_count = count;
146
147
}
147
148
148
- inline void deallocate ()
149
- {
150
- _allocator.deallocate (_array, _count);
151
- _array = nullptr ;
152
- _count = 0 ;
149
+ inline void deallocate ()
150
+ {
151
+ _allocator.deallocate (_array, _count);
152
+ _array = nullptr ;
153
+ _count = 0 ;
153
154
}
154
155
155
156
inline void initialize ()
0 commit comments