Skip to content

Commit

Permalink
v2: ndslice API rework, extern(C++) support (#143)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
9il authored Jul 31, 2018
1 parent 77723d5 commit 581c0ca
Show file tree
Hide file tree
Showing 40 changed files with 4,419 additions and 5,956 deletions.
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
platform: x64
environment:
matrix:
- DC: dmd
DVersion: 2.080.0
arch: x64
- DC: dmd
DVersion: 2.080.0
arch: x86
# - DC: dmd
# DVersion: 2.080.0
# arch: x64
# - DC: dmd
# DVersion: 2.080.0
# arch: x86
- DC: ldc
DVersion: '1.9.0'
arch: x64
Expand Down
26 changes: 26 additions & 0 deletions cpp_example/eye.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module eye;

import mir.ndslice;

extern(C++, Space)
{
Slice!(double*, 2) eye(size_t n) nothrow @nogc
{
auto ret = stdcUninitSlice!double(n, n);
ret[] = 0;
ret.diagonal[] = 1;
return ret;
}

void printMatrix(Slice!(double*, 2) matrix)
{
import core.stdc.stdio;

foreach(row; matrix)
{
foreach(e; row)
printf("%f ", e);
printf("\n");
}
}
}
16 changes: 16 additions & 0 deletions cpp_example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <cstdlib>
#include "mir/ndslice.h"

namespace Space
{
mir_slice<double*, 2> eye(size_t n);
void printMatrix(mir_slice<double*, 2> matrix);
}

int main()
{
mir_slice<double*, 2> matrix = Space::eye(3);
Space::printMatrix(matrix);
std::free(matrix._iterator);
return 0;
}
3 changes: 3 additions & 0 deletions cpp_example/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ldmd2 -betterC -O -inline -release eye.d -I../source -c
g++ main.cpp eye.o -std=c++11 -I../include
./a.out
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PACKAGE_mir_internal = utility
PACKAGE_mir_interpolate = package constant linear spline pchip utility
PACKAGE_mir_math = constant common sum numeric package
PACKAGE_mir_math_func = expdigamma
PACKAGE_mir_ndslice_connect = cpp cpython
PACKAGE_mir_ndslice_connect = cpython

PACKAGE_mir_ndslice = \
algorithm\
Expand Down
31 changes: 31 additions & 0 deletions include/mir/ndslice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
************ Mir-Algorithm ************
The module provides wrappers for $(SUBREF slice, Slice) that
can be used as arguments for C++ functions.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
Copyright: Copyright © 2017-, Ilya Yaroshenko
Authors: Ilya Yaroshenko
*/
#include <cstddef>

// It is out of ndslice namespace because of a DMD mangling bug.
enum class mir_slice_kind : int
{
universal = 0,
canonical = 1,
contiguous = 2
};

template <
typename Iterator,
size_t N = 1,
mir_slice_kind kind = mir_slice_kind::contiguous
>
struct mir_slice
{
size_t _lengths[N];
ptrdiff_t _strides[kind == mir_slice_kind::universal ? N : kind == mir_slice_kind::canonical ? N - 1 : 0];
Iterator _iterator;
};
23 changes: 23 additions & 0 deletions include/mir/series.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "mir/ndslice.h"

template <
typename Index,
typename Data
>
struct mir_observation
{
Index _index;
Data _data;
}

template <
typename IndexIterator,
typename Iterator,
size_t N = 1,
mir_slice_kind kind = mir_slice_kind::contiguous
>
struct mir_series
{
mir_slice<Iterator, N, kind> _data;
IndexIterator _index;
};
56 changes: 0 additions & 56 deletions include/ndslice.h

This file was deleted.

Loading

0 comments on commit 581c0ca

Please sign in to comment.