Add more iterator utilities
This patch adds some more iterator helper classes. They really fall
into two groups, but there didn't seem much value in separating them:
- A later patch has a class hierarchy of the form:
Base
+- Derived1
+- Derived2
A class wants to store an array A1 of Derived1 pointers and an
array A2 of Derived2 pointers. However, for compactness reasons,
it was convenient to have a single array of Base pointers,
with A1 and A2 being slices of this array. This reduces the
overhead from two pointers and two ints (3 LP64 words) to one
pointer and two ints (2 LP64 words).
But consumers of the class shouldn't be aware of this: they should
see A1 as containing Derived1 pointers rather than Base pointers
and A2 as containing Derived2 pointers rather than Base pointers.
This patch adds derived_iterator and const_derived_container
classes to support this use case.
- A later patch also adds various linked lists. This patch adds
wrapper_iterator and list_iterator classes to make it easier
to create iterators for these linked lists. For example:
// Iterators for lists of definitions.
using def_iterator = list_iterator<def_info, &def_info::next_def>;
using reverse_def_iterator
= list_iterator<def_info, &def_info::prev_def>;
This in turn makes it possible to use range-based for loops
on the lists.
The patch just adds the things that the later patches need; it doesn't
try to make the classes as functionally complete as possible. I think
we should add extra functionality when needed rather than ahead of time.
gcc/
* iterator-utils.h (derived_iterator): New class.
(const_derived_container, wrapper_iterator): Likewise.
(list_iterator): Likewise.