Skip to content

Commit 03a7803

Browse files
authored
Merge pull request #36 from libmir/ap
add array primitives
2 parents f1e5984 + f5cdb48 commit 03a7803

File tree

4 files changed

+184
-2
lines changed

4 files changed

+184
-2
lines changed

doc/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ ARTWORK_DIR=$(DOC_SOURCE_DIR)/artwork
2525
# Packages in mir. Just mention the package name here. The contents of package
2626
# xy/zz is in variable PACKAGE_xy_zz. This allows automation in iterating
2727
# packages and their modules.
28-
MIR_PACKAGES = mir mir/ndslice mir/internal mir/math mir/math/func
28+
MIR_PACKAGES = mir mir/ndslice mir/internal mir/math mir/math/func mir/array
2929

3030
PACKAGE_mir = bitmanip conv functional primitives
31+
PACKAGE_mir_array = primitives
3132
PACKAGE_mir_ndslice = package algorithm allocation dynamic field ndfield iterator package slice sorting concatenation topology
3233
PACKAGE_mir_math = constant common sum
3334
PACKAGE_mir_math_func = expdigamma

source/mir/array/primitives.d

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/++
2+
Range primitives for arrays with multi-dimensional like API support.
3+
4+
Note:
5+
UTF strings behaves like common arrays in Mir.
6+
`std.uni.byCodePoint` can be used to creat a range of chararacters.
7+
8+
See_also: $(MREF mir,_primitives).
9+
10+
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
11+
Copyright: Copyright © 2017-, Ilya Yaroshenko
12+
Authors: Ilya Yaroshenko
13+
+/
14+
module mir.array.primitives;
15+
16+
import std.traits;
17+
import mir.internal.utility;
18+
19+
pragma(inline, true) @fastmath:
20+
21+
///
22+
bool empty(size_t dim = 0, T)(in T[] ar)
23+
if (!dim)
24+
{
25+
return !ar.length;
26+
}
27+
28+
///
29+
unittest
30+
{
31+
assert((int[]).init.empty);
32+
assert(![1].empty!0); // Slice-like API
33+
}
34+
35+
///
36+
ref front(size_t dim = 0, T)(T[] ar)
37+
if (!dim && !is(Unqual!T[] == void[]))
38+
{
39+
assert(ar.length, "Accessing front of an empty array.");
40+
return ar[0];
41+
}
42+
43+
///
44+
unittest
45+
{
46+
assert(*&[3, 4].front == 3); // access be ref
47+
assert([3, 4].front!0 == 3); // Slice-like API
48+
}
49+
50+
51+
///
52+
ref back(size_t dim = 0, T)(T[] ar)
53+
if (!dim && !is(Unqual!T[] == void[]))
54+
{
55+
assert(ar.length, "Accessing back of an empty array.");
56+
return ar[$ - 1];
57+
}
58+
59+
///
60+
unittest
61+
{
62+
assert(*&[3, 4].back == 4); // access be ref
63+
assert([3, 4].back!0 == 4); // Slice-like API
64+
}
65+
66+
///
67+
void popFront(size_t dim = 0, T)(ref T[] ar)
68+
if (!dim && !is(Unqual!T[] == void[]))
69+
{
70+
assert(ar.length, "Evaluating popFront() on an empty array.");
71+
ar = ar[1 .. $];
72+
}
73+
74+
///
75+
unittest
76+
{
77+
auto ar = [3, 4];
78+
ar.popFront;
79+
assert(ar == [4]);
80+
ar.popFront!0; // Slice-like API
81+
assert(ar == []);
82+
}
83+
84+
///
85+
void popBack(size_t dim = 0, T)(ref T[] ar)
86+
if (!dim && !is(Unqual!T[] == void[]))
87+
{
88+
assert(ar.length, "Evaluating popBack() on an empty array.");
89+
ar = ar[0 .. $ - 1];
90+
}
91+
92+
///
93+
unittest
94+
{
95+
auto ar = [3, 4];
96+
ar.popBack;
97+
assert(ar == [3]);
98+
ar.popBack!0; // Slice-like API
99+
assert(ar == []);
100+
}
101+
102+
///
103+
size_t popFrontN(size_t dim = 0, T)(ref T[] ar, size_t n)
104+
if (!dim && !is(Unqual!T[] == void[]))
105+
{
106+
n = ar.length < n ? ar.length : n;
107+
ar = ar[n .. $];
108+
return n;
109+
}
110+
111+
///
112+
unittest
113+
{
114+
auto ar = [3, 4];
115+
ar.popFrontN(1);
116+
assert(ar == [4]);
117+
ar.popFrontN!0(10); // Slice-like API
118+
assert(ar == []);
119+
}
120+
121+
///
122+
size_t popBackN(size_t dim = 0, T)(ref T[] ar, size_t n)
123+
if (!dim && !is(Unqual!T[] == void[]))
124+
{
125+
n = ar.length < n ? ar.length : n;
126+
ar = ar[0 .. $ - n];
127+
return n;
128+
}
129+
130+
///
131+
unittest
132+
{
133+
auto ar = [3, 4];
134+
ar.popBackN(1);
135+
assert(ar == [3]);
136+
ar.popBackN!0(10); // Slice-like API
137+
assert(ar == []);
138+
}
139+
140+
///
141+
void popFrontExactly(size_t dim = 0, T)(ref T[] ar, size_t n)
142+
if (!dim && !is(Unqual!T[] == void[]))
143+
{
144+
assert(ar.length >= n, "Evaluating *.popFrontExactly(n) on an array with length less then n.");
145+
ar = ar[n .. $];
146+
}
147+
148+
///
149+
unittest
150+
{
151+
auto ar = [3, 4, 5];
152+
ar.popFrontExactly(2);
153+
assert(ar == [5]);
154+
ar.popFrontExactly!0(1); // Slice-like API
155+
assert(ar == []);
156+
}
157+
158+
///
159+
void popBackExactly(size_t dim = 0, T)(ref T[] ar, size_t n)
160+
if (!dim && !is(Unqual!T[] == void[]))
161+
{
162+
assert(ar.length >= n, "Evaluating *.popBackExactly(n) on an array with length less then n.");
163+
ar = ar[0 .. $ - n];
164+
}
165+
166+
///
167+
unittest
168+
{
169+
auto ar = [3, 4, 5];
170+
ar.popBackExactly(2);
171+
assert(ar == [3]);
172+
ar.popBackExactly!0(1); // Slice-like API
173+
assert(ar == []);
174+
}

source/mir/ndslice/internal.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module mir.ndslice.internal;
22

3-
import std.range.primitives;
43
import std.traits;
54
import std.meta;
65
import mir.ndslice.slice;
76
import mir.internal.utility;
7+
import mir.array.primitives;
88

99
@fastmath:
1010

source/mir/primitives.d

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/++
22
Templates used to check primitives.
3+
4+
Publicly imports $(MREF mir,array,_primitives).
5+
6+
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
7+
Copyright: Copyright © 2017-, Ilya Yaroshenko
8+
Authors: Ilya Yaroshenko
39
+/
410
module mir.primitives;
511

612
import mir.internal.utility;
13+
public import mir.array.primitives;
714

815
@fastmath:
916

0 commit comments

Comments
 (0)