Skip to content

Commit

Permalink
improve slot assignment, make 'chain' preserve append order
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Aug 31, 2024
1 parent 79fb5fb commit 32e1e34
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 1,091 deletions.
10 changes: 5 additions & 5 deletions include/yorel/yomm2/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ using virtual_ptr_policy = std::conditional_t<
// -----------------------------------------------------------------------------
// Method

template<typename Key, typename Signature, class Policy= YOMM2_DEFAULT_POLICY>
template<typename Key, typename Signature, class Policy = YOMM2_DEFAULT_POLICY>
struct method;

template<typename Key, typename R, class Policy, typename... A>
Expand Down Expand Up @@ -136,7 +136,7 @@ struct method<Key, R(A...), Policy> : detail::method_info {
Policy, declared_argument_types, parameter_types>>;
info.vp_begin = spec_type_ids::begin;
info.vp_end = spec_type_ids::end;
fn.specs.push_front(info);
fn.specs.push_back(info);
}
};

Expand Down Expand Up @@ -430,7 +430,7 @@ method<Key, R(A...), Policy>::method() {
this->not_implemented = (void*)not_implemented_handler;
this->ambiguous = (void*)ambiguous_handler;
this->method_type = Policy::template static_type<method>();
Policy::methods.push_front(*this);
Policy::methods.push_back(*this);
}

template<typename Key, typename R, class Policy, typename... A>
Expand Down Expand Up @@ -691,14 +691,14 @@ auto update() -> detail::compiler<Policy>;

inline error_handler_type set_error_handler(error_handler_type handler) {
auto p = &default_policy::error;
auto prev= default_policy::error;
auto prev = default_policy::error;
default_policy::error = handler;
return prev;
}

inline method_call_error_handler
set_method_call_error_handler(method_call_error_handler handler) {
auto prev= default_policy::call_error;
auto prev = default_policy::call_error;
default_policy::call_error = handler;
return prev;
}
Expand Down
57 changes: 1 addition & 56 deletions include/yorel/yomm2/detail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ struct runtime;

namespace mp11 = boost::mp11;

enum { TRACE_RUNTIME = 1, TRACE_CALLS = 2 };

#if defined(YOMM2_SHARED)
extern yOMM2_API std::ostream* logs;
extern yOMM2_API unsigned trace_flags;
#else
inline std::ostream* logs;
inline unsigned trace_flags;
#endif

template<typename>
struct parameter_type_list;

Expand Down Expand Up @@ -177,51 +167,6 @@ const char* default_method_name() {
#endif
}

// -----------------------------------------------------------------------------
// iterator adapter for passing range from external_vpt to fast_perfect_hash

template<class PairIterator>
class pair_first_iterator {
PairIterator iter;

public:
using iterator_category = typename std::forward_iterator_tag;
using difference_type = typename PairIterator::difference_type;
using value_type = decltype(std::declval<PairIterator>()->first);
using pointer = const value_type*;
using reference = const value_type&;

explicit pair_first_iterator(PairIterator iter) : iter(iter) {
}

reference operator*() const {
return iter->first;
}

pointer operator->() const {
return &iter->first;
}

pair_first_iterator& operator++() {
++iter;
return *this;
}

pair_first_iterator operator++(int) const {
return pair_first_iterator(iter++);
}

friend bool
operator==(const pair_first_iterator& a, const pair_first_iterator& b) {
return a.iter == b.iter;
}

friend bool
operator!=(const pair_first_iterator& a, const pair_first_iterator& b) {
return a.iter != b.iter;
}
};

// -----------------------------------------------------------------------------
// class info

Expand Down Expand Up @@ -258,7 +203,7 @@ struct class_declaration_aux<Policy, detail::types<Class, Bases...>>
this->type = collect_static_type_id<Policy, Class>();
this->first_base = type_id_list<Policy, types<Bases...>>::begin;
this->last_base = type_id_list<Policy, types<Bases...>>::end;
Policy::classes.push_front(*this);
Policy::classes.push_back(*this);
this->is_abstract = std::is_abstract_v<Class>;
this->static_vptr = &Policy::template static_vptr<Class>;
}
Expand Down
103 changes: 51 additions & 52 deletions include/yorel/yomm2/detail/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define YOREL_YOMM2_CHAIN_INCLUDED

#include <algorithm>
#include <cassert>
#include <boost/assert.hpp>

namespace yorel {
namespace yomm2 {
Expand All @@ -13,69 +13,78 @@ class static_chain {
static_chain(static_chain&) = delete;
static_chain() = default;

explicit static_chain(int) : first(nullptr), removed_prev(nullptr) {
}

class static_link {
public:
static_link(const static_link&) = delete;
static_link() = default;
explicit static_link(int) : _next(nullptr) {
}

T* next() {
return _next;
return next_ptr;
}

protected:
friend class static_chain;
T* _next;
T* prev_ptr;
T* next_ptr;
};

struct link : static_link {
link() : static_link(0) {
void push_back(T& node) {
assert(node.prev_ptr == nullptr);
assert(node.next_ptr == nullptr);

if (!first) {
first = &node;
node.prev_ptr = &node;
return;
}
};

void push_front(T& node) {
assert(node._next == nullptr);
node._next = first;
first = &node;
auto last = first->prev_ptr;
last->next_ptr = &node;
node.prev_ptr = last;
first->prev_ptr = &node;
}

void remove(T& node) {
iterator iter;
BOOST_ASSERT(first != nullptr);

if (&node == first) {
first = node._next;
node._next = nullptr;
removed_prev = nullptr;
return;
}
auto prev = node.prev_ptr;
auto next = node.next_ptr;
auto last = first->prev_ptr;

if (removed_prev != nullptr && removed_prev != &node) {
iter =
std::find_if(iterator(removed_prev), end(), [&node](T& other) {
return other._next == &node;
});
if (iter == end()) {
iter = std::find_if(
begin(), iterator(removed_prev),
[&node](T& other) { return other._next == &node; });
node.prev_ptr = nullptr;
node.next_ptr = nullptr;

if (&node == last) {
if (&node == first) {
first = nullptr;
return;
}
} else {
iter = std::find_if(begin(), end(), [&node](T& other) {
return other._next == &node;
});

first->prev_ptr = prev;
prev->next_ptr = nullptr;
return;
}

if (iter == end()) {
assert(false);
abort();
if (&node == first) {
first = next;
first->prev_ptr = last;
return;
}

iter->_next = node._next;
removed_prev = &*iter;
prev->next_ptr = next;
next->prev_ptr = prev;
}

void clear() {
auto next = first;
first = nullptr;

while (next) {
auto cur = next;
next = cur->next_ptr;
cur->prev_ptr = nullptr;
cur->next_ptr = nullptr;
}
}

class iterator {
Expand All @@ -100,7 +109,7 @@ class static_chain {

iterator& operator++() {
assert(ptr);
ptr = ptr->_next;
ptr = ptr->next_ptr;
return *this;
}

Expand Down Expand Up @@ -151,7 +160,7 @@ class static_chain {

const_iterator& operator++() {
assert(ptr);
ptr = ptr->_next;
ptr = ptr->next_ptr;
return *this;
}

Expand Down Expand Up @@ -192,16 +201,6 @@ class static_chain {

protected:
T* first;
T* removed_prev;
};

template<typename T>
class chain : public static_chain<T> {
public:
chain() {
this->first = nullptr;
this->removed_prev = nullptr;
}
};

} // namespace detail
Expand Down
Loading

0 comments on commit 32e1e34

Please sign in to comment.