2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // some "unsafe" array operations
19 // such arrays should be always anchored to first element
20 module iv
.unarray
/*is aliced*/;
24 // ////////////////////////////////////////////////////////////////////////// //
25 public void unsafeArrayReserve(T
) (ref T
[] arr
, usize newlen
) /*nothrow*/ {
26 if (/*newlen < 0 ||*/ newlen
>= int.max
/2) assert(0, "invalid number of elements in array");
27 if (arr
.length
< newlen
) {
30 if (arr
.ptr
!is optr
) {
31 import core
.memory
: GC
;
33 if (optr
is GC
.addrOf(optr
)) GC
.setAttr(optr
, GC
.BlkAttr
.NO_INTERIOR
);
39 public void unsafeArraySetLength(T
) (ref T
[] arr
, usize newlen
) /*nothrow*/ {
40 if (/*newlen < 0 ||*/ newlen
>= int.max
/2) assert(0, "invalid number of elements in array");
41 if (arr
.length
> newlen
) {
44 } else if (arr
.length
< newlen
) {
47 if (arr
.ptr
!is optr
) {
48 import core
.memory
: GC
;
50 if (optr
is GC
.addrOf(optr
)) GC
.setAttr(optr
, GC
.BlkAttr
.NO_INTERIOR
);
56 public void unsafeArrayAppend(T
) (ref T
[] arr
, auto ref T v
) /*nothrow*/ {
57 if (arr
.length
>= int.max
/2) assert(0, "too many elements in array");
60 if (arr
.ptr
!is optr
) {
61 import core
.memory
: GC
;
63 if (optr
is GC
.addrOf(optr
)) GC
.setAttr(optr
, GC
.BlkAttr
.NO_INTERIOR
);
68 public void unsafeArrayClear(T
) (ref T
[] arr
) /*nothrow*/ {
70 import core
.stdc
.string
: memset
;
71 static if (is(T
== class)) arr
[] = null; /*else arr[] = T.init;*/
72 memset(arr
.ptr
, 0, arr
.length
*T
.sizeof
);
79 public void unsafeArrayRemove(T
) (ref T
[] arr
, usize idx
) /*nothrow*/ {
80 if (/*idx < 0 ||*/ idx
>= arr
.length
) assert(0, "invalid index in `unsafeArrayRemove()`");
81 static if (is(T
== class)) arr
[idx
] = null; else arr
[idx
] = T
.init
;
82 if (arr
.length
-idx
> 1) {
83 import core
.stdc
.string
: memset
, memmove
;
84 memmove(arr
.ptr
+idx
, arr
.ptr
+idx
+1, (arr
.length
-idx
-1)*T
.sizeof
);
85 memset(arr
.ptr
+arr
.length
-1, 0, T
.sizeof
);
92 public void unsafeArrayInsertBefore(T
) (ref T
[] arr
, usize idx
, auto ref T v
) /*nothrow*/ {
93 if (/*idx < 0 ||*/ idx
> arr
.length
) assert(0, "invalid index in `unsafeArrayRemove()`");
94 auto olen
= cast(int)arr
.length
;
95 if (olen
>= int.max
/2) assert(0, "too many elements in array");
98 if (arr
.ptr
!= optr
) {
99 import core
.memory
: GC
;
101 if (optr
is GC
.addrOf(optr
)) GC
.setAttr(optr
, GC
.BlkAttr
.NO_INTERIOR
);
103 // move elements down
105 import core
.stdc
.string
: memset
, memmove
;
106 memmove(arr
.ptr
+idx
+1, arr
.ptr
+idx
, (olen
-idx
)*T
.sizeof
);
107 memset(arr
.ptr
+idx
, 0, T
.sizeof
);