File tree 1 file changed +28
-6
lines changed
1 file changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -173,14 +173,36 @@ fn append_rope(left: rope, right: rope) -> rope {
173
173
/*
174
174
Function: concat
175
175
176
- Concatenate many ropes
176
+ Concatenate many ropes.
177
+
178
+ If the ropes are balanced initially and have the same height, the resulting
179
+ rope remains balanced. However, this function does not take any further
180
+ measure to ensure that the result is balanced.
177
181
*/
178
182
fn concat ( v : [ rope ] ) -> rope {
179
- let acc = node:: empty;
180
- for r: rope in v {
181
- acc = append_rope ( acc, r) ;
182
- }
183
- ret bal( acc) ;
183
+ //Copy `v` into a mutable vector
184
+ let len = vec:: len ( v) ;
185
+ if len == 0 u { ret node:: empty; }
186
+ let ropes = vec:: init_elt_mut ( v[ 0 ] , len) ;
187
+ uint:: range ( 1 u, len) { |i|
188
+ ropes[ i] = v[ i] ;
189
+ }
190
+
191
+ //Merge progresively
192
+ while len > 1 u {
193
+ uint:: range ( 0 u, len/2 u) { |i|
194
+ ropes[ i] = append_rope ( ropes[ 2 u* i] , ropes[ 2 u* i+1 u] ) ;
195
+ }
196
+ if len%2 u != 0 u {
197
+ ropes[ len/2 u] = ropes[ len - 1 u] ;
198
+ len = len/2 u + 1 u;
199
+ } else {
200
+ len = len/2 u;
201
+ }
202
+ }
203
+
204
+ //Return final rope
205
+ ret ropes[ 0 ] ;
184
206
}
185
207
186
208
You can’t perform that action at this time.
0 commit comments