Skip to content

Commit

Permalink
🆕 feat(DevelopingStatusUpdate) : Added developing status
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMargoo committed Sep 16, 2023
1 parent 75d915b commit d5c7b58
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "base-in-dev/vlib/lib/iconv"]
path = base-in-dev/vlib/lib/iconv
url = https://github.com/vovythevov/libiconv-cmake.git
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div align=center>
<img src="./readme-resource/logo.png"/>
<img src="./readme-resource/developing-image.png">
<h1>VuiLib:一个开源,轻巧的 C++ 界面库</h1>
<img src="https://img.shields.io/badge/build-passing-successimportant"/>
<img src="https://img.shields.io/badge/license-MIT-green"/>
<img src="https://img.shields.io/badge/version-3.x.x%20beta-orange"/>
<img src="https://img.shields.io/badge/version-3.x.x%20developing-orange"/>
</div>

## Installation - 安装
Expand Down
2 changes: 2 additions & 0 deletions base-in-dev/vlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include_directories(inc)
include_directories(inc/kernel)
include_directories(inc/kernel/base)
include_directories(inc/kernel/mem)
include_directories(lib)

add_executable(vlib
inc/kernel/base/vbase.h
Expand Down Expand Up @@ -39,6 +40,7 @@ add_executable(vlib
inc/kernel/thread/vthreadpool.h
src/kernel/thread/vthreadpool.cpp
inc/kernel/container/varray.h
inc/kernel/container/vstring.h
)

target_include_directories(vlib PRIVATE inc)
Expand Down
57 changes: 52 additions & 5 deletions base-in-dev/vlib/inc/kernel/container/varray.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class VArray {
using ArrayType = VArray<Type, AllocatorType, TypeExtractor>;
using Pointer = TypeExtractor::Pointer;
using ConstRefer = TypeExtractor::ConstRefer;
using CopyRefer = TypeExtractor::CopyRefer;
using MoveRefer = TypeExtractor::MoveRefer;
using Iterator = VArrayIterator<Type>;

public:
Expand Down Expand Up @@ -168,6 +168,25 @@ class VArray {

return *this;
}
bool operator==(const ArrayType &Object) noexcept {
if (Object.GetSize() != GetSize()) {
return false;
}
size_t Count = 0;
bool Flag = true;
Object.Visit([this, &Count, &Flag](const Type &Value) {
if (Value != At(Count)) {
Flag = false;
}

++Count;
});

return Flag;
}
bool operator!=(const ArrayType &Object) noexcept {
return !operator==(Object);
}

public:
/**
Expand Down Expand Up @@ -231,14 +250,14 @@ class VArray {
* \brief Push value from back
* \param Value The value that need to be pushed
*/
void Push(const ConstRefer Value) {
void Push(ConstRefer Value) {
_InsertFromBack(Value);
}
/**
* \brief Push value from back
* \param Value The value that need to be pushed
*/
void Push(CopyRefer Value) {
void Push(MoveRefer Value) {
_InsertFromBack(std::move(Value));
}
/**
Expand All @@ -249,6 +268,18 @@ class VArray {
Push(Element);
}
}
/**
* \brief Push a element from constructor parameter
*/
template <class... ArgType>
void BuildPush(ArgType... Argument) {
if (_GetSize() >= Memory.Size) {
_ReallocateMemory(Memory.Size * 2);
}
Memory.Cursor = new (Memory.Cursor) Type(std::forward<ArgType>(Argument)...);

++Memory.Cursor;
}
/**
* \brief Insert an element at specified position
*/
Expand All @@ -258,7 +289,7 @@ class VArray {
/**
* \brief Insert an element at specified position
*/
void Insert(CopyRefer Value, const size_t &Where) {
void Insert(MoveRefer Value, const size_t &Where) {
_InsertByPosition(Value, Where);
}
/**
Expand All @@ -281,6 +312,14 @@ class VArray {
return std::initializer_list<Type>(Memory.Pointer, Memory.Cursor);
}

public:
/**
* \brief Return whether the array is empty
*/
bool IsEmpty() {
return Memory.Pointer == Memory.Cursor;
}

public:
/**
* \brief Delete a element
Expand All @@ -307,7 +346,15 @@ class VArray {
/**
* \brief Provide a function to do some operations of element (Do not operate the array object in the function)
*/
void Range(const std::function<void(ConstRefer)> &Function) {
void Range(const std::function<void(MoveRefer)> &Function) {
for (size_t Count = 0; Count < _GetSize(); ++Count) {
Function(*(Memory.Pointer + Count));
}
}
/**
* \brief Provide a visitor for array
*/
void Visit(const std::function<void(ConstRefer)> &Function) const {
for (size_t Count = 0; Count < _GetSize(); ++Count) {
Function(*(Memory.Pointer + Count));
}
Expand Down
11 changes: 6 additions & 5 deletions base-in-dev/vlib/inc/kernel/container/vdeque.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ class VDequeArray {
VDequeArray(const size_t &InitSize, AllocatorType &ArrayAllocator) noexcept
: Allocator(ArrayAllocator), ExpandSize(InitSize) {
Array = Allocator.template AllocateArray<VDequeNode<NodeType>>(InitSize);
Head = Array + InitSize / 2;
Tail = Head;

Head = Array + InitSize / 2;
Tail = Head;
}

/**
Expand Down Expand Up @@ -121,7 +122,7 @@ class VDequeArray {
void HeadInsert(NodeType &&Value) {
_CheckFrontField();

Head->Value = Value;
Head->Value = *(new (&(Head->Value)) NodeType(std::move(Value)));
Head = Head - 1;
}

Expand Down Expand Up @@ -161,7 +162,7 @@ class VDequeArray {
* \brief Delete the value at the tail of the array
*/
void TailDelete() {
if (Tail != Array + ExpandSize) {
if (Tail != Array + ExpandSize && Head != Tail) {
Tail->Value.~NodeType();
Tail = Tail - 1;
}
Expand Down Expand Up @@ -212,7 +213,7 @@ class VDequeArray {
DiffType PtrDiff = Tail - Head;
DiffType HeadPosition = ExpandSize - (PtrDiff / 2);
for (size_t Count = 0; Count < PtrDiff; ++Count) {
*(NewArea + Count + HeadPosition) = *(Head + Count);
*(NewArea + Count + HeadPosition) = std::move(*(Head + Count));
}

Allocator.template DeleteArray<VDequeNode<NodeType>>(Array, ExpandSize);
Expand Down
1 change: 1 addition & 0 deletions base-in-dev/vlib/inc/kernel/container/viterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* \file viterator.h
* \brief This file contains the definition of various iterator classes.
*/
#pragma once

#include <kernel/container/vtypeextractor.h>

Expand Down
11 changes: 9 additions & 2 deletions base-in-dev/vlib/inc/kernel/container/vqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* \brief The VQueue class is a simple queue data structure.
* It is implemented using the VDeque class.
*/
template <class Type, class AllocatorType, class TypeExtractor = VTypeExtractor<Type>>
template <class Type, class AllocatorType = VMemoryPool, class TypeExtractor = VTypeExtractor<Type>>
class VQueue {
public:
/**
Expand Down Expand Up @@ -81,7 +81,7 @@ class VQueue {
* Pops a value from the back of the queue.
*/
void Pop() {
NativeDeque.PopBack();
NativeDeque.PopFront();
}

/**
Expand Down Expand Up @@ -111,6 +111,13 @@ class VQueue {
return NativeDeque.GetSize();
}

/**
* \brief Check is whether the queue is empty
*/
[[nodiscard]] bool IsEmpty() {
return NativeDeque.IsEmpty();
}

private:
VDeque<Type, AllocatorType, TypeExtractor> NativeDeque; ///< The underlying deque for the queue.
};
36 changes: 36 additions & 0 deletions base-in-dev/vlib/inc/kernel/container/vstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023~Now Margoo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* \file vstring.h
* \brief Provide a string class for the vlib
*/

#pragma once

#include <kernel/container/vtypeextractor.h>

template <class Type>
struct VStringPrivateData {
Type *Pointer;
size_t Length;
};
2 changes: 1 addition & 1 deletion base-in-dev/vlib/inc/kernel/container/vtypeextractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template <class Type>
struct VTypeExtractor {
using Pointer = typename Type *; ///< Pointer to Type
using Refer = typename Type &; ///< Reference to Type
using CopyRefer = typename Type &&; ///< Copy reference (rvalue reference) to Type
using MoveRefer = typename Type &&; ///< Copy reference (rvalue reference) to Type
using ConstType = const Type; ///< Const Type
using ConstPointer = const Type *; ///< Pointer to const Type
using ConstRefer = const Type &; ///< Reference to const Type
Expand Down
30 changes: 29 additions & 1 deletion base-in-dev/vlib/inc/kernel/thread/vthreadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,39 @@
#include <functional>
#include <future>

#include <kernel/container/varray.h>
#include <kernel/container/vqueue.h>

/**
* \brief VThread provide a thread pool which is used for VThread class
*/
class VThreadPool {
public:
explicit VThreadPool(const size_t &ThreadCount = std::thread::hardware_concurrency());
explicit VThreadPool(VMemoryPool &Allocator, const size_t &ThreadCount = std::thread::hardware_concurrency());
template <class FunctionType, typename... Args>
auto Summit(FunctionType &&Function, Args &&...Argument) {
using ReturnType = std::invoke_result_t<FunctionType, Args...>;
std::shared_ptr<std::packaged_task<ReturnType()>> Task = std::make_shared<std::packaged_task<ReturnType()>>(
std::bind(std::forward<FunctionType>(Function), std::forward<Args>(Argument)...));
std::future<ReturnType> Result = Task->get_future();
{
std::unique_lock<std::mutex> ThreadLock(QueueMutex);
if (StopFlag) {
_vdebug_handle().crash("Summit task on a stopped thread pool!");
}

Tasks.Push([Task = std::move(Task)]() { (*Task)(); });
}

Condition.notify_one();
return Result;
}
~VThreadPool();

private:
VArray<std::thread> Threads;
VQueue<std::function<void()>> Tasks;
std::mutex QueueMutex;
std::condition_variable Condition;
bool StopFlag;
};
33 changes: 32 additions & 1 deletion base-in-dev/vlib/src/kernel/thread/vthreadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,38 @@

#include <kernel/thread/vthreadpool.h>

VThreadPool::VThreadPool(const size_t &ThreadCount) {
VThreadPool::VThreadPool(VMemoryPool &Allocator, const size_t &ThreadCount)
: Tasks(Allocator), Threads(Allocator), StopFlag(false) {
for (size_t Count = 0; Count < ThreadCount; ++Count) {
Threads.BuildPush([this]() {
while (true) {
std::function<void()> CurrentTask;
{
std::unique_lock<std::mutex> ThreadLock(this->QueueMutex);
this->Condition.wait(ThreadLock, [this]() { return this->StopFlag || !this->Tasks.IsEmpty(); });

if (this->StopFlag && this->Tasks.IsEmpty()) {
return;
}

CurrentTask = std::move(this->Tasks.GetFront());
this->Tasks.Pop();
}

if (CurrentTask != nullptr) {
CurrentTask();
}
}
});
}
}
VThreadPool::~VThreadPool() {
{
std::unique_lock<std::mutex> ThreadLock(QueueMutex);
StopFlag = true;
}
Condition.notify_all();
for (auto &Thread : Threads) {
Thread.join();
}
}
24 changes: 19 additions & 5 deletions base-in-dev/vlib/tester/mem/mem-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,29 @@

#pragma once

#include "../../lib/iconv/include/export.h"
#include <kernel/container/varray.h>
#include <kernel/thread/vthreadpool.h>

int main() {
VMemoryPool Pool;
VArray<int> TestArray(Pool);
TestArray.Insert({1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0}, 0);
for (auto &Element : TestArray) {
printf("%d", Element);
}
VThreadPool ThreadPool(Pool);
ThreadPool.Summit([]() {
size_t Result = 1 + 1;
printf("%zu ", Result);
});
ThreadPool.Summit([]() {
size_t Result = 2 + 2;
printf("%zu ", Result);
});
ThreadPool.Summit([]() {
size_t Result = 3 + 3;
printf("%zu ", Result);
});
ThreadPool.Summit([]() {
size_t Result = 4 + 4;
printf("%zu ", Result);
});

return 0;
}
1 change: 0 additions & 1 deletion base-in-dev/vlib/tester/tester.cpp
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

Loading

0 comments on commit d5c7b58

Please sign in to comment.