11
11
12
12
module core.stdcpp.array ;
13
13
14
- // /////////////////////////////////////////////////////////////////////////////
15
- // std::array declaration.
16
- //
17
- // - no iterators
18
- // /////////////////////////////////////////////////////////////////////////////
19
-
20
- import stdcpp.allocator;
21
-
22
14
alias array = std.array ;
23
15
24
16
extern (C++ , std):
@@ -32,74 +24,56 @@ extern(C++, class) struct array(T, size_t N)
32
24
alias const_reference = ref const (T);
33
25
alias pointer = T* ;
34
26
alias const_pointer = const (T)* ;
35
- // alias iterator = pointer;
36
- // alias const_iterator = const_pointer;
37
- // alias reverse_iterator
38
- // alias const_reverse_iterator
39
-
40
- // Iterators
41
- // iterator begin() @trusted @nogc;
42
- // const_iterator begin() const @trusted @nogc;
43
- // const_iterator cbegin() const @trusted @nogc;
44
- // iterator end() @trusted @nogc;
45
- // const_iterator end() const @trusted @nogc;
46
- // const_iterator cend() const @trusted @nogc;
47
-
48
- // no reverse iterator for now.
49
27
50
- alias as_array this ;
28
+ alias as_array this ;
51
29
52
- extern (D ) size_type size() const nothrow @safe @nogc { return N; }
53
- extern (D ) size_type max_size() const nothrow @safe @nogc { return N; }
54
- extern (D ) bool empty() const nothrow @safe @nogc { return N > 0 ; }
30
+ extern (D ) size_type size() const nothrow @safe @nogc { return N; }
31
+ extern (D ) size_type max_size() const nothrow @safe @nogc { return N; }
32
+ extern (D ) bool empty() const nothrow @safe @nogc { return N == 0 ; }
55
33
56
34
// Element access
57
- extern (D ) reference front() @safe @nogc { return as_array()[0 ]; }
58
- extern (D ) const_reference front() const @safe @nogc { return as_array()[0 ]; }
59
- extern (D ) reference back() @safe @nogc { return as_array()[N == 0 ? 0 : N - 1 ]; }
60
- extern (D ) const_reference back() const @safe @nogc { return as_array()[N == 0 ? 0 : N - 1 ]; }
35
+ extern (D ) reference front() @safe @nogc { static if (N > 0 ) { return as_array()[ 0 ]; } else { return as_array()[][ 0 ]; } }
36
+ extern (D ) const_reference front() const @safe @nogc { static if (N > 0 ) { return as_array()[ 0 ]; } else { return as_array()[][ 0 ]; } }
37
+ extern (D ) reference back() @safe @nogc { static if (N > 0 ) { return as_array()[N- 1 ]; } else { return as_array()[][ 0 ]; } }
38
+ extern (D ) const_reference back() const @safe @nogc { static if (N > 0 ) { return as_array()[N- 1 ]; } else { return as_array()[][ 0 ]; } }
61
39
62
- extern (D ) void fill(ref const (T) value) @safe @nogc { foreach (ref T v; as_array()) v = value; }
40
+ extern (D ) void fill(ref const (T) value) @safe @nogc { foreach (ref T v; as_array()) v = value; }
63
41
64
42
// D helpers
65
- extern (D ) T[] opSlice () nothrow @safe @nogc { return as_array(); }
66
- extern (D ) const (T)[] opSlice () const nothrow @safe @nogc { return as_array(); }
67
- extern (D ) T[] opSlice (size_type start, size_type end) @safe { assert (start <= end && end <= N, " Index out of bounds" ); return as_array ()[start .. end]; }
68
- extern (D ) const (T)[] opSlice (size_type start, size_type end) const @safe { assert (start <= end && end <= N, " Index out of bounds" ); return as_array ()[start .. end]; }
69
- extern (D ) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert (pos == 0 , " std::vector is one-dimensional" ); return N; }
70
-
71
- // support all the assignment variants
72
- extern (D ) void opSliceAssign (T value) { opSlice ()[] = value; }
73
- extern (D ) void opSliceAssign (T value, size_type i, size_type j) { opSlice (i, j)[] = value; }
74
- extern (D ) void opSliceUnary(string op)() if (op == " ++" || op == " --" ) { mixin (op ~ " opSlice()[];" ); }
75
- extern (D ) void opSliceUnary(string op)(size_type i, size_type j) if (op == " ++" || op == " --" ) { mixin (op ~ " opSlice(i, j)[];" ); }
76
- extern (D ) void opSliceOpAssign(string op)(T value) { mixin (" opSlice()[] " ~ op ~ " = value;" ); }
77
- extern (D ) void opSliceOpAssign(string op)(T value, size_type i, size_type j) { mixin (" opSlice(i, j)[] " ~ op ~ " = value;" ); }
78
-
79
- private :
43
+ extern (D ) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert (pos == 0 , " std::vector is one-dimensional" ); return N; }
44
+
80
45
version (CRuntime_Microsoft )
81
46
{
82
- import core.stdcpp.utility : _Xout_of_range;
47
+ // perf will be greatly improved by inlining the primitive access functions
48
+ extern (D ) T* data() nothrow @safe @nogc { return &_Elems[0 ]; }
49
+ extern (D ) const (T)* data() const nothrow @safe @nogc { return &_Elems[0 ]; }
83
50
84
- T[N ? N : 1 ] _Elems;
51
+ extern (D ) ref T at(size_type i) nothrow @trusted @nogc { static if (N > 0 ) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); return _Elems[0 ]; } }
52
+ extern (D ) ref const (T) at(size_type i) const nothrow @trusted @nogc { static if (N > 0 ) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); return _Elems[0 ]; } }
85
53
86
- void _Xran () const @trusted @nogc { _Xout_of_range( " invalid array<T, N> subscript " ) ; }
54
+ extern ( D ) ref inout (T)[N] as_array() const inout @safe @nogc { return _Elems[ 0 .. N] ; }
87
55
88
- public :
89
- // perf will be greatly improved by inlining the primitive access functions
90
- extern (D ) T* data() nothrow @safe @nogc { return &_Elems[0 ]; }
91
- extern (D ) const (T)* data() const nothrow @safe @nogc { return &_Elems[0 ]; }
56
+ private :
57
+ import core.stdcpp.utility : _Xout_of_range;
92
58
93
- extern (D ) ref T opIndex (size_type i) nothrow @safe @nogc { return _Elems[0 .. N][i]; }
94
- extern (D ) ref const (T) opIndex (size_type i) const nothrow @safe @nogc { return _Elems[0 .. N][i]; }
95
- extern (D ) ref T at(size_type i) nothrow @trusted @nogc { static if (N > 0 ) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); } }
96
- extern (D ) ref const (T) at(size_type i) const nothrow @trusted @nogc { static if (N > 0 ) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); } }
59
+ T[N ? N : 1 ] _Elems;
97
60
98
- extern (D ) T[] as_array() nothrow @safe @nogc { return _Elems[0 .. N]; }
99
- extern (D ) const (T)[] as_array() const nothrow @safe @nogc { return _Elems[0 .. N]; }
61
+ void _Xran () const @safe @nogc { _Xout_of_range(" invalid array<T, N> subscript" ); }
100
62
}
101
63
else version (CRuntime_Glibc )
102
64
{
65
+ import core.exception : RangeError;
66
+
67
+ // perf will be greatly improved by inlining the primitive access functions
68
+ extern (D ) T* data() nothrow @safe @nogc { static if (N > 0 ) { return &_M_elems[0 ]; } else { return null ; } }
69
+ extern (D ) const (T)* data() const nothrow @safe @nogc { static if (N > 0 ) { return &_M_elems[0 ]; } else { return null ; } }
70
+
71
+ extern (D ) ref T at(size_type i) @trusted { if (i >= N) throw new RangeError(" Index out of range" ); return _M_elems.ptr[i]; }
72
+ extern (D ) ref const (T) at(size_type i) const @trusted { if (i >= N) throw new RangeError(" Index out of range" ); return _M_elems.ptr[i]; }
73
+
74
+ extern (D ) ref inout (T)[N] as_array() inout nothrow @safe @nogc { return _M_elems[0 .. N]; }
75
+
76
+ private :
103
77
static if (N > 0 )
104
78
{
105
79
T[N] _M_elems;
@@ -109,22 +83,6 @@ private:
109
83
struct _Placeholder {}
110
84
_Placeholder _M_placeholder;
111
85
}
112
-
113
- public :
114
- import core.exception : RangeError;
115
-
116
- // perf will be greatly improved by inlining the primitive access functions
117
- extern (D ) T* data() nothrow @safe @nogc { static if (N > 0 ) { return &_M_elems[0 ]; } else { return null ; } }
118
- extern (D ) const (T)* data() const nothrow @safe @nogc { static if (N > 0 ) { return &_M_elems[0 ]; } else { return null ; } }
119
-
120
- extern (D ) ref T opIndex (size_type i) nothrow @nogc { static if (N > 0 ) { return _M_elems[i]; } else { return (cast (T[])null )[i]; } }
121
- extern (D ) ref const (T) opIndex (size_type i) const nothrow @nogc { static if (N > 0 ) { return _M_elems[i]; } else { return (cast (T[])null )[i]; } }
122
- extern (D ) ref T at(size_type i) @trusted { if (i >= N) throw new RangeError(" Index out of range" ); return _M_elems.ptr[i]; }
123
- extern (D ) ref const (T) at(size_type i) const @trusted { if (i >= N) throw new RangeError(" Index out of range" ); return _M_elems.ptr[i]; }
124
-
125
- alias as_array this ;
126
- extern (D ) T[] as_array() nothrow @safe @nogc { static if (N > 0 ) { return _M_elems[]; } else { return null ; } }
127
- extern (D ) const (T)[] as_array() const nothrow @safe @nogc { static if (N > 0 ) { return _M_elems[]; } else { return null ; } }
128
86
}
129
87
else
130
88
{
0 commit comments