Skip to content

Commit 5177edc

Browse files
committed
fix a log of bugs..
1 parent ec86773 commit 5177edc

File tree

9 files changed

+231
-126
lines changed

9 files changed

+231
-126
lines changed

source/mir/array/allocation.d

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,41 @@ if (isIterable!Range && !isInfinite!Range && !isStaticArray!Range || isPointer!R
126126
if (length == 0)
127127
return null;
128128

129-
import std.conv : emplace;
130-
131-
return () @trusted {
132-
import std.array: uninitializedArray;
133-
auto result = length.uninitializedArray!(Unqual!E[]);
134-
auto p = cast(E*) result.ptr;
135-
static if (isInputRange!Range)
136-
for (; !r.empty; r.popFront)
137-
p = emplace!E(p, r.front) + 1;
138-
else
139-
static if (isPointer!Range)
140-
foreach (e; *r)
141-
p = emplace!E(p, e) + 1;
142-
else
143-
foreach (e; r)
144-
p = emplace!E(p, e) + 1;
145-
return cast(E[]) result;
146-
}();
129+
import std.backdoor : emplaceRef;
130+
import std.array: uninitializedArray;
131+
132+
auto result = (() @trusted => uninitializedArray!(Unqual!E[])(length))();
133+
134+
static if (isInputRange!Range)
135+
{
136+
foreach(ref e; result)
137+
{
138+
emplaceRef!E(e, r.front);
139+
r.popFront;
140+
}
141+
}
142+
else
143+
static if (isPointer!Range)
144+
{
145+
auto it = result;
146+
foreach(ref f; *r)
147+
{
148+
emplaceRef!E(it[0], f);
149+
it = it[1 .. $];
150+
}
151+
}
152+
else
153+
static if (isPointer!Range)
154+
{
155+
auto it = result;
156+
foreach(ref f; r)
157+
{
158+
emplaceRef!E(it[0], f);
159+
it = it[1 .. $];
160+
}
161+
}
162+
163+
return (() @trusted => cast(E[]) result)();
147164
}
148165
else
149166
{

source/mir/bitmanip.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private template createAccessors(
8787
// setter
8888
~"@property void " ~ name ~ "()(bool v) @safe pure nothrow @nogc { "
8989
~"if (v) "~store~" |= "~myToString(maskAllElse)~";"
90-
~"else "~store~" &= ~cast(typeof("~store~"))"~myToString(maskAllElse)~";}\n";
90+
~"else "~store~" &= cast(typeof("~store~"))(-1-cast(typeof("~store~"))"~myToString(maskAllElse)~");}\n";
9191
}
9292
else
9393
{
@@ -106,7 +106,7 @@ private template createAccessors(
106106
~"assert(v >= "~name~`_min, "Value is smaller than the minimum value of bitfield '`~name~`'"); `
107107
~"assert(v <= "~name~`_max, "Value is greater than the maximum value of bitfield '`~name~`'"); `
108108
~store~" = cast(typeof("~store~"))"
109-
~" (("~store~" & ~cast(typeof("~store~"))"~myToString(maskAllElse)~")"
109+
~" (("~store~" & (-1-cast(typeof("~store~"))"~myToString(maskAllElse)~"))"
110110
~" | ((cast(typeof("~store~")) v << "~myToString(offset)~")"
111111
~" & "~myToString(maskAllElse)~"));}\n"
112112
// constants

source/mir/functional.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Unref!(RefTuple!T) unref(V : RefTuple!T, T...)(V value)
139139
{
140140
typeof(return) ret;
141141
foreach(i, ref elem; ret.expand)
142-
elem = value.expand[i].unref;
142+
elem = unref(value.expand[i]);
143143
return ret;
144144
}
145145

source/mir/ndslice/algorithm.d

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ template frontOf(size_t N)
7575
}
7676
}
7777

78+
template allFlattened(args...)
79+
{
80+
static if (args.length)
81+
{
82+
alias arg = args[0];
83+
@optmath @property fwd()(){
84+
import mir.ndslice.topology: flattened;
85+
return arg.flattened;
86+
}
87+
alias allFlattened = AliasSeq!(fwd, allFlattened!(args[1..$]));
88+
}
89+
else
90+
alias allFlattened = AliasSeq!();
91+
}
92+
7893
private template areAllContiguousTensors(Slices...)
7994
{
8095
import mir.ndslice.traits: isContiguousSlice;
@@ -84,28 +99,6 @@ private template areAllContiguousTensors(Slices...)
8499
enum areAllContiguousTensors = false;
85100
}
86101

87-
private template Flattened(Slice)
88-
{
89-
import mir.ndslice.topology: flattened;
90-
alias Flattened = typeof(Slice.init.flattened);
91-
}
92-
93-
private alias FlattenedList(Slices...) = staticMap!(Flattened, Slices);
94-
95-
private void createVectors(Args...)(ref Args args)
96-
{
97-
static assert(Args.length % 2 == 0);
98-
alias slices = args[0 .. $ / 2];
99-
alias vectors = args[$ / 2 .. $];
100-
foreach (i, ref vector; vectors)
101-
{
102-
import mir.ndslice.topology;
103-
vector = slices[i].flattened;
104-
static if (i)
105-
vector._lengths[0] = vectors[0]._lengths[0]; // hint for compiler
106-
}
107-
}
108-
109102
version(LDC) {}
110103
else version (Windows) {}
111104
else version (X86_64)
@@ -224,9 +217,7 @@ template reduce(alias fun)
224217
slices.checkShapesMatch;
225218
static if (areAllContiguousTensors!Slices)
226219
{
227-
FlattenedList!Slices vectors;
228-
createVectors(slices, vectors);
229-
return .reduce!fun(seed, vectors);
220+
return .reduce!fun(seed, allFlattened!slices);
230221
}
231222
else
232223
{
@@ -247,9 +238,7 @@ template reduce(alias fun)
247238
slices.checkShapesMatch;
248239
static if (areAllContiguousTensors!Slices)
249240
{
250-
FlattenedList!Slices vectors;
251-
createVectors(slices, vectors);
252-
return .reduce!fun(seed, vectors);
241+
return .reduce!fun(seed, allFlattened!slices);
253242
}
254243
else
255244
{
@@ -458,9 +447,7 @@ template each(alias fun)
458447
slices.checkShapesMatch;
459448
static if (areAllContiguousTensors!Slices)
460449
{
461-
FlattenedList!Slices vectors;
462-
createVectors(slices, vectors);
463-
.each!fun(vectors);
450+
.each!fun(allFlattened!slices);
464451
}
465452
else
466453
{
@@ -1471,9 +1458,7 @@ template any(alias pred = "a")
14711458
slices.checkShapesMatch;
14721459
static if (areAllContiguousTensors!Slices)
14731460
{
1474-
FlattenedList!Slices vectors;
1475-
createVectors(slices, vectors);
1476-
return .any!pred(vectors);
1461+
return .any!pred(allFlattened!slices);
14771462
}
14781463
else
14791464
{
@@ -1617,9 +1602,7 @@ template all(alias pred = "a")
16171602
slices.checkShapesMatch;
16181603
static if (areAllContiguousTensors!Slices)
16191604
{
1620-
FlattenedList!Slices vectors;
1621-
createVectors(slices, vectors);
1622-
return .all!pred(vectors);
1605+
return .all!pred(allFlattened!slices);
16231606
}
16241607
else
16251608
{
@@ -1753,9 +1736,7 @@ template count(alias fun)
17531736
else
17541737
static if (areAllContiguousTensors!Slices)
17551738
{
1756-
FlattenedList!Slices vectors;
1757-
createVectors(slices, vectors);
1758-
return .count!fun(vectors);
1739+
return .count!fun(allFlattened!slices);
17591740
}
17601741
else
17611742
{

source/mir/ndslice/allocation.d

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,24 @@ ContiguousSlice!(N, T)
8080
/// ditto
8181
auto slice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
8282
{
83-
alias T = Unqual!(slice.DeepElemType);
84-
static if (hasElaborateAssign!T)
85-
alias fun = .slice;
83+
if (__ctfe)
84+
{
85+
import mir.ndslice.topology: flattened;
86+
import mir.array.allocation: array;
87+
return slice.flattened.array.sliced(slice.shape);
88+
}
8689
else
87-
alias fun = .uninitSlice;
88-
auto ret = (shape)@trusted{ return fun!T(shape);}(slice.shape);
89-
ret[] = slice;
90-
auto retq = ()@trusted{ return (cast(slice.DeepElemType*)ret._iterator).sliced(ret.shape); }();
91-
return retq;
90+
{
91+
import std.backdoor: emplaceRef;
92+
alias E = slice.DeepElemType;
93+
94+
auto result = (() @trusted => slice.shape.uninitSlice!(Unqual!E))();
95+
96+
import mir.ndslice.algorithm: each;
97+
each!((ref to, auto ref from) => emplaceRef!E(to, from))(result, slice);
98+
99+
return (() @trusted => cast(Slice!(Contiguous, [packs[0]], E*)) result)();
100+
}
92101
}
93102

94103
///
@@ -197,53 +206,40 @@ makeSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths, T
197206
return array.sliced(lengths);
198207
}
199208

200-
///// ditto
201-
//ContiguousSlice!(N, T)
202-
//makeSlice(T,
203-
// Flag!`replaceArrayWithPointer` ra = Yes.replaceArrayWithPointer,
204-
// Allocator,
205-
// SliceKind kind, size_t[] packs, Iterator)(auto ref Allocator alloc, Slice!(kind, packs, Iterator) slice)
206-
//{
207-
// import std.experimental.allocator : makeArray;
208-
// import mir.ndslice.topology : flattened;
209-
// auto array = alloc.makeArray!T(slice.flattened);
210-
// auto _slice = array.sliced!ra(slice.shape);
211-
// return typeof(return)(array, _slice);
212-
//}
213-
214-
/////
215-
version(mir_test)
216-
//@nogc unittest
217-
//{
218-
// import std.experimental.allocator;
219-
// import std.experimental.allocator.mallocator;
220-
221-
// auto tup = makeSlice!int(Mallocator.instance, 2, 3, 4);
209+
/// ditto
210+
auto makeSlice(Allocator, SliceKind kind, size_t[] packs, Iterator)
211+
(auto ref Allocator allocator, Slice!(kind, packs, Iterator) slice)
212+
{
213+
import std.backdoor: emplaceRef;
214+
alias E = slice.DeepElemType;
222215

223-
// assert(tup.array.length == 24);
224-
// assert(tup.slice.elementsCount == 24);
225-
// assert(tup.array.ptr == &tup.slice[0, 0, 0]);
216+
auto result = allocator.makeUninitSlice!(Unqual!E)(slice.shape);
226217

227-
// //makes duplicate using `makeSlice`
228-
// tup.slice[0, 0, 0] = 3;
229-
// auto dup = makeSlice(Mallocator.instance, tup.slice);
230-
// assert(dup.slice == tup.slice);
218+
import mir.ndslice.algorithm: each;
219+
each!((ref to, auto ref from) => emplaceRef!E(to, from))(result, slice);
231220

232-
// Mallocator.instance.dispose(tup.array);
233-
// Mallocator.instance.dispose(dup.array);
234-
//}
221+
return cast(Slice!(Contiguous, [packs[0]], E*)) result;
222+
}
235223

236224
/// Initialization with default value
237225
version(mir_test)
238226
@nogc unittest
239227
{
240228
import std.experimental.allocator;
241229
import std.experimental.allocator.mallocator;
230+
import mir.ndslice.algorithm: all;
231+
import mir.ndslice.topology: map;
242232

243-
auto sl = makeSlice(Mallocator.instance, [2, 3, 4], 10);
233+
auto sl = Mallocator.instance.makeSlice([2, 3, 4], 10);
244234
auto ar = sl.field;
245-
assert(sl[1, 1, 1] == 10);
235+
assert(sl.all!"a == 10");
236+
237+
auto sl2 = Mallocator.instance.makeSlice(sl.map!"a * 2");
238+
auto ar2 = sl2.field;
239+
assert(sl2.all!"a == 20");
240+
246241
Mallocator.instance.dispose(ar);
242+
Mallocator.instance.dispose(ar2);
247243
}
248244

249245
version(mir_test)
@@ -270,6 +266,7 @@ Returns:
270266
+/
271267
ContiguousSlice!(N, T)
272268
makeUninitSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths...)
269+
if (N)
273270
{
274271
if (immutable len = lengthsProduct(lengths))
275272
{

source/mir/ndslice/iterator.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ auto mapIterator(alias fun, Iterator)(Iterator iterator)
635635
&& !__traits(compiles, Iterator.__map!fun(iterator)))
636636
{
637637
// https://github.com/libmir/mir-algorithm/issues/111
638-
pragma(msg, __FUNCTION__~" not coalescing chained map calls into a single lambda, possibly because of multiple embedded context pointers");
638+
debug(mir) pragma(msg, __FUNCTION__~" not coalescing chained map calls into a single lambda, possibly because of multiple embedded context pointers");
639639
return MapIterator!(Iterator, fun)(iterator);
640640
}
641641
else

source/mir/ndslice/topology.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,7 +2266,7 @@ template map(fun...)
22662266
static if (packs.length == 1)
22672267
{
22682268
import mir.ndslice.iterator: mapIterator;
2269-
auto iterator = slice._iterator.mapIterator!f;
2269+
auto iterator = mapIterator!f(slice._iterator);
22702270
return Slice!(kind, packs, typeof(iterator))(slice._lengths, slice._strides, iterator);
22712271
}
22722272
// Specialization for packed tensors (tensors composed of tensors).

0 commit comments

Comments
 (0)