Skip to content

Commit

Permalink
big update.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxueling committed Sep 24, 2024
1 parent 435c88e commit ae933d4
Show file tree
Hide file tree
Showing 73 changed files with 1,393 additions and 1,558 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ cmake_minimum_required(VERSION 3.21)
project(sfc)

add_compile_options(-Wall -Wextra -Wconversion)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
endif()

add_subdirectory(src)
add_subdirectory(test)

if(PROJECT_IS_TOP_LEVEL)
add_subdirectory(benchmark)
endif()
7 changes: 0 additions & 7 deletions benchmark/CMakeLists.txt

This file was deleted.

7 changes: 0 additions & 7 deletions benchmark/logger/CMakeLists.txt

This file was deleted.

11 changes: 0 additions & 11 deletions benchmark/logger/glog.cc

This file was deleted.

29 changes: 0 additions & 29 deletions benchmark/logger/main.cc

This file was deleted.

12 changes: 0 additions & 12 deletions benchmark/logger/sfc_log.cc

This file was deleted.

12 changes: 0 additions & 12 deletions benchmark/logger/spdlog.cc

This file was deleted.

10 changes: 1 addition & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
file(GLOB_RECURSE SRC CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/*.cc")

add_library(sfc ${SRC})

if("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
target_compile_features(sfc PUBLIC cxx_std_20)
else()
target_compile_features(sfc PUBLIC cxx_std_17)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
target_compile_options(sfc PUBLIC -fconcepts)
endif()
endif()
target_compile_features(sfc PUBLIC cxx_std_20)

target_link_options(sfc PUBLIC -rdynamic)
target_include_directories(sfc PUBLIC ${CMAKE_CURRENT_LIST_DIR})
11 changes: 2 additions & 9 deletions src/sfc/alloc/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,17 @@ struct Global {
template <class T>
auto realloc_array(T* old_ptr, usize old_len, usize new_len) -> T* {
const auto max_len = this->usable_size(old_ptr) / sizeof(T);
if (new_len <= max_len) {
if (new_len <= max_len || __is_trivially_copyable(T)) {
const auto layout = Layout::array<T>(old_len);
const auto new_ptr = this->realloc_imp(old_ptr, layout, new_len * sizeof(T));
return static_cast<T*>(new_ptr);
}

const auto new_ptr = this->alloc_array<T>(new_len);
ptr::uninit_move(old_ptr, new_ptr, old_len);
ptr::drop_in_place(old_ptr, old_len);
ptr::drop(old_ptr, old_len);
return new_ptr;
}

template <trait::Copy T>
auto realloc_array(T* old_ptr, usize old_len, usize new_len) -> T* {
const auto m = Layout::array<T>(old_len);
const auto p = this->realloc_imp(old_ptr, m, new_len * sizeof(T));
return static_cast<T*>(p);
}
};

} // namespace sfc::alloc
2 changes: 1 addition & 1 deletion src/sfc/alloc/boxed.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class Box<R(T...)> {
auto operator()(T... args) -> R {
assert_fmt(_ptr._self, "Box::(): deref null object.");

const auto p = static_cast<trait::Any*>(_ptr._self);
const auto p = static_cast<ops::Any*>(_ptr._self);
const auto f = _ptr._meta->_call;
(p->*f)(static_cast<T&&>(args)...);
}
Expand Down
4 changes: 3 additions & 1 deletion src/sfc/alloc/rc.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class Rc {
Rc() noexcept = default;

~Rc() {
if (!_ptr) return;
if (!_ptr) {
return;
}

if (_ptr->dec_strong() == 0) {
mem::drop(*_ptr);
Expand Down
14 changes: 7 additions & 7 deletions src/sfc/alloc/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ class [[nodiscard]] Vec {
return _len != 0;
}

[[sfc_inline]] auto as_slice() const -> Slice<const T> {
[[sfc_inline]] auto as_slice() const -> slice::Slice<const T> {
return {_buf._ptr, _len};
}

[[sfc_inline]] auto as_mut_slice() -> Slice<T> {
[[sfc_inline]] auto as_mut_slice() -> slice::Slice<T> {
return {_buf._ptr, _len};
}

Expand Down Expand Up @@ -212,11 +212,11 @@ class [[nodiscard]] Vec {
return _buf[idx];
}

[[sfc_inline]] auto operator[](Range<> ids) const -> Slice<const T> {
[[sfc_inline]] auto operator[](Range<> ids) const -> slice::Slice<const T> {
return this->as_slice()[ids];
}

[[sfc_inline]] auto operator[](Range<> ids) -> Slice<T> {
[[sfc_inline]] auto operator[](Range<> ids) -> slice::Slice<T> {
return this->as_mut_slice()[ids];
}

Expand Down Expand Up @@ -261,7 +261,7 @@ class [[nodiscard]] Vec {
return;
}

ptr::drop_in_place(_buf._ptr + len, _len - len);
ptr::drop(_buf._ptr + len, _len - len);
_len = len;
}

Expand Down Expand Up @@ -398,12 +398,12 @@ class [[nodiscard]] Vec {
}

public:
using Iter = typename Slice<T>::Iter;
using Iter = typename slice::Iter<const T>;
auto iter() const -> Iter {
return this->as_slice().iter();
}

using IterMut = typename Slice<T>::IterMut;
using IterMut = typename slice::Iter<T>;
auto iter_mut() -> IterMut {
return this->as_mut_slice().iter_mut();
}
Expand Down
7 changes: 3 additions & 4 deletions src/sfc/backtrace/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ static auto cxx_demangle(Str raw) -> String {
return String{raw};
}

auto ret = 0;
char buf[MAX_LEN];
auto len = sizeof(buf);
auto fun = __cxa_demangle(raw.as_ptr(), buf, &len, &ret);
auto res = String{fun ? fun : raw};
return res;
auto ret = 0;
auto res = __cxa_demangle(raw.as_ptr(), buf, &len, &ret);
return String{res ?: raw};
}

Frame::Frame(void* addr) : _addr{addr} {}
Expand Down
95 changes: 95 additions & 0 deletions src/sfc/collections/btree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once

#include "sfc/alloc.h"

namespace sfc::collections::btree {

template <class K, class V, usize B = 6>
struct BTree {
struct Node;

struct Entry {};

Node* _root = nullptr;
usize _len = 0;

public:
BTree() {}

~BTree() {
this->clear();
}

BTree(BTree&& other) noexcept : _root{other._root} {
other._root = nullptr;
}

auto len() const -> usize {
return _len;
}

void clear();

void append(BTree& other);

auto entry(K key) -> Entry;

auto get(const auto& key) const -> Option<const V&>;

auto get_mut(const auto& key) -> Option<V&>;

auto insert(K key, V value) -> Option<V>;

auto remove(const auto& key) -> Option<V>;
};

template <class K, class V, usize B>
struct BTree<K, V, B>::Node {
static constexpr usize CAPACITY = 2 * B - 1;

Node* _parent = nullptr;
u16 _idx = 0;
u16 _len = 0;

mem::Uninit<K> _keys[CAPACITY];
mem::Uninit<V> _vals[CAPACITY];
Node* _edge[CAPACITY + 1];

public:
private:
auto search_for_insert(auto& key) -> Tuple<> {

}

auto split() -> Node* {
if (_parent == nullptr) {
_parent = new Node{};
_parent->_edge[0] = this;
}

auto lhs = this;
auto rhs = new Node{};
lhs._len = rhs._len = B - 1;
ptr::move(_keys, rhs._keys);
ptr::move(_vals, rhs._vals);
ptr::move(_edge, rhs._edge);
_parent->insert_fit(_idx + 1, mem::move(*&_keys[B - 1]), mem::move(*&_vals[B - 1]), rhs);

ptr::drop(_keys + B - 1, B);
ptr::drop(_vals + B - 1, B);
}

void insert_fit(usize idx, K key, V val, Node* node = nullptr) {
ptr::move(_keys + idx, _keys + idx + 1, _len - idx);
ptr::move(_vals + idx, _vals + idx + 1, _len - idx);
_keys[idx] = static_cast<K&&>(key);
_vals[idx] = static_cast<V&&>(val);

if (node != nullptr) {
ptr::move(_edge + idx + 1, _edge + idx + 2, _len - idx);
_edge[idx + 1] = node;
}
}
};

} // namespace sfc::collections::btree
2 changes: 1 addition & 1 deletion src/sfc/collections/circbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace sfc::collections {

template <class T>
class CircBuf {
vec::Buf<T> _buf;
vec::Buf<T> _buf = {};
usize _len = 0;
usize _first = 0; // begin of the buffer
usize _last = 0; // end of the buffer(behind the last element)
Expand Down
1 change: 0 additions & 1 deletion src/sfc/collections/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace sfc::collections {

template <class T>
class [[nodiscard]] Queue {
protected:
Vec<T> _vec;
usize _pos = 0;

Expand Down
8 changes: 8 additions & 0 deletions src/sfc/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@
#include "core/slice.h"
#include "core/str.h"
#include "core/variant.h"

namespace sfc {
using str::Str;
using tuple::Tuple;
using slice::Slice;
using option::Option;
using variant::Variant;
} // namespace sfc
21 changes: 9 additions & 12 deletions src/sfc/core/cmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,29 @@ enum Ordering {
};

struct Lt {
template <class T>
[[sfc_inline]] auto operator()(const T& a, const T& b) const -> bool {
[[sfc_inline]] auto operator()(const auto& a, const auto& b) const -> bool {
return a < b;
}
};

struct Gt {
template <class T>
[[sfc_inline]] auto operator()(const T& a, const T& b) const -> bool {
return !(a < b);
[[sfc_inline]] auto operator()(const auto& a, const auto& b) const -> bool {
return a > b;
}
};

template <class T>
[[sfc_inline]] auto cmp(const T& a, const T& b) -> Ordering {
if (a == b) return Ordering::Equal;
[[sfc_inline]] auto cmp(const auto& a, const auto& b) -> Ordering {
if (a == b) {
return Ordering::Equal;
}
return a < b ? Ordering::Less : Ordering::Greater;
}

template <class T>
[[sfc_inline]] constexpr auto min(const T& a, const T& b) -> const T& {
[[sfc_inline]] constexpr auto min(const auto& a, const auto& b) -> auto {
return a < b ? a : b;
}

template <class T>
[[sfc_inline]] constexpr auto max(const T& a, const T& b) -> const T& {
[[sfc_inline]] constexpr auto max(const auto& a, const auto& b) -> auto {
return a < b ? b : a;
}

Expand Down
Loading

0 comments on commit ae933d4

Please sign in to comment.