-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add rcarray C++ header
- Loading branch information
Showing
11 changed files
with
234 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters