Source

Quickrope / quickrope-tree.pkg

  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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# quickrope_tree - Tree structure basis for quick ropes

# This file is part of Quickrope: pure-functional sequences with
# O(1) to O(log log n) local operations.

# This is a reference implementation, for actual work you should
# rather use package quickrope (for ropes) or quickrope_v

# Copyright (c) 2011, 2012 Michele Bini

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

## History:

# Sat Oct  1 22:21:40 CEST 2011  - Insert 'map' and 'apply' functions, untested.
# Sun Feb  5 03:23:01 CET 2012   - Version 4 (with >>)
# Sun Feb  5 04:48:45 CET 2012   - Version 4.1 (without boundary constructor BRANCH)

## Code:

package quickrope_tree {

    package data {
	Size = Unt;
	
	Zoom_Tree(X)
	    = NODE3(Zoom_Tree(X), Size, Zoom_Tree(X), Size, Zoom_Tree(X), Size)
	    | NODE2(Zoom_Tree(X), Size, Zoom_Tree(X), Size)
	    | LEAF(X)
	    | EMPTY;
    };
    include data;
    
    Balance = BALANCED | HEAVY_LEFT | HEAVY_RIGHT;

    # Operators make code much more easy to read
    infix val (<=>);      # Sorting keys
    infix val (<==>);     # Node balancedness
    infix val (=<=);      # not-more-than-twice
    infix val (+++);      # concat trees
    infix val (*+++);     # generic prepend
    infix val (+++*);     # generic append

    infix val (>||);       # local prepend
    infix val (||<);       # local append
    infix val (<||);       # local pop left
    infix val (||>);       # local pop right

    (<=>)  = unt::compare;

    (<)    = unt::(<);
    (<=)   = unt::(<=);
    (>)    = unt::(>);
    (>=)   = unt::(>=);

    fun notmorethantwice(x, y) = (x <= y * 0u2);
    (=<=) = notmorethantwice;

    fun balance(x, y) = (x > y)
	?? (notmorethantwice(x, y) ?? BALANCED :: HEAVY_LEFT)
	:: (notmorethantwice(y, x) ?? BALANCED :: HEAVY_RIGHT);

    (<==>) = balance;

    Max_ABC
	= MAX_A | MAX_B | MAX_C;

    Balance3 = BALANCED_3 | HEAVY_A | HEAVY_B | HEAVY_C;
    fun balance3(a, b, c) = case (a <=> c)
	 /* a = c */ EQUAL => case (a <=> b) 
	      /* a = b */ EQUAL    => BALANCED_3;
	      /* a < b */ LESS     => b =<= a ?? BALANCED_3 :: HEAVY_B;
	      /* b < a */ GREATER  => BALANCED_3;
	 esac;
	 /* a < c */ LESS => case (c <=> b)
	      /* c = b */ EQUAL    => BALANCED_3;
	      /* c < b */ LESS     => b =<= c ?? BALANCED_3 :: HEAVY_B;
	      /* b < c */ GREATER  => if (a < b)
		   /* a < b */ c =<= b ?? BALANCED_3 :: HEAVY_C;
	      else
		   /* b <= a */ c =<= a ?? BALANCED_3 :: HEAVY_C;
	      fi;
	 esac;
	 /* c < a */ GREATER => case (a <=> b)
	      /* a = b */ EQUAL    => BALANCED_3;
	      /* a < b */ LESS     => b =<= a ?? BALANCED_3 :: HEAVY_B;
	      /* b < a */ GREATER  => if (c < b)
		   /* c < b */ a =<= b ?? BALANCED_3 :: HEAVY_A;
	      else
		   /* b <= c */ a =<= c ?? BALANCED_3 :: HEAVY_A;
	      fi;
	 esac;
    esac;

    # Returns C if A and C are equal (favors aggregation of a and b in join)
    fun max_abc(a, b, c) = (b > a) ?? ( (b > c) ?? MAX_B :: MAX_C) :: ( (a > c) ?? MAX_A :: MAX_C);

    # Returns A if A and C are equal (favors aggregation of b and c in join)
    fun max_cba(a, b, c) = (b > c) ?? ( (b > a) ?? MAX_B :: MAX_A) :: ( (c > a) ?? MAX_C :: MAX_A);

    # Different configurations give different results in benchmarks

    # (max_abc, max_cba) = (max_cba, max_abc); # Much faster for some datasets (especially large ones), slower for other
    # max_cba = max_abc;

    fun size(EMPTY) 		     => 0u0;
	size(LEAF(_)) 		     => 0u1;
	size(NODE2(_, s, _, s1))         => s + s1;
	size(NODE3(_, s, _, s1, _, s2))  => s + s1 + s2;
    end;

    fun singleton(x) = LEAF(x);

    fun x *+++ m = case (m)
	 EMPTY 					=> LEAF(x);
	 LEAF(p)					=> NODE2(LEAF(x), 0u1, LEAF(p), 0u1);
	 NODE2(a_m, s_a_m, b_m, s_b_m)		=> NODE3(LEAF(x), 0u1, a_m, s_a_m, b_m, s_b_m);
	 NODE3(a_m, s_a_m, b_m, s_b_m, c_m, s_c_m)	=> case (max_cba(s_a_m, s_b_m, s_c_m))
	      MAX_C => NODE3(LEAF(x), 0u1, a_m +++ b_m, s_a_m + s_b_m, c_m, s_c_m);
	      MAX_B => NODE3(LEAF(x) +++ a_m, 0u1 + s_a_m, b_m, s_b_m, c_m, s_c_m);
	      MAX_A => NODE3(LEAF(x), 0u1, a_m, s_a_m, b_m +++ c_m, s_b_m + s_c_m);
	 esac;
    esac
    also
    fun m +++* y = case (m)
	 EMPTY 					=> LEAF(y);
	 LEAF(p)					=> NODE2(LEAF(p), 0u1, LEAF(y), 0u1);
	 NODE2(a_m, s_a_m, b_m, s_b_m)		=> NODE3(a_m, s_a_m, b_m, s_b_m, LEAF(y), 0u1);
	 NODE3(a_m, s_a_m, b_m, s_b_m, c_m, s_c_m)	=> case (max_abc(s_a_m, s_b_m, s_c_m))
	      MAX_A => NODE3(a_m, s_a_m,   b_m +++ c_m, s_b_m + s_c_m,   LEAF(y), 0u1);
	      MAX_B => NODE3(a_m, s_a_m,   b_m, s_b_m,   c_m +++ LEAF(y), s_c_m + 0u1);
	      MAX_C => NODE3(a_m +++ b_m, s_a_m + s_b_m,   c_m, s_c_m,   LEAF(y), 0u1);
	 esac;
    esac
    also
    fun EMPTY +++ y 			=> y;
	x +++ EMPTY 			=> x;
	(x as (LEAF _)) +++ (y as LEAF(_))	=> NODE2(x, 0u1, y, 0u1);
	(LEAF a) +++ y			=> a *+++ y;
	x +++ (LEAF a)			=> x +++* a;
	(x as NODE2(a_x, s_a_x, b_x, s_b_x)) +++ (y as NODE2(a_y, s_a_y, b_y, s_b_y))
	=> {
	    s_x = s_a_x + s_b_x;
	    s_y = s_a_y + s_b_y;
	    case (balance(s_x, s_y))
		 BALANCED	=> NODE2(x, s_x, y, s_y);
		 HEAVY_RIGHT	=> NODE3(x, s_x, a_y, s_a_y, b_y, s_b_y);
		 HEAVY_LEFT	=> NODE3(a_x, s_a_x, b_x, s_b_x, y, s_y);
	    esac;
	};
	(x as NODE2(a_x, s_a_x, b_x, s_b_x)) +++ (y as NODE3(a_y, s_a_y, b_y, s_b_y, c_y, s_c_y))
	=> {
	    s_x = s_a_x + s_b_x;
	    s_y = s_a_y + s_b_y + s_c_y;
	    case (balance(s_x, s_y))
		 BALANCED	=> NODE2(x, s_x, y, s_y);
		 HEAVY_LEFT	=> NODE3(a_x, s_a_x, b_x, s_b_x, y, s_y);
		 HEAVY_RIGHT	=> case (max_cba(s_a_y, s_b_y, s_c_y))
		      MAX_C	=> NODE3(x, s_x, a_y +++ b_y, s_a_y + s_b_y, c_y, s_c_y);
		      MAX_B	=> NODE3(x +++ a_y, s_x + s_a_y, b_y, s_b_y, c_y, s_c_y);
		      MAX_A	=> NODE3(x, s_x, a_y, s_a_y, b_y +++ c_y, s_b_y + s_c_y);
		 esac;
	    esac;
	};
	(x as NODE3(a_x, s_a_x, b_x, s_b_x, c_x, s_c_x)) +++ (y as NODE2(a_y, s_a_y, b_y, s_b_y))
	=> {
	    s_x = s_a_x + s_b_x + s_c_x;
	    s_y = s_a_y + s_b_y;
	    case (balance(s_x, s_y))
		 BALANCED	=> NODE2(x, s_x, y, s_y);
		 HEAVY_RIGHT	=> NODE3(x, s_x, a_y, s_a_y, b_y, s_b_y);
		 HEAVY_LEFT	=> case (max_abc(s_a_x, s_b_x, s_c_x))
		      MAX_A	=> NODE3(a_x, s_a_x, b_x +++ c_x, s_b_x + s_c_x, y, s_y);
		      MAX_B	=> NODE3(a_x, s_a_x, b_x, s_b_x, c_x +++ y, s_c_x + s_y);
		      MAX_C	=> NODE3(a_x +++ b_x, s_a_x + s_b_x, c_x, s_c_x, y, s_y);
		 esac;
	    esac;
	};
	(x as NODE3(a_x, s_a_x, b_x, s_b_x, c_x, s_c_x)) +++ (y as NODE3(a_y, s_a_y, b_y, s_b_y, c_y, s_c_y))
	=> {
	    s_x = s_a_x + s_b_x + s_c_x;
	    s_y = s_a_y + s_b_y + s_c_y;
	    case (balance(s_x, s_y))
		 BALANCED	=> NODE2(x, s_x, y, s_y);
		 HEAVY_RIGHT	=> case (max_cba(s_a_y, s_b_y, s_c_y))
		      MAX_C	=> NODE3(x, s_x, a_y +++ b_y, s_a_y + s_b_y, c_y, s_c_y);
		      MAX_B	=> NODE3(x +++ a_y, s_x + s_a_y, b_y, s_b_y, c_y, s_c_y);
		      MAX_A	=> NODE3(x, s_x, a_y, s_a_y, b_y +++ c_y, s_b_y + s_c_y);
		 esac;
		 HEAVY_LEFT	=> case (max_abc(s_a_x, s_b_x, s_c_x))
		      MAX_A	=> NODE3(a_x, s_a_x, b_x +++ c_x, s_b_x + s_c_x, y, s_y);
		      MAX_B	=> NODE3(a_x, s_a_x, b_x, s_b_x, c_x +++ y, s_c_x + s_y);
		      MAX_C	=> NODE3(a_x +++ b_x, s_a_x + s_b_x, c_x, s_c_x, y, s_y);
		 esac;
	    esac;
	};
    end;
    
    fun split(x, offset) = case (x)
	 EMPTY 			=> (EMPTY, EMPTY);
	 LEAF(_)			=> if (offset < 0u1)
	      (EMPTY, x);
	 else
	      (x, EMPTY);
	 fi;
	 NODE2(a, s_a, b, s_b)	=> case (offset <=> s_a)
	      EQUAL    => (a, b);
	      LESS     => {
		  my (m, n) = split(a, offset);
		  (m, n +++ b);
	      };
	      GREATER  => {
		  offset -= s_a;
		  my (m, n) = split(b, offset);
		  (a +++ m, n);
	      };
	 esac;
	 NODE3(a, s_a, b, s_b, c, s_c)	=> case (offset <=> s_a)
	      EQUAL    => (a, (b +++ c));
	      LESS     => {
		  my (m, n) = split(a, offset);
		  (m, (n +++ (b +++ c)));
	      };
	      GREATER  => {
		  offset -= s_a;
		  case (offset <=> s_b)
		       EQUAL    => (a +++ b, c);
		       LESS     => {
			   my (m, n) = split(b, offset);
			   (a +++ m, n +++ c);
		       };
		       GREATER  => {
			   offset -= s_b;
			   my (m, n) = split(c, offset);
			   (a +++ (b +++ m), n);
		       };
		  esac;
	      };
	 esac;
    esac;

    fun map f = map where
	fun map(EMPTY) 		 		=> EMPTY;
	    map(LEAF(x))			        => LEAF(f(x));
	    map(NODE2(x, s_x, y, s_y))		=> NODE2(map(x), s_x, map(y), s_y);
	    map(NODE3(x, s_x, y, s_y, z, s_z))	=> NODE3(map(x), s_x, map(y), s_y, map(z), s_z);
	end;
    end;

    fun apply f = apply where
	fun apply(EMPTY) 				        => { };
	    apply(LEAF(x))				        => f(x);
	    apply(NODE2(x, s_x, y, s_y))		=> { apply(x); apply(y); };
	    apply(NODE3(x, s_x, y, s_y, z, s_z))	=> { apply(x); apply(y); apply(z); };
	end;
    end;

    fun node2(n as (a, sa, b, sb)) = case (sa <==> sb)
	 BALANCED     => NODE2(n);
	 HEAVY_LEFT   => balance([], 0u0, a, [b], sb);
	 HEAVY_RIGHT  => balance([a], sa, b, [], 0u0);
    esac where
	fun join_q(a, NIL) => a;
	    join_q(a, b!c) => join_q(a +++ b, c);
	end;

	fun join_p(a, NIL) => a;
	    join_p(k, j!i) => join_p(j +++ k, i);
	end;

	fun balance(p, sp, x, q, sq) = case (x)
	     EMPTY => NODE2 n;
	     LEAF(_) => case q
		  NIL => NODE2 n;
		  k!q => NODE2(join_p(x, p), sp, join_q(k, q), sq);
	     esac;
	     NODE2(a, sa, b, sb) => {
		 my sp' = sp + sa; my sq' = sq + sb;
		 case (sp' <==> sq')
		      BALANCED     => NODE2(join_p(a, p), sp', join_q(b, q), sq');
		      HEAVY_LEFT   => balance(p, sp, a, (b!q), sq');
		      HEAVY_RIGHT  => balance((a!p), sp', b, q, sq);
		 esac;
	     };
	     NODE3(a, sa, b, sb, c, sc) => {
		 my sp' = sp + sa; my sq' = sq + sb + sc;
		 case (sp' <==> sq')
		      BALANCED     => NODE2(join_p(a, p), sp', join_q(b, c!q), sq');
		      HEAVY_LEFT   => balance(p, sp, a, (b!c!q), sq');
		      HEAVY_RIGHT  => {
			  my sp'' = sp' + sb; my sq'' = sq' - sb;
			  case (sp' <==> sq')
			       BALANCED     => NODE2(join_p(b, a!p), sp'', join_q(c, q), sq');
			       HEAVY_LEFT   => balance((a!p), sp', b, (c!q), sq'');
			       HEAVY_RIGHT  => balance((b!a!p), sp'', c, q, sq);
			  esac;
		      };
		 esac;
	     };
	esac;
    end;

    fun node3(n as (a, sa, b, sb, c, sc)) = {
	case (balance3(sa, sb, sc))
	     BALANCED_3  => NODE3 n;
	     HEAVY_A     => a +++ (b +++ c);
	     _ 	     => (a +++ b) +++ c;
	esac;
    };

    # Worst case O(log N), amortized O(1)
    fun EMPTY >|| x => x;
	x >|| (y as NODE3(a, sa, b, sb, c, sc)) => {
	    my sx = size(x);
	    my sab = sa + sb;
	    my sy = sc + sab;
	    case (sx <==> sy)
		 BALANCED     => NODE2(x, sx, y, sy);
		 HEAVY_LEFT   => x +++ y;
		 HEAVY_RIGHT  => {
		     if (sa < sx * 0u200)
			  node3(x +++ a, sx + sa, b, sb, c, sc);
		     else
			  node2((x +++ a) >|| b, sx + sab, c, sc);
		     fi;
		 };
	    esac;
	};
	x >|| y => x +++ y;
    end;

    fun x ||< EMPTY => x;
	(y as NODE3(a, sa, b, sb, c, sc)) << x => {
	    my sx = size(x);
	    my sbc = sb + sc;
	    my sy = sa + sbc;
	    case (sy <==> sx)
		 BALANCED     => NODE2(y, sy, x, sx);
		 HEAVY_LEFT   => y +++ x;
		 HEAVY_RIGHT  => {
		     if (sc < sx * 0u200)
			  node3(a, sa, b, sb, c +++ x, sc + sx);
		     else
			  node2(a, sa, b ||< (c +++ x), sbc + sx);
		     fi;
		 };
	    esac;
	};
	x ||< y => x +++ y;
    end;

    package debug {    
	fun paren_print print p = print_b where
	    fun opn() = print "(";
	    fun cls() = print ")";
	    fun sep() = print "|";
	    fun print_b(EMPTY)    => { opn(); cls(); };
		print_b(LEAF(x))  => p(x);
		print_b(NODE2(x, _, y, _)) => {
		    opn(); print_b x; sep(); print_b y; cls();
		};
		print_b(NODE3(x, _, y, _, z, _)) => {
		    opn(); print_b(x); sep(); print_b(y); sep(); print_b(z); cls();
		};
	    end;
	end;

	paren_print_char = paren_print print (fn x = printf "%c" x);
	
	# Probe depth of a particular position in the sequence
	fun depth_of(b, n) = x(b, n, 1) where
	    fun x(EMPTY, _, d)    => d;
		x(LEAF(_), n, d)  => d;
		x(NODE2(a, sa, b, sb), n, d) => case (sa <=> n)
		     EQUAL    => d;
		     LESS     => x(a, n, d + 1);
		     GREATER  => x(b, n - sa, d + 1);
		esac;
		x(NODE3(a, sa, b, sb, c, sc), n, d) => case (sa <=> n)
		     EQUAL    => d;
		     LESS     => x(a, n, d + 1);
		     GREATER  => {
			 n -= sa;
			 case (sb <=> n)
			      EQUAL    => d;
			      LESS     => x(b, n, d + 1);
			      GREATER  => x(c, n - sb, d + 1);
			 esac;
		     };
		esac;
	    end;
	end;
    };

    fun unfold next last_p element seed tree = unfold(seed, tree) where
	fun unfold(seed, tree) = last_p(seed) ?? tree
	    :: unfold(next(seed), singleton(element(seed)) >> tree);
    end;

    fun unfold__join next last_p element seed tree = unfold(seed, tree) where
	fun unfold(seed, tree) = last_p(seed) ?? tree
	    :: unfold(next(seed), singleton(element(seed)) +++ tree);
    end;
    
    (+)    = (+++);
    
    fun from_list x = loop(x, empty) where
	fun loop(NIL, t) => t;
	    loop(x!r, t) => loop(r, t << singleton(x));
	end;
    end;

    fun all_invariants_hold(EMPTY) => TRUE;
	all_invariants_hold(x) => deep(x) where
	    fun deep(EMPTY) => FALSE;
		deep(LEAF(_)) => TRUE;
		deep(NODE2(a, sa, b, sb)) => (sa =<= sb) and (sb =<= sa)
		    and deep(a) and deep(b);
		deep(NODE3(a, sa, b, sb, c, sc)) => case (max_abc(sa, sb, sc))
		     MAX_A => (sa =<= sb) or (sa =<= sc);
		     MAX_B => (sb =<= sc) or (sb =<= sa);
		     MAX_C => (sc =<= sa) or (sc =<= sb);
		esac and deep(a) and deep(b) and deep(c);
	    end;
	end;
    end;    
};