min_max_heap 2.1.0
min_max_heap: ^2.1.0 copied to clipboard
A Min-max binary heap data structure implementation. AKA double ended priority queue. Supports generics.
example/min_max_heap_example.dart
import 'package:logging/logging.dart';
import 'package:min_max_heap/min_max_heap.dart';
import 'data.dart';
final log = Logger('Example');
void main(List<String> args) {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((event) {
print('${event.level.name}: ${event.time.toLocal()}: ${event.message}');
});
final MinMaxHeap<Task> taskManager = MinMaxHeap.fromIterable(
iterable: tasks,
criteria: (Task task) => task.timeToBeDone.inMilliseconds,
);
log.info(taskManager.min);
log.info(taskManager.max);
taskManager.addAll(<Task>[
Task(
name: 'log',
timeToBeDone: const Duration(milliseconds: 50),
),
Task(
name: 'heavy_operation',
timeToBeDone: const Duration(milliseconds: 1000),
),
]);
log.info(taskManager.min);
log.info(taskManager.max);
log.fine(taskManager.asMapOfLevels);
//{
// 0: [Task(name: log, timeToBeDone: 50)],
// 1: [
// Task(name: heavy_operation, timeToBeDone: 1000),
// Task(name: sort_array, timeToBeDone: 300)
// ],
// 2: [
// Task(name: test_data, timeToBeDone: 250),
// Task(name: request_to_server, timeToBeDone: 500)
// ]
// }
log.info(taskManager.sorted());
taskManager.replaceWhere(
(element) => element.timeToBeDone.inMilliseconds < 200,
replacer: (element) => Task(
name: 'Replaced',
timeToBeDone: element.timeToBeDone,
),
);
log.info(taskManager.min);
}