Skip to content

Commit

Permalink
test module init (#57)
Browse files Browse the repository at this point in the history
* test module init

* update - array

* update auto-tests-github

* update

* use -y to avoid ask when install package

* update again

* array and array-test base
  • Loading branch information
Sunrisepeak authored Jul 5, 2024
1 parent 4af3717 commit 78c7268
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 312 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/dstruct-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,38 @@ jobs:

runs-on: ubuntu-latest

defaults:
run:
working-directory: tests

steps:
- uses: actions/checkout@v3
- uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: branch@master
- name: set build mode
run: xmake f -m debug
run: xmake f -m debug -y
- name: build examples|test
run: xmake
run: xmake -y
- name: run all test
run: xmake r

build-win:

runs-on: windows-latest

defaults:
run:
working-directory: tests

steps:
- uses: actions/checkout@v3
- uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: branch@master
- name: set build mode
run: xmake f -m debug
run: xmake f -m debug -y
- name: build examples|test
run: xmake
run: xmake -y
- name: run all test
run: xmake r
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DStruct
# DStruct | 重构WIP - [开发看板](https://github.com/users/Sunrisepeak/projects/12)

DStruct 是一个易于移植且结构简洁的数据结构模板库

Expand Down
13 changes: 0 additions & 13 deletions core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@

namespace dstruct {

#define DSTRUCT_TYPE_SPEC_HELPER(DStruct) \
public: \
using ValueType = typename DStruct::ValueType; \
using ReferenceType = typename DStruct::ReferenceType; \
using ConstReferenceType = typename DStruct::ConstReferenceType; \
using PointerType = typename DStruct::PointerType; \
using ConstPointerType = typename DStruct::ConstPointerType; \
using SizeType = typename DStruct::SizeType; \
using DifferenceType = typename DStruct::DifferenceType; \
public: \
using IteratorType = typename DStruct::IteratorType; \
using ConstIteratorType = typename DStruct::ConstIteratorType;

#define DSTRUCT_COPY_SEMANTICS(DStruct) \
DStruct(const DStruct &ds) : DStruct() { *this = ds; } \
DStruct & operator=(const DStruct &ds) { \
Expand Down
113 changes: 75 additions & 38 deletions core/ds/array/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,125 @@
namespace dstruct {

template <typename T, size_t N>
class Array : public DStructTypeSpec_<T, dstruct::Alloc /*unused*/ , PrimitiveIterator> {
class Array : public DStructTypeSpec_<T, void, PrimitiveIterator> {

DSTRUCT_TYPE_SPEC_HELPER(Array)

public: // big Five

Array() = default;

Array(typename Array::ConstReferenceType element) {
Array(ConstReferenceType element) {
for (int i = 0; i < N; i++) {
mC_d[i] = element;
mData[i] = element;
}
}

Array(const Array &arr) { *this = arr; }
Array & operator=(const Array &arr) {
for (int i = 0; i < N; i++) {
mC_d[i] = arr.mC_d[i];
Array(port::initializer_list<T> &&list) {
auto it = list.begin();
int i = 0;

while (it != list.end() && i < N) {
mData[i] = dstruct::move(*it);
it++;
i++;
}
return *this;
}

Array(Array &&arr) { *this = dstruct::move(arr); };
Array & operator=(Array &&arr) {
for (int i = 0; i < N; i++) mC_d[i] = dstruct::move(arr.mC_d[i]);
return *this;
while (i < N) {
mData[i] = T();
i++;
}
}

~Array() = default; // array: auto-destroy for every element

public: // Capacity
bool empty() const {
bool empty() const noexcept {
return N == 0;
}

typename Array::SizeType size() const {
SizeType size() const noexcept {
return N;
}

typename Array::SizeType capacity() const {
return N;
SizeType capacity() const noexcept {
return N == 0 ? 1 : N;
}

public: // Access
typename Array::ConstReferenceType back() const {
return mC_d[N - 1];
ConstReferenceType back() const noexcept {
return mData[N - 1];
}

ReferenceType back() noexcept {
return mData[N - 1];
}

ConstReferenceType front() const noexcept {
return mData[0];
}

typename Array::ConstReferenceType front() const {
return mC_d[0];
ReferenceType front() noexcept {
return mData[0];
}

typename Array::ConstReferenceType operator[](int index) const {
ConstReferenceType operator[](int index) const noexcept {
if (index < 0)
index = N + index;
return mC_d[index];
return mData[index];
}

public: // Modifiers
typename Array::ReferenceType operator[](int index) {
ReferenceType operator[](int index) noexcept {
if (index < 0)
index = N + index;
return mC_d[index];
return mData[index];
}

public: // iterator
typename Array::IteratorType begin() {
return mC_d;
PointerType data() noexcept {
return mData;
}

ConstPointerType data() const noexcept {
return mData;
}

typename Array::ConstIteratorType begin() const {
return mC_d;
public: // iterator
IteratorType begin() noexcept { return mData; }
ConstIteratorType begin() const noexcept { return mData; }
ConstIteratorType cbegin() const noexcept { return mData; }

IteratorType end() noexcept { return mData + N; }
ConstIteratorType end() const noexcept { return mData + N; }
ConstIteratorType cend() const noexcept { return mData + N; }

public: // Method chaining
using SortCmpFunc = bool (*)(ConstReferenceType, ConstReferenceType);
Array & sort(SortCmpFunc cmp = [](ConstReferenceType a, ConstReferenceType b) { return a < b; }) noexcept {
// tmp-impl
for (int i = 0; i < N - 1; ++i) {
for (int j = 0; j < N - 1 - i; ++j) {
if (!cmp(mData[j], mData[j + 1])) {
dstruct::swap(mData[j], mData[j + 1]);
}
}
}
return *this;
}

typename Array::IteratorType end() {
return mC_d + N;
Array & reverse() {
for (int i = 0; i < N / 2; i++) {
dstruct::swap((*this)[i], (*this)[-1 - i]);
}
return *this;
}

typename Array::ConstIteratorType end() const {
return mC_d + N;
Array & swap(Array &arr) {
for (int i = 0; i < N; i++) {
dstruct::swap(mData[i], arr.mData[i]);
}
return *this;
}

protected:
T mC_d[N == 0 ? 1 : N];
T mData[N == 0 ? 1 : N];
}; // Array

};
Expand Down
20 changes: 20 additions & 0 deletions core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ struct IsPointer<T *> {
const static bool value = true;
};

template <typename T>
struct IsConst {
const static bool value = false;
};

template <typename T>
struct IsConst<const T> {
const static bool value = true;
};

template <typename T>
struct IsConst<const T &> {
const static bool value = true;
};

template <typename T>
struct IsConst<const T *> {
const static bool value = true;
};

template <typename T>
struct less {
bool operator()(const T& a, const T& b) const {
Expand Down
4 changes: 4 additions & 0 deletions dstruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

//#define ENABLE_SMA

// base

#include <core/utils.hpp>

#include <dstruct-static.hpp>

#include <core/ds/Heap.hpp>
Expand Down
47 changes: 20 additions & 27 deletions xmake.lua → examples/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ set_languages("cxx11")

--add_defines("NDEBUG")

add_includedirs(
".",
"tests"
)
add_includedirs("..")

--add_includedirs("port/libc")
--[[
Expand All @@ -16,84 +13,80 @@ target("dstruct_cpp_test")
add_files("tests/cpp_test.cpp")
--]]

target("dstruct_test")
set_kind("binary")
add_files("tests/main.cpp")

target("dstruct_array")
set_kind("binary")
add_files("examples/array/array.cpp")
add_files("array/array.cpp")

target("dstruct_vector")
set_kind("binary")
add_files("examples/array/vector.cpp")
add_files("array/vector.cpp")

target("dstruct_string")
set_kind("binary")
add_files("examples/string.cpp")
add_files("string.cpp")

target("embedded_list")
set_kind("binary")
add_files("examples/linked-list/embedded_list.cpp")
add_files("linked-list/embedded_list.cpp")

target("dstruct_single_linked_list")
set_kind("binary")
add_files("examples/linked-list/single_linked_list.cpp")
add_files("linked-list/single_linked_list.cpp")

target("dstruct_double_linked_list")
set_kind("binary")
add_files("examples/linked-list/double_linked_list.cpp")
add_files("linked-list/double_linked_list.cpp")

target("dstruct_queue")
set_kind("binary")
add_files("examples/queue/queue.cpp")
add_files("queue/queue.cpp")

target("dstruct_deque")
set_kind("binary")
add_files("examples/queue/deque.cpp")
add_files("queue/deque.cpp")

target("dstruct_stack")
set_kind("binary")
add_files("examples/stack/stack.cpp")
add_files("stack/stack.cpp")

target("dstruct_xvalue_stack")
set_kind("binary")
add_files("examples/stack/xvalue_stack.cpp")
add_files("stack/xvalue_stack.cpp")

target("dstruct_heap")
set_kind("binary")
add_files("examples/heap.cpp")
add_files("heap.cpp")

target("dstruct_binary_search_tree")
set_kind("binary")
add_files("examples/tree/binary_search_tree.cpp")
add_files("tree/binary_search_tree.cpp")

target("dstruct_avl_tree")
set_kind("binary")
add_files("examples/tree/avl_tree.cpp")
add_files("tree/avl_tree.cpp")

target("dstruct_ufset")
set_kind("binary")
add_files("examples/set/ufset.cpp")
add_files("set/ufset.cpp")

target("dstruct_map")
set_kind("binary")
add_files("examples/map.cpp")
add_files("map.cpp")

target("dstruct_smemory_vector")
set_kind("binary")
add_files("examples/smemory_vector.cpp")
add_files("smemory_vector.cpp")

-- algorithms
target("dstruct_for_each")
set_kind("binary")
add_files("examples/algorithms/for_each.cpp")
add_files("algorithms/for_each.cpp")

target("dstruct_heap_algo")
set_kind("binary")
add_files("examples/algorithms/heap_algo.cpp")
add_files("algorithms/heap_algo.cpp")

-- other
target("dstruct_static_mem_allocator")
set_kind("binary")
add_files("examples/static_mem_allocator.cpp")
add_files("static_mem_allocator.cpp")
Loading

0 comments on commit 78c7268

Please sign in to comment.