Skip to content

Commit

Permalink
add rcarray C++ header (#160)
Browse files Browse the repository at this point in the history
* add rcarray C++ header
  • Loading branch information
9il authored Nov 20, 2018
1 parent 639fc7a commit f4061d9
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 18 deletions.
22 changes: 16 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
language: d
sudo: required
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
packages:
- g++-7
- gcc-7-multilib
- clang-5.0
packages:
- pkg-config
d:
Expand All @@ -12,13 +21,14 @@ branches:
only:
- master
env:
- ARCH="x86_64"
- CC="clang-5.0" CXX="clang++-5.0" ARCH="x86_64"

matrix:
include:
- {os: linux, d: ldc-beta, env: ARCH="x86", addons: {apt: {packages: [[gcc-multilib]]}}}
- {os: linux, d: ldc, env: ARCH="x86", addons: {apt: {packages: [[gcc-multilib]]}}}
- {os: linux, d: dmd-beta, env: ARCH="x86", addons: {apt: {packages: [[gcc-multilib]]}}}
- {os: linux, d: dmd, env: ARCH="x86", addons: {apt: {packages: [[gcc-multilib]]}}}
- {os: linux, d: ldc-beta, env: CC="clang-5.0" CXX="clang++-5.0" ARCH="x86"}
- {os: linux, d: ldc, env: CC="clang-5.0" CXX="clang++-5.0" ARCH="x86"}
- {os: linux, d: dmd-beta, env: CC="clang-5.0" CXX="clang++-5.0" ARCH="x86"}
- {os: linux, d: dmd, env: CC="clang-5.0" CXX="clang++-5.0" ARCH="x86"}
allow_failures:
- {d: dmd-nightly}
- {d: ldc-beta}
Expand All @@ -37,7 +47,7 @@ script:
- travis_wait 100 dub test --arch "$ARCH" --build=unittest-cov
- travis_wait 100 dub test --arch "$ARCH" -c dips
- ./test_examples.sh
- meson build -D with_test=true && cd build && ninja -j4 && ninja -j4 test -v && cd .. # TODO: 32bit meson test
- meson build -D with_test=true --default-library=static && cd build && ninja -j4 && ninja -j4 test -v && cd .. # TODO: 32bit meson test
# - travis_wait 100 dub test --arch "$ARCH" --build=unittest-release

after_success:
Expand Down
12 changes: 11 additions & 1 deletion cpp_example/eye.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern(C++, Space)
return ret;
}

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

Expand All @@ -24,3 +24,13 @@ extern(C++, Space)
}
}
}


// Space::printMatrix(mir_slice<double*, 2ul, (mir_slice_kind)2>)
// Space::printMatrix(mir_slice<double*, 2ull, (mir_slice_kind)2>)
// Space::reverseRcSlice(mir_slice<mir_rci<double>, 1ull, (mir_slice_kind)2>&)

//Space::printMatrix(mir_slice<double*, 2ul, (mir_slice_kind)2>)


pragma(msg, printMatrix.mangleof);
20 changes: 20 additions & 0 deletions cpp_example/init_rcarray.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module init_rcarray;

import mir.ndslice;
import mir.rcarray;

extern(C++, Space)
{
void initWithIota(ref RCArray!double a)
{
foreach(i, ref e; a)
e = i;
}

void reverseRcSlice(ref Slice!(RCI!double) a)
{
import mir.utility: swap;
foreach(i; 0 .. a.length / 2)
swap(a[i], a[$ - 1 - i]);
}
}
24 changes: 24 additions & 0 deletions cpp_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include "mir/rcarray.h"
#include "mir/ndslice.h"

namespace Space
{
mir_slice<double*, 2> eye(size_t n);
void printMatrix(mir_slice<double*, 2> matrix);
void initWithIota(mir_rcarray<double> &a);
void reverseRcSlice(mir_slice<mir_rci<double>>& a);
}

int main()
{
mir_slice<double*, 2> matrix = Space::eye(3);
Space::printMatrix(matrix);
std::free(matrix._iterator);


// test rcarray constructors and destructors
mir_rcarray<double> a(3); // [NaN, NaN, NaN]
assert(a.size() == 3);

Space::initWithIota(a); //[0, 1, 2]
auto b = a; // check copy constructor
auto c = b.asSlice();
auto d = c; // check copy constructor
Space::reverseRcSlice(d); //[2, 1, 0]

// reversed 0 1 2 (iota)
assert(a[0] == 2);
assert(a[1] == 1);
assert(a[2] == 0);

assert(d._iterator._iterator == a.data());

return 0;
}
7 changes: 7 additions & 0 deletions cpp_example/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mir_algorithm_cpp_test_exe = executable(meson.project_name() + '-test',
['eye.d', 'init_rcarray.d', 'main.cpp'],
include_directories: mir_algorithm_dir,
dependencies: mir_algorithm_dep,
)

test(meson.project_name() + '-cpp-test', mir_algorithm_cpp_test_exe)
2 changes: 1 addition & 1 deletion cpp_example/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ldmd2 -betterC -O -inline -release eye.d -I../source -c
ldmd2 -betterC -O -inline -release eye.d init_rcarray.d -I../source -c
g++ main.cpp eye.o -std=c++11 -I../include
./a.out
16 changes: 14 additions & 2 deletions include/mir/ndslice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
Copyright: Copyright © 2017-, Ilya Yaroshenko
Authors: Ilya Yaroshenko
*/

#ifndef MIR_NDSLICE

#define MIR_NDSLICE

#include <cstddef>
#include <cstdint>

#if INTPTR_MAX == INT32_MAX
#define mir_size_t unsigned int
#define mir_ptrdiff_t int
#elif INTPTR_MAX == INT64_MAX
#define mir_size_t unsigned long long
#define mir_ptrdiff_t long long
#ifdef _WIN32
#define mir_size_t unsigned long long
#define mir_ptrdiff_t long long
#else
#define mir_size_t unsigned long
#define mir_ptrdiff_t long
#endif
#else
#error "Environment not 32 or 64-bit."
#endif
Expand All @@ -40,3 +50,5 @@ struct mir_slice
mir_ptrdiff_t _strides[kind == mir_slice_kind::universal ? N : kind == mir_slice_kind::canonical ? N - 1 : 0];
Iterator _iterator;
};

#endif
103 changes: 103 additions & 0 deletions include/mir/rcarray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef MIR_RCARRAY

#define MIR_RCARRAY

#include <cassert>
#include <stdexcept>
#include "mir/ndslice.h"

template <typename T>
struct mir_rci;

template <typename T>
struct mir_rcarray
{
private:

void* _context;

public:

~mir_rcarray();
mir_rcarray(mir_rcarray& rhs);
bool initialize(size_t length, unsigned int alignment, bool deallocate, bool initialize);

mir_slice<mir_rci<T>> asSlice();

inline mir_rcarray(size_t length, unsigned int alignment = alignof(T), bool deallocate = true, bool initialize = true)
{
if (!this->initialize(length, alignment, deallocate, initialize))
{
throw std::runtime_error("mir_rcarray: out of memory arror.");
}
}

inline size_t size() noexcept
{
return _context ? *(size_t*)((char*)_context + sizeof(void*)) : 0;
}

inline T& at(size_t index)
{
assert(index < this->size());
return ((T*)((char*)_context + sizeof(void*) * 4))[index];
}

inline const T& at(size_t index) const
{
assert(index < this->size());
return ((const T*)((char*)_context + sizeof(void*) * 4))[index];
}

inline T& operator[](size_t index)
{
assert(index < this->size());
return ((T*)((char*)_context + sizeof(void*) * 4))[index];
}

inline const T& operator[](size_t index) const
{
assert(index < this->size());
return ((const T*)((char*)_context + sizeof(void*) * 4))[index];
}

inline T* data() noexcept
{
return _context ? (T*)((char*)_context + sizeof(void*) * 4) : NULL;
}

inline T* begin() noexcept
{
return _context ? (T*)((char*)_context + sizeof(void*) * 4) : NULL;
}

inline const T* cbegin() const noexcept
{
return _context ? (const T*)((char*)_context + sizeof(void*) * 4) : NULL;
}

inline T* end() noexcept
{
return this->begin() + this->size();
}

inline const T* cend() const noexcept
{
return this->cbegin() + this->size();
}
};


template <typename T>
struct mir_rci
{
public:

T* _iterator;
mir_rcarray<T> _array;

~mir_rci() = default;
mir_rci(mir_rci& rhs) = default;
};

#endif
6 changes: 6 additions & 0 deletions include/mir/series.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifndef MIR_SERIES

#define MIR_SERIES

#include "mir/ndslice.h"

template <
Expand All @@ -21,3 +25,5 @@ struct mir_series
mir_slice<Iterator, N, kind> _data;
IndexIterator _index;
};

#endif
16 changes: 15 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
project('mir-algorithm', 'd', version : '3.1.0', license: 'BSL-1.0')
project('mir-algorithm', 'cpp', 'd', version : '3.1.0', license: 'BSL-1.0',
default_options : ['cpp_std=c++1z'])


add_global_arguments([
'-dip1008',
], language: 'd')

add_project_arguments([
'-dip25',
'-dip1000',
], language: 'd')


mir_algorithm_dir = include_directories('source/', 'include/')

Expand Down Expand Up @@ -89,4 +101,6 @@ if get_option('with_test')

test(meson.project_name() + '-test', mir_algorithm_test_exe)

subdir('cpp_example')

endif
24 changes: 17 additions & 7 deletions source/mir/rcarray.d
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,20 @@ struct mir_rcarray(T)
}

///
pragma(inline, false)
this(ref typeof(this) rhs) pure nothrow @nogc
{
this._context = rhs._context;
this.__xpostblit;
}

///
auto asSlice() scope return @property
{
import mir.ndslice.slice: mir_slice;
alias It = mir_rci!T;
return mir_slice!It([length], It((()@trusted => ptr)(), this));
}
}
else
{
Expand All @@ -156,6 +165,14 @@ struct mir_rcarray(T)
{
return initializeImpl(length, alignment, deallocate, initialize);
}

///
auto asSlice()() scope return @property
{
import mir.ndslice.slice: mir_slice;
alias It = mir_rci!T;
return mir_slice!It([length], It((()@trusted => ptr)(), this));
}
}

///
Expand Down Expand Up @@ -344,13 +361,6 @@ struct mir_rcarray(T)
{
return _context !is null ? _context.length : 0;
}

auto asSlice()() scope return @property
{
import mir.ndslice.slice: mir_slice;
alias It = mir_rci!T;
return mir_slice!It([length], It((()@trusted => ptr)(), this));
}
}

/// ditto
Expand Down

0 comments on commit f4061d9

Please sign in to comment.