Skip to content

Commit

Permalink
Provide rudimentary eix::forward_list
Browse files Browse the repository at this point in the history
  • Loading branch information
vaeth committed Apr 8, 2017
1 parent 197d544 commit 134e887
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 18 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*eix-0.32.7
Martin Väth <martin at mvath.de>:
- internal: provide and use eix::array instead of hacks
- internal: provide rudimentary eix::forward_list if necessary
- internal: use std::.. for documented functions from #include <c...>
- internal: remove UNUSED hackery
- contrib/make.sh: Improve clang filtering
Expand Down
1 change: 0 additions & 1 deletion src/cache/eixcache/eixcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "database/io.h"
#include "database/package_reader.h"
#include "eixTk/formated.h"
#include "eixTk/forward_list.h"
#include "eixTk/i18n.h"
#include "eixTk/likely.h"
#include "eixTk/null.h"
Expand Down
167 changes: 163 additions & 4 deletions src/eixTk/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,172 @@
#include <forward_list>
namespace eix {
using std::forward_list;
}
} // namespace eix
#else
#include <list>

// This is not a full implementation of forward_list; only some things we use

#include "eixTk/likely.h"
#include "eixTk/null.h"
namespace eix {
template<typename T> class forward_list : public std::list<T> {
template<class T> class forward_list_node {
public:
T value;
forward_list_node *node;

explicit forward_list_node(const T& val)
: value(val) {
}
};

template<class T> class forward_list_iterator {
public:
forward_list_node<T> *m_node;

typedef forward_list_iterator<T> self;
typedef T value_type;
typedef T& reference;
typedef T *pointer;

forward_list_iterator() {
}

explicit forward_list_iterator(forward_list_node<T> *node)
: m_node(node) {
}

reference operator*() const {
return m_node->value;
}

pointer operator->() const {
return &(m_node->value);
}

self operator++() {
m_node = m_node->node;
return *this;
}

self operator++(int) {
self tmp(*this);
m_node = m_node->node;
return tmp;
}

bool operator==(const self& x) {
return (m_node == x.m_node);
}

bool operator!=(const self& x) {
return (m_node != x.m_node);
}
};

template<class T> class forward_list_const_iterator {
public:
const forward_list_node<T> *m_node;

typedef forward_list_const_iterator<T> self;
typedef T value_type;
typedef const T& const_reference;
typedef const T *const_pointer;

forward_list_const_iterator() {
}

explicit forward_list_const_iterator(forward_list_node<T> *node)
: m_node(node) {
}

const_reference operator*() const {
return m_node->value;
}

const_pointer operator->() const {
return &(m_node->value);
}

self operator++() {
m_node = m_node->node;
return *this;
}

self operator++(int) {
self tmp(*this);
m_node = m_node->node;
return tmp;
}

bool operator==(const self& x) {
return (m_node == x.m_node);
}

bool operator!=(const self& x) {
return (m_node != x.m_node);
}
};

template<class T> class forward_list {
private:
typedef forward_list_node<T> node_type;
typedef forward_list_node<T> *node_ptr;
node_ptr head;

public:
typedef T value_type;
typedef forward_list_iterator<T> iterator;
typedef forward_list_const_iterator<T> const_iterator;

forward_list()
: head(NULLPTR) {
}

~forward_list() {
for(node_ptr curr(head); likely(curr != NULLPTR); ) {
node_ptr next(curr->node);
delete curr;
curr = next;
}
}

iterator begin() {
return iterator(head);
}

const_iterator begin() const {
return const_iterator(head);
}

iterator before_begin() {
return iterator(NULLPTR);
}

const_iterator before_begin() const {
return const_iterator(NULLPTR);
}

iterator end() {
return iterator(NULLPTR);
}

const_iterator end() const {
return const_iterator(NULLPTR);
}

void insert_after(iterator iterator, const value_type& value) {
node_ptr new_node(new node_type(value));
node_ptr parent(iterator.m_node);
if(likely(parent != NULLPTR)) {
new_node->node = parent->node;
parent->node = new_node;
} else {
new_node->node = head;
head = new_node;
}
}
};
}
} // namespace eix
#endif

#endif // SRC_EIXTK_FORWARD_LIST_H_
12 changes: 0 additions & 12 deletions src/portage/mask_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ template<typename m_Type> class Masks : protected eix::forward_list<m_Type> {
typedef typename eix::forward_list<m_Type> MasksList;
using MasksList::begin;
using MasksList::end;
#ifdef HAVE_FORWARD_LIST
using MasksList::before_begin;
#endif
typedef typename MasksList::iterator iterator;
typedef typename MasksList::const_iterator const_iterator;

Masks() : MasksList() {
}

void add(const m_Type& m) {
#ifdef HAVE_FORWARD_LIST
iterator parent(before_begin());
for(iterator it(begin()); it != end(); parent = it++) {
if(m.priority < it->priority) {
Expand All @@ -57,15 +54,6 @@ template<typename m_Type> class Masks : protected eix::forward_list<m_Type> {
}
}
MasksList::insert_after(parent, m);
#else
for(iterator it(begin()); it != end(); ++it) {
if(m.priority < it->priority) {
MasksList::insert(it, m);
return;
}
}
MasksList::push_back(m);
#endif
}
};

Expand Down
1 change: 0 additions & 1 deletion src/portage/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "eixTk/dialect.h"
#include "eixTk/eixarray.h"
#include "eixTk/eixint.h"
#include "eixTk/forward_list.h"
#include "eixTk/inttypes.h"
#include "eixTk/likely.h"
#include "eixTk/null.h"
Expand Down

0 comments on commit 134e887

Please sign in to comment.