3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-09-10 03:31:26 +00:00

hashlib: add insertion order const iterator

This commit is contained in:
Emil J. Tywoniak 2025-09-02 18:21:30 +02:00
parent 60bcb93e8b
commit 7ecf0747b3
2 changed files with 25 additions and 4 deletions

View file

@ -558,13 +558,16 @@ public:
int index; int index;
const_iterator(const dict *ptr, int index) : ptr(ptr), index(index) { } const_iterator(const dict *ptr, int index) : ptr(ptr), index(index) { }
public: public:
typedef std::forward_iterator_tag iterator_category; typedef std::bidirectional_iterator_tag iterator_category;
typedef std::pair<K, T> value_type; typedef std::pair<K, T> value_type;
typedef ptrdiff_t difference_type; typedef ptrdiff_t difference_type;
typedef std::pair<K, T>* pointer; typedef const std::pair<K, T>* pointer;
typedef std::pair<K, T>& reference; typedef const std::pair<K, T>& reference;
const_iterator() { } const_iterator() { }
const_iterator operator++() { index--; return *this; } const_iterator operator++() { index--; return *this; }
const_iterator operator++(int) { const_iterator tmp = *this; index--; return tmp; }
const_iterator operator--() { index++; return *this; }
const_iterator operator--(int) { const_iterator tmp = *this; index++; return tmp; }
const_iterator operator+=(int amt) { index -= amt; return *this; } const_iterator operator+=(int amt) { index -= amt; return *this; }
bool operator<(const const_iterator &other) const { return index > other.index; } bool operator<(const const_iterator &other) const { return index > other.index; }
bool operator==(const const_iterator &other) const { return index == other.index; } bool operator==(const const_iterator &other) const { return index == other.index; }
@ -598,6 +601,13 @@ public:
const std::pair<K, T> *operator->() const { return &ptr->entries[index].udata; } const std::pair<K, T> *operator->() const { return &ptr->entries[index].udata; }
operator const_iterator() const { return const_iterator(ptr, index); } operator const_iterator() const { return const_iterator(ptr, index); }
}; };
using reverse_iterator = std::reverse_iterator<const_iterator>;
reverse_iterator rbegin() const {
return std::make_reverse_iterator(end());
}
reverse_iterator rend() const {
return std::make_reverse_iterator(begin());
}
constexpr dict() constexpr dict()
{ {
@ -847,7 +857,7 @@ public:
const_iterator begin() const { return const_iterator(this, int(entries.size())-1); } const_iterator begin() const { return const_iterator(this, int(entries.size())-1); }
const_iterator element(int n) const { return const_iterator(this, int(entries.size())-1-n); } const_iterator element(int n) const { return const_iterator(this, int(entries.size())-1-n); }
const_iterator end() const { return const_iterator(nullptr, -1); } const_iterator end() const { return const_iterator(this, -1); }
}; };
template<typename K, typename OPS> template<typename K, typename OPS>

View file

@ -21,6 +21,7 @@
// do not depend on any other components of yosys (except stuff like log_*). // do not depend on any other components of yosys (except stuff like log_*).
#include "kernel/yosys.h" #include "kernel/yosys.h"
#include <iterator>
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
@ -276,6 +277,16 @@ inline int ceil_log2(int x)
#endif #endif
} }
template <typename T>
auto reversed(const T& container) {
struct reverse_view {
const T& cont;
auto begin() const { return cont.rbegin(); }
auto end() const { return cont.rend(); }
};
return reverse_view{container};
}
YOSYS_NAMESPACE_END YOSYS_NAMESPACE_END
#endif #endif