OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Basic implementation of a heap, with O(log n) insertion and removal. | |
6 abstract class Heap<T> { | |
ahe
2017/05/01 16:50:34
Should this be in src or somewhere?
Paul Berry
2017/05/01 18:12:13
Good point. Done.
| |
7 final _items = <T>[]; | |
8 | |
9 bool get isEmpty => _items.isEmpty; | |
10 | |
11 bool get isNotEmpty => _items.isNotEmpty; | |
12 | |
13 void add(T item) { | |
14 int index = _items.length; | |
15 _items.length += 1; | |
16 while (index > 0) { | |
17 T parent = _items[_parentIndex(index)]; | |
18 if (sortsBefore(parent, item)) break; | |
19 _items[index] = parent; | |
20 index = _parentIndex(index); | |
21 } | |
22 _items[index] = item; | |
23 } | |
24 | |
25 T remove() { | |
26 T removed = _items[0]; | |
27 T orphan = _items.removeLast(); | |
28 if (_items.isNotEmpty) _reInsert(orphan); | |
29 return removed; | |
30 } | |
31 | |
32 /// Client should use a derived class to specify the sort order. | |
33 bool sortsBefore(T a, T b); | |
34 | |
35 int _firstChildIndex(int index) { | |
36 return (index << 1) + 1; | |
37 } | |
38 | |
39 int _parentIndex(int index) { | |
40 return (index - 1) >> 1; | |
41 } | |
42 | |
43 void _reInsert(T item) { | |
44 int index = 0; | |
45 while (true) { | |
46 int childIndex = _firstChildIndex(index); | |
47 if (childIndex >= _items.length) break; | |
48 T child = _items[childIndex]; | |
49 if (childIndex + 1 < _items.length) { | |
50 T nextChild = _items[childIndex + 1]; | |
51 if (sortsBefore(nextChild, child)) { | |
52 child = nextChild; | |
53 childIndex++; | |
54 } | |
55 } | |
56 if (sortsBefore(item, child)) break; | |
57 _items[index] = _items[childIndex]; | |
58 index = childIndex; | |
59 } | |
60 _items[index] = item; | |
61 } | |
62 } | |
OLD | NEW |