Skip to content

Commit e6d2777

Browse files
chagne the implementation of vtr::vector::pairs()
1 parent 0a05545 commit e6d2777

File tree

2 files changed

+23
-61
lines changed

2 files changed

+23
-61
lines changed

libs/libvtrutil/src/vtr_vector.h

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class vector : private std::vector<V, Allocator> {
5656
class key_iterator;
5757
typedef vtr::Range<key_iterator> key_range;
5858

59-
class pair_iterator;
60-
typedef vtr::Range<pair_iterator> pair_range;
61-
6259
public:
6360
//Pass through std::vector's types
6461
using typename storage::allocator_type;
@@ -157,16 +154,31 @@ class vector : private std::vector<V, Allocator> {
157154
}
158155

159156
/**
160-
* @brief Returns a range containing the key-value pairs.
157+
* @brief Provides an iterable object to enumerate the vector.
161158
*
162-
* This function returns a range object that represents the sequence of key-value
163-
* pairs within the vector. The range can be used to iterate over the pairs using
164-
* standard range-based loops or algorithms.
159+
* This function allows for easy enumeration, yielding a tuple of (index, element)
160+
* pairs in each iteration. It is similar in functionality to Python's `enumerate()`.
165161
*
166-
* @return A `pair_range` object representing the range of key-value pairs.
162+
* @ return An iterable wrapper that can be used in a range-based for loop to obtain
163+
* (index, element) pairs.
167164
*/
168-
pair_range pairs() const {
169-
return vtr::make_range(pair_begin(), pair_end());
165+
auto pairs() const {
166+
struct enumerated_iterator {
167+
key_type i;
168+
vector::const_iterator iter;
169+
170+
bool operator!=(const enumerated_iterator& other) const { return iter != other.iter; }
171+
void operator++() { i = key_type(size_t(i) + 1); iter++; }
172+
std::tuple<key_type, decltype(*iter)&> operator*() { return std::tie(i, *iter); }
173+
};
174+
175+
struct enumerated_wrapper {
176+
const vector& vec;
177+
auto begin() { return enumerated_iterator{ key_type(0), vec.begin() }; }
178+
auto end() { return enumerated_iterator{ key_type(vec.size()), vec.end() }; }
179+
};
180+
181+
return enumerated_wrapper{ *this };
170182
}
171183

172184
public:
@@ -218,59 +230,9 @@ class vector : private std::vector<V, Allocator> {
218230
value_type value_;
219231
};
220232

221-
/**
222-
* @brief A bidirectional iterator for a vtr:vector object.
223-
*
224-
* The `pair_iterator` class provides a way to iterate over key-value pairs
225-
* within a vtr::vector container. It supports bidirectional iteration,
226-
* allowing the user to traverse the container both forwards and backwards.
227-
*/
228-
class pair_iterator {
229-
public:
230-
using iterator_category = std::bidirectional_iterator_tag;
231-
using difference_type = std::ptrdiff_t;
232-
using value_type = std::pair<key_type, V>;
233-
using pointer = value_type*;
234-
using reference = value_type&;
235-
236-
/// @brief constructor
237-
pair_iterator(vector<K, V, Allocator>& vec, key_type init)
238-
: vec_(vec), value_(init, vec[init]) {}
239-
240-
/// @brief ++ operator
241-
pair_iterator& operator++() {
242-
value_ = std::make_pair(key_type(size_t(value_.first) + 1), vec_[key_type(size_t(value_.first) + 1)]);
243-
return *this;
244-
}
245-
/// @brief -- operator
246-
pair_iterator& operator--() {
247-
value_ = std::make_pair(key_type(size_t(value_.first) - 1), vec_[key_type(size_t(value_.first) - 1)]);
248-
return *this;
249-
}
250-
/// @brief dereference operator
251-
reference operator*() { return value_; }
252-
/// @brief -> operator
253-
pointer operator->() { return &value_; }
254-
255-
/// @brief == operator
256-
friend bool operator==(const pair_iterator& lhs, const pair_iterator& rhs) { return lhs.value_.first == rhs.value_.first; }
257-
/// @brief != operator
258-
friend bool operator!=(const pair_iterator& lhs, const pair_iterator& rhs) { return !(lhs == rhs); }
259-
260-
private:
261-
/// @brief Reference to the vector of key-value pairs.
262-
vector<K, V, Allocator>& vec_;
263-
// @brief The current key-value pair being pointed to by the iterator.
264-
value_type value_;
265-
};
266-
267233
private:
268234
key_iterator key_begin() const { return key_iterator(key_type(0)); }
269235
key_iterator key_end() const { return key_iterator(key_type(size())); }
270-
271-
pair_iterator pair_begin() const { return pair_iterator(*const_cast<vector<K, V, Allocator>*>(this), key_type(0)); }
272-
pair_iterator pair_end() const { return pair_iterator(*const_cast<vector<K, V, Allocator>*>(this), key_type(size())); }
273236
};
274-
275237
} // namespace vtr
276238
#endif

vpr/test/test_odd_even_routing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ TEST_CASE("test_route_flow", "[vpr_noc_odd_even_routing]") {
226226
compare_routes(golden_path, found_path, noc_model);
227227
}
228228

229-
SECTION("Test case where multiple traffic flows are router, and routes are checked for turn legality and deadlock freedom.") {
229+
SECTION("Test case where multiple traffic flows are routed, and routes are checked for turn legality and deadlock freedom.") {
230230
std::random_device device;
231231
std::mt19937 rand_num_gen(device());
232232
std::uniform_int_distribution<std::mt19937::result_type> dist(0, 99);

0 commit comments

Comments
 (0)