Files
sdk/pkg/compiler/test/codesize/swarm/BiIterator.dart
T
Mayank Patke c878108cc8 [dart2js] Update pubspec to Dart 3.7 and reformat.
All non-pubspec changes generated by `dart format pkg/compiler`.

Change-Id: Iadaa70974816d96ccb47813fe72d5a2bb83d6bda
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400181
Reviewed-by: Nate Biggs <natebiggs@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
2024-12-13 11:24:50 -08:00

55 lines
1.5 KiB
Dart

part of swarmlib;
/// An iterator that allows the user to move forward and backward though
/// a set of items. (Bi-directional)
class BiIterator<E> {
/// Provides forward and backward iterator functionality to keep track
/// which item is currently selected.
ObservableValue<int> currentIndex;
/// The collection of items we will be iterating through.
List<E> list;
BiIterator(this.list, [List<ChangeListener>? oldListeners])
: currentIndex = ObservableValue<int>(0) {
if (oldListeners != null) {
currentIndex.listeners = oldListeners;
}
}
/// Returns the next section from the sections, given the current
/// position. Returns the last source if there is no next section.
E next() {
if (currentIndex.value < list.length - 1) {
currentIndex.value += 1;
}
return list[currentIndex.value];
}
/// Returns the current Section (page in the UI) that the user is
/// looking at.
E get current {
return list[currentIndex.value];
}
/// Returns the previous section from the sections, given the current
/// position. Returns the front section if we are already at the front of
/// the list.
E previous() {
if (currentIndex.value > 0) {
currentIndex.value -= 1;
}
return list[currentIndex.value];
}
/// Move the iterator pointer over so that it points to a given list item.
void jumpToValue(E val) {
for (int i = 0; i < list.length; i++) {
if (identical(list[i], val)) {
currentIndex.value = i;
break;
}
}
}
}