-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.rs
182 lines (159 loc) · 4.67 KB
/
merge.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use trie::Cursor;
/// A cursor-like merge of several cursors.
pub struct CursorMerger<'a, C: Cursor<'a>> {
// pairs of data and cursors, ordered by `C::Key`.
pub cursors: Vec<((&'a C::Key, C::Val), C)>,
}
/// A view of merged results, reflecting a key and items with this key.
///
/// The view holds a mut reference for the `CursorMerger`, and must be dropped
/// before the merger may be advanced again. This is because the view uses the
/// storage of the merger to avoid allocating and copying its iterated values.
pub struct CursorView<'a, 'b, C> where 'a: 'b, C: Cursor<'a>+'b {
pos: usize,
len: usize,
merger: &'b mut CursorMerger<'a, C>,
}
impl<'a, 'b, C> CursorView<'a, 'b, C> where 'a: 'b, C: Cursor<'a>+'b {
/// Returns the key being merged, unless all elements have been consumed.
pub fn key(&self) -> Option<&'a C::Key> {
if self.pos < self.len {
Some(&(self.merger.cursors[self.pos].0).0)
}
else {
None
}
}
/// Returns the number of remaining elements in the merge.
pub fn len(&self) -> usize {
self.len - self.pos
}
}
impl<'a, 'b, C> Iterator for CursorView<'a, 'b, C> where 'a: 'b, C: Cursor<'a>+'b {
type Item = C::Val;
fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.len {
if let Some(next) = self.merger.cursors[self.pos].1.next() {
self.pos += 1;
Some(::std::mem::replace(&mut self.merger.cursors[self.pos-1].0, next).1)
}
else {
self.len -= 1;
Some((self.merger.cursors.remove(self.pos).0).1)
}
}
else {
None
}
}
}
impl<'a, 'b, C> Drop for CursorView<'a, 'b, C> where 'a: 'b, C: Cursor<'a>+'b {
fn drop(&mut self) {
while self.pos < self.len {
if let Some(next) = self.merger.cursors[self.pos].1.next() {
self.pos += 1;
self.merger.cursors[self.pos-1].0 = next;
}
else {
self.len -= 1;
self.merger.cursors.remove(self.pos);
}
}
self.merger.re_sort(self.len);
}
}
impl<'a, C: Cursor<'a>> CursorMerger<'a, C> {
/// Creates a new, empty CursorMerger.
pub fn new() -> Self { CursorMerger::<'a, C> { cursors: vec![] } }
/// Returns a view over the data of the next key, if any, and advances the cursor.
pub fn next<'b>(&'b mut self) -> Option<CursorView<'a, 'b, C>> {
if self.cursors.len() > 0 {
let mut count = 1;
while count < self.cursors.len() && &(self.cursors[0].0).0 == &(self.cursors[count].0).0 {
count += 1;
}
Some(CursorView { pos : 0, len: count, merger: self })
}
else {
None
}
}
/// Advances the CursorMerger to the first key at least as large as `key`.
#[inline(never)]
pub fn seek(&mut self, key: &C::Key) {
// call seek until we don't have to
let mut cursor = 0;
while cursor < self.cursors.len() && (self.cursors[cursor].0).0 < key {
// must advance cursor, pop next.
self.cursors[cursor].1.seek(key);
if let Some(next) = self.cursors[cursor].1.next() {
self.cursors[cursor].0 = next;
cursor += 1;
}
else {
self.cursors.remove(cursor);
}
}
self.re_sort(cursor);
debug_assert!(self.cursors.len() == 0 || (self.cursors[0].0).0 >= key);
}
/// Reveals the next key, if one exists.
pub fn peek(&mut self) -> Option<&'a C::Key> {
if self.cursors.len() > 0 {
Some((&self.cursors[0].0).0)
}
else {
None
}
}
/// Clears the CursorMerger.
pub fn clear(&mut self) {
self.cursors.clear();
}
/// Refills a CursorMerger from an iterator of Cursors, re-using allocated memory.
pub fn refill_from<I: Iterator<Item=C>>(&mut self, iterator: I) {
self.cursors.clear();
for mut item in iterator {
if let Some(next) = item.next() {
self.cursors.push((next, item));
}
}
self.cursors.sort_by(|x,y| (x.0).0.cmp(&(y.0).0));
}
/// Constructs a new CursorMerger from a iterator of Cursors.
pub fn from<I: Iterator<Item=C>>(iterator: I) -> Self {
let mut result = Self::new();
result.refill_from(iterator);
result
}
pub fn push(&mut self, mut cursor: C) {
if let Some(next) = cursor.next() {
self.cursors.push((next, cursor));
}
}
// internal method for ensuring order invariant.
fn re_sort(&mut self, until: usize) {
if until > 0 {
// determine how large a prefix to re-sort
let count = {
let mut max_key = &(self.cursors[0].0).0;
for index in 1..until {
if (self.cursors[index].0).0.gt(max_key) {
max_key = &(self.cursors[index].0).0;
}
}
// scan prefix of old keys until we pass it ...
let mut count = until;
while count < self.cursors.len() && (self.cursors[count].0).0.le(max_key) {
count += 1;
}
count
};
// sort that prefix
self.cursors[..count].sort_by(|x,y| (x.0).0.cmp(&(y.0).0));
for index in 1 .. self.cursors.len() {
debug_assert!((self.cursors[index-1].0).0 <= (self.cursors[index].0).0);
}
}
}
}