Skip to content

Commit 581c0ca

Browse files
authored
v2: ndslice API rework, extern(C++) support (#143)
* v2: ndslice API rework * update combinatorics * uncomment code * clean Slice * fix example * minor cleanup * clean code * move mir.ndslice.algorithm -> mir.algorithm.iteration * add cpp_example * fix docs * update example * comment out DMD
1 parent 77723d5 commit 581c0ca

40 files changed

+4419
-5956
lines changed

appveyor.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
platform: x64
22
environment:
33
matrix:
4-
- DC: dmd
5-
DVersion: 2.080.0
6-
arch: x64
7-
- DC: dmd
8-
DVersion: 2.080.0
9-
arch: x86
4+
# - DC: dmd
5+
# DVersion: 2.080.0
6+
# arch: x64
7+
# - DC: dmd
8+
# DVersion: 2.080.0
9+
# arch: x86
1010
- DC: ldc
1111
DVersion: '1.9.0'
1212
arch: x64

cpp_example/eye.d

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module eye;
2+
3+
import mir.ndslice;
4+
5+
extern(C++, Space)
6+
{
7+
Slice!(double*, 2) eye(size_t n) nothrow @nogc
8+
{
9+
auto ret = stdcUninitSlice!double(n, n);
10+
ret[] = 0;
11+
ret.diagonal[] = 1;
12+
return ret;
13+
}
14+
15+
void printMatrix(Slice!(double*, 2) matrix)
16+
{
17+
import core.stdc.stdio;
18+
19+
foreach(row; matrix)
20+
{
21+
foreach(e; row)
22+
printf("%f ", e);
23+
printf("\n");
24+
}
25+
}
26+
}

cpp_example/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <cstdlib>
2+
#include "mir/ndslice.h"
3+
4+
namespace Space
5+
{
6+
mir_slice<double*, 2> eye(size_t n);
7+
void printMatrix(mir_slice<double*, 2> matrix);
8+
}
9+
10+
int main()
11+
{
12+
mir_slice<double*, 2> matrix = Space::eye(3);
13+
Space::printMatrix(matrix);
14+
std::free(matrix._iterator);
15+
return 0;
16+
}

cpp_example/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ldmd2 -betterC -O -inline -release eye.d -I../source -c
2+
g++ main.cpp eye.o -std=c++11 -I../include
3+
./a.out

doc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PACKAGE_mir_internal = utility
3838
PACKAGE_mir_interpolate = package constant linear spline pchip utility
3939
PACKAGE_mir_math = constant common sum numeric package
4040
PACKAGE_mir_math_func = expdigamma
41-
PACKAGE_mir_ndslice_connect = cpp cpython
41+
PACKAGE_mir_ndslice_connect = cpython
4242

4343
PACKAGE_mir_ndslice = \
4444
algorithm\

include/mir/ndslice.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
************ Mir-Algorithm ************
3+
4+
The module provides wrappers for $(SUBREF slice, Slice) that
5+
can be used as arguments for C++ functions.
6+
7+
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
8+
Copyright: Copyright © 2017-, Ilya Yaroshenko
9+
Authors: Ilya Yaroshenko
10+
*/
11+
#include <cstddef>
12+
13+
// It is out of ndslice namespace because of a DMD mangling bug.
14+
enum class mir_slice_kind : int
15+
{
16+
universal = 0,
17+
canonical = 1,
18+
contiguous = 2
19+
};
20+
21+
template <
22+
typename Iterator,
23+
size_t N = 1,
24+
mir_slice_kind kind = mir_slice_kind::contiguous
25+
>
26+
struct mir_slice
27+
{
28+
size_t _lengths[N];
29+
ptrdiff_t _strides[kind == mir_slice_kind::universal ? N : kind == mir_slice_kind::canonical ? N - 1 : 0];
30+
Iterator _iterator;
31+
};

include/mir/series.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "mir/ndslice.h"
2+
3+
template <
4+
typename Index,
5+
typename Data
6+
>
7+
struct mir_observation
8+
{
9+
Index _index;
10+
Data _data;
11+
}
12+
13+
template <
14+
typename IndexIterator,
15+
typename Iterator,
16+
size_t N = 1,
17+
mir_slice_kind kind = mir_slice_kind::contiguous
18+
>
19+
struct mir_series
20+
{
21+
mir_slice<Iterator, N, kind> _data;
22+
IndexIterator _index;
23+
};

include/ndslice.h

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)