Skip to content

Commit

Permalink
Ok, remove the custom PriorityQueue all-together. It doesn't give t…
Browse files Browse the repository at this point in the history
…hat much of a performance benefit, and in the few cases where random updates/removes are actually needed it is easy to add an `isObsolete` flag.
  • Loading branch information
renggli committed Dec 28, 2023
1 parent 85a4544 commit 02e8996
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 400 deletions.
2 changes: 0 additions & 2 deletions lib/collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ library collection;
export 'src/collection/bimap.dart'
show BiMap, BiMapOnIterableExtension, BiMapOnMapExtension;
export 'src/collection/bitlist.dart' show BitList, BitListExtension;
export 'src/collection/heap.dart'
show Heap, HeapEntry, KeyValueHeapEntry, ValueHeapEntry;
export 'src/collection/iterable/chunked.dart' show ChunkedIterableExtension;
export 'src/collection/iterable/combinations.dart'
show CombinationsIterableExtension;
Expand Down
209 changes: 0 additions & 209 deletions lib/src/collection/heap.dart

This file was deleted.

30 changes: 12 additions & 18 deletions lib/src/graph/algorithms/a_star_search.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:collection';

import '../../../collection.dart';
import 'package:collection/collection.dart';

import '../../../functional.dart';
import '../path.dart';
import '../strategy.dart';
Expand Down Expand Up @@ -43,7 +44,7 @@ class _AStarSearchIterator<V> implements Iterator<Path<V, num>> {

final AStarSearchIterable<V> iterable;
final Map<V, _State<V>> states;
final queue = Heap<_State<V>>();
final queue = PriorityQueue<_State<V>>();

@override
late Path<V, num> current;
Expand All @@ -52,26 +53,21 @@ class _AStarSearchIterator<V> implements Iterator<Path<V, num>> {
bool moveNext() {
while (queue.isNotEmpty) {
final sourceState = queue.removeFirst();
if (sourceState.isObsolete) continue;
for (final target in iterable.successorsOf(sourceState.vertex)) {
final value = iterable.edgeCost(sourceState.vertex, target);
final total = sourceState.total + value;
final targetState = states[target];
if (targetState == null) {
if (targetState == null || total < targetState.total) {
final state = _State<V>(
vertex: target,
parent: sourceState,
value: value,
total: total,
estimate: total + iterable.costEstimate(target));
targetState?.isObsolete = true;
states[target] = state;
queue.add(state);
} else if (total < targetState.total) {
queue.remove(targetState);
targetState.parent = sourceState;
targetState.value = value;
targetState.total = total;
targetState.estimate = total + iterable.costEstimate(target);
queue.add(targetState);
}
}
if (iterable.targetPredicate(sourceState.vertex)) {
Expand All @@ -92,7 +88,7 @@ class _AStarSearchIterator<V> implements Iterator<Path<V, num>> {
}
}

class _State<V> extends HeapEntry<_State<V>> {
class _State<V> implements Comparable<_State<V>> {
_State({
required this.vertex,
this.parent,
Expand All @@ -102,14 +98,12 @@ class _State<V> extends HeapEntry<_State<V>> {
});

final V vertex;
_State<V>? parent;
num value;
num total;
num estimate;
final _State<V>? parent;
final num estimate;
final num value;
final num total;
bool isObsolete = false;

@override
int compareTo(_State<V> other) => estimate.compareTo(other.estimate);

@override
String toString() => '$vertex|$estimate';
}
24 changes: 11 additions & 13 deletions lib/src/graph/algorithms/dijkstra_search.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:collection';

import 'package:collection/collection.dart';

import '../../../functional.dart';
import '../../collection/heap.dart';
import '../path.dart';
import '../strategy.dart';

Expand Down Expand Up @@ -40,7 +41,7 @@ class _DijkstraSearchIterator<V> implements Iterator<Path<V, num>> {

final DijkstraSearchIterable<V> iterable;
final Map<V, _State<V>> states;
final queue = Heap<_State<V>>();
final queue = PriorityQueue<_State<V>>();

@override
late Path<V, num> current;
Expand All @@ -49,21 +50,17 @@ class _DijkstraSearchIterator<V> implements Iterator<Path<V, num>> {
bool moveNext() {
while (queue.isNotEmpty) {
final sourceState = queue.removeFirst();
if (sourceState.isObsolete) continue;
for (final target in iterable.successorsOf(sourceState.vertex)) {
final value = iterable.edgeCost(sourceState.vertex, target);
final total = sourceState.total + value;
final targetState = states[target];
if (targetState == null) {
if (targetState == null || total < targetState.total) {
final state = _State<V>(
vertex: target, parent: sourceState, value: value, total: total);
targetState?.isObsolete = true;
states[target] = state;
queue.add(state);
} else if (total < targetState.total) {
queue.remove(targetState);
targetState.parent = sourceState;
targetState.value = value;
targetState.total = total;
queue.add(targetState);
}
}
if (iterable.targetPredicate(sourceState.vertex)) {
Expand All @@ -84,7 +81,7 @@ class _DijkstraSearchIterator<V> implements Iterator<Path<V, num>> {
}
}

class _State<V> extends HeapEntry<_State<V>> {
class _State<V> implements Comparable<_State<V>> {
_State({
required this.vertex,
this.parent,
Expand All @@ -93,9 +90,10 @@ class _State<V> extends HeapEntry<_State<V>> {
});

final V vertex;
_State<V>? parent;
num value;
num total;
final _State<V>? parent;
final num value;
final num total;
bool isObsolete = false;

@override
int compareTo(_State<V> other) => total.compareTo(other.total);
Expand Down
Loading

0 comments on commit 02e8996

Please sign in to comment.