summary refs log tree commit diff stats
path: root/compiler/ropes.nim
blob: f27bf70f72830f3e3e18d6e6ca3ae846b7eb961f (plain) (blame)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# Ropes for the C code generator
#
#  Ropes are a data structure that represents a very long string
#  efficiently; especially concatenation is done in O(1) instead of O(N).
#  Ropes make use a lazy evaluation: They are essentially concatenation
#  trees that are only flattened when converting to a native Nimrod
#  string or when written to disk. The empty string is represented by a
#  nil pointer.
#  A little picture makes everything clear:
#
#  "this string" & " is internally " & "represented as"
#
#             con  -- inner nodes do not contain raw data
#            /   \
#           /     \
#          /       \
#        con       "represented as"
#       /   \
#      /     \
#     /       \
#    /         \
#   /           \
#"this string"  " is internally "
#
#  Note that this is the same as:
#  "this string" & (" is internally " & "represented as")
#
#             con
#            /   \
#           /     \
#          /       \
# "this string"    con
#                 /   \
#                /     \
#               /       \
#              /         \
#             /           \
#" is internally "        "represented as"
#
#  The 'con' operator is associative! This does not matter however for
#  the algorithms we use for ropes.
#
#  Note that the left and right pointers are not needed for leaves.
#  Leaves have relatively high memory overhead (~30 bytes on a 32
#  bit machines) and we produce many of them. This is why we cache and
#  share leaves accross different rope trees.
#  To cache them they are inserted in a `cache` array.

import 
  msgs, strutils, platform, hashes, crc, options

const
  PayloadSize = 64_000 # dummy size for range checking

type
  TFormatStr* = string # later we may change it to CString for better
                       # performance of the code generator (assignments 
                       # copy the format strings
                       # though it is not necessary)

when true:
  # working version:
  type
    PRope* = ref TRope
    TRope*{.acyclic.} = object of TObject # the empty rope is represented 
                                          # by nil to safe space
      left*, right*: PRope
      length*: int
      data*: string             # != nil if a leaf
    
    TRopeSeq* = seq[PRope]

  proc con*(a, b: PRope): PRope
  proc con*(a: PRope, b: string): PRope
  proc con*(a: string, b: PRope): PRope
  proc con*(a: varargs[PRope]): PRope
  proc app*(a: var PRope, b: PRope)
  proc app*(a: var PRope, b: string)
  proc prepend*(a: var PRope, b: PRope)
  proc toRope*(s: string): PRope
  proc toRope*(i: BiggestInt): PRope
  proc ropeLen*(a: PRope): int
  proc writeRopeIfNotEqual*(r: PRope, filename: string): bool
  proc ropeToStr*(p: PRope): string
  proc ropef*(frmt: TFormatStr, args: varargs[PRope]): PRope
  proc appf*(c: var PRope, frmt: TFormatStr, args: varargs[PRope])
  proc RopeEqualsFile*(r: PRope, f: string): bool
    # returns true if the rope r is the same as the contents of file f
  proc RopeInvariant*(r: PRope): bool
    # exported for debugging
  # implementation

  proc ropeLen(a: PRope): int = 
    if a == nil: result = 0
    else: result = a.length
    
  proc newRope(data: string = nil): PRope = 
    new(result)
    if data != nil: 
      result.length = len(data)
      result.data = data

  proc newMutableRope*(capacity = 30): PRope =
    ## creates a new rope that supports direct modifications of the rope's
    ## 'data' and 'length' fields.
    new(result)
    result.data = newStringOfCap(capacity)

  proc freezeMutableRope*(r: PRope) {.inline.} =
    r.length = r.data.len

  var 
    cache: array[0..2048*2 -1, PRope]

  proc RopeInvariant(r: PRope): bool = 
    if r == nil: 
      result = true
    else: 
      result = true #
                    #    if r.data <> snil then
                    #      result := true
                    #    else begin
                    #      result := (r.left <> nil) and (r.right <> nil);
                    #      if result then result := ropeInvariant(r.left);
                    #      if result then result := ropeInvariant(r.right);
                    #    end 

  proc insertInCache(s: string): PRope = 
    var h = hash(s) and high(cache)
    result = cache[h]
    if isNil(result) or result.data != s:
      result = newRope(s)
      cache[h] = result
    
  proc toRope(s: string): PRope =
    if s.len == 0:
      result = nil
    else:
      result = insertInCache(s)
    assert(RopeInvariant(result))

  proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) = 
    var length = len(rs)
    if at > length: 
      setlen(rs, at + 1)
    else: 
      setlen(rs, length + 1)    # move old rope elements:
    for i in countdown(length, at + 1): 
      rs[i] = rs[i - 1] # this is correct, I used pen and paper to validate it
    rs[at] = r

  proc newRecRopeToStr(result: var string, resultLen: var int, r: PRope) = 
    var stack = @[r]
    while len(stack) > 0: 
      var it = pop(stack)
      while it.data == nil: 
        add(stack, it.right)
        it = it.left
      assert(it.data != nil)
      CopyMem(addr(result[resultLen]), addr(it.data[0]), it.length)
      Inc(resultLen, it.length)
      assert(resultLen <= len(result))

  proc ropeToStr(p: PRope): string = 
    if p == nil: 
      result = ""
    else: 
      result = newString(p.length)
      var resultLen = 0
      newRecRopeToStr(result, resultLen, p)

  proc con(a, b: PRope): PRope = 
    if a == nil: result = b
    elif b == nil: result = a
    else:
      result = newRope()
      result.length = a.length + b.length
      result.left = a
      result.right = b

  proc con(a: PRope, b: string): PRope = result = con(a, toRope(b))
  proc con(a: string, b: PRope): PRope = result = con(toRope(a), b)

  proc con(a: varargs[PRope]): PRope = 
    for i in countup(0, high(a)): result = con(result, a[i])

  proc toRope(i: BiggestInt): PRope = result = toRope($i)

  proc app(a: var PRope, b: PRope) = a = con(a, b)
  proc app(a: var PRope, b: string) = a = con(a, b)
  proc prepend(a: var PRope, b: PRope) = a = con(b, a)

  proc writeRope*(f: TFile, c: PRope) = 
    var stack = @[c]
    while len(stack) > 0: 
      var it = pop(stack)
      while it.data == nil: 
        add(stack, it.right)
        it = it.left
        assert(it != nil)
      assert(it.data != nil)
      write(f, it.data)

  proc WriteRope*(head: PRope, filename: string, useWarning = false) =
    var f: tfile
    if open(f, filename, fmWrite):
      if head != nil: WriteRope(f, head)
      close(f)
    else:
      rawMessage(if useWarning: warnCannotOpenFile else: errCannotOpenFile,
                 filename)

  proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope = 
    var i = 0
    var length = len(frmt)
    result = nil
    var num = 0
    while i <= length - 1: 
      if frmt[i] == '$': 
        inc(i)                  # skip '$'
        case frmt[i]
        of '$': 
          app(result, "$")
          inc(i)
        of '#': 
          inc(i)
          app(result, args[num])
          inc(num)
        of '0'..'9': 
          var j = 0
          while true: 
            j = (j * 10) + Ord(frmt[i]) - ord('0')
            inc(i)
            if (i > length + 0 - 1) or not (frmt[i] in {'0'..'9'}): break 
          num = j
          if j > high(args) + 1: 
            internalError("ropes: invalid format string $" & $(j))
          else:
            app(result, args[j - 1])
        of 'n':
          if optLineDir notin gOptions: app(result, tnl)
          inc i
        of 'N':
          app(result, tnl)
          inc(i)
        else: InternalError("ropes: invalid format string $" & frmt[i])
      var start = i
      while i < length:
        if frmt[i] != '$': inc(i)
        else: break
      if i - 1 >= start: 
        app(result, substr(frmt, start, i - 1))
    assert(RopeInvariant(result))

  proc appf(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) = 
    app(c, ropef(frmt, args))

  const 
    bufSize = 1024              # 1 KB is reasonable

  proc auxRopeEqualsFile(r: PRope, bin: var tfile, buf: Pointer): bool = 
    if r.data != nil:
      if r.length > bufSize: 
        internalError("ropes: token too long")
        return
      var readBytes = readBuffer(bin, buf, r.length)
      result = readBytes == r.length and
          equalMem(buf, addr(r.data[0]), r.length) # BUGFIX
    else: 
      result = auxRopeEqualsFile(r.left, bin, buf)
      if result: result = auxRopeEqualsFile(r.right, bin, buf)
    
  proc RopeEqualsFile(r: PRope, f: string): bool = 
    var bin: tfile
    result = open(bin, f)
    if not result: 
      return                    # not equal if file does not exist
    var buf = alloc(BufSize)
    result = auxRopeEqualsFile(r, bin, buf)
    if result: 
      result = readBuffer(bin, buf, bufSize) == 0 # really at the end of file?
    dealloc(buf)
    close(bin)

  proc crcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 = 
    if r.data != nil: 
      result = startVal
      for i in countup(0, len(r.data) - 1): 
        result = updateCrc32(r.data[i], result)
    else: 
      result = crcFromRopeAux(r.left, startVal)
      result = crcFromRopeAux(r.right, result)

  proc newCrcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 = 
    # XXX profiling shows this is actually expensive
    var stack: TRopeSeq = @[r]
    result = startVal
    while len(stack) > 0: 
      var it = pop(stack)
      while it.data == nil: 
        add(stack, it.right)
        it = it.left
      assert(it.data != nil)
      var i = 0
      var L = len(it.data)
      while i < L: 
        result = updateCrc32(it.data[i], result)
        inc(i)

  proc crcFromRope(r: PRope): TCrc32 = 
    result = newCrcFromRopeAux(r, initCrc32)

  proc writeRopeIfNotEqual(r: PRope, filename: string): bool = 
    # returns true if overwritten
    var c: TCrc32
    c = crcFromFile(filename)
    if c != crcFromRope(r): 
      writeRope(r, filename)
      result = true
    else: 
      result = false

else:
  # optimized but broken version:

  type
    PRope* = ref TRope
    TRope*{.acyclic.} = object of TObject # the empty rope is represented 
                                          # by nil to safe space
      left, right: PRope
      L: int                            # < 0 if a leaf
      d: array [0..PayloadSize, char]             # != nil if a leaf
    
    TRopeSeq* = seq[PRope]

  proc con*(a, b: PRope): PRope
  proc con*(a: PRope, b: string): PRope
  proc con*(a: string, b: PRope): PRope
  proc con*(a: varargs[PRope]): PRope
  proc app*(a: var PRope, b: PRope)
  proc app*(a: var PRope, b: string)
  proc prepend*(a: var PRope, b: PRope)
  proc toRope*(s: string): PRope
  proc toRope*(i: BiggestInt): PRope
  proc ropeLen*(a: PRope): int
  proc writeRopeIfNotEqual*(r: PRope, filename: string): bool
  proc ropeToStr*(p: PRope): string
  proc ropef*(frmt: TFormatStr, args: varargs[PRope]): PRope
  proc appf*(c: var PRope, frmt: TFormatStr, args: varargs[PRope])
  proc RopeEqualsFile*(r: PRope, f: string): bool
    # returns true if the rope r is the same as the contents of file f
  proc RopeInvariant*(r: PRope): bool
    # exported for debugging
  # implementation

  proc ropeLen(a: PRope): int = 
    if a == nil: result = 0
    else: result = a.L.abs
    
  proc newRope(data: string = nil): PRope =
    if data != nil:
      unsafeNew(result, sizeof(TRope)-PayloadSize+len(data)+1)
      result.L = -len(data)
      # copy including '\0':
      copyMem(addr result.d, cstring(data), len(data))
    else:
      unsafeNew(result, sizeof(TRope)-PayloadSize+1)

  proc eqContent(r: PRope, s: string): bool =
    assert r.L < 0
    if -r.L == s.len:
      result = equalMem(addr(r.d), cstring(s), s.len)

  when false:
    proc newMutableRope*(capacity = 30): PRope =
      ## creates a new rope that supports direct modifications of the rope's
      ## 'data' and 'length' fields.
      new(result)
      result.data = newStringOfCap(capacity)

    proc freezeMutableRope*(r: PRope) {.inline.} =
      r.length = r.data.len

  var 
    cache: array[0..2048*2 -1, PRope]

  proc RopeInvariant(r: PRope): bool = 
    if r == nil: 
      result = true
    else: 
      result = true #
                    #    if r.data <> snil then
                    #      result := true
                    #    else begin
                    #      result := (r.left <> nil) and (r.right <> nil);
                    #      if result then result := ropeInvariant(r.left);
                    #      if result then result := ropeInvariant(r.right);
                    #    end 

  proc insertInCache(s: string): PRope = 
    var h = hash(s) and high(cache)
    result = cache[h]
    if isNil(result) or not eqContent(result, s):
      result = newRope(s)
      cache[h] = result
    
  proc toRope(s: string): PRope =
    if s.len == 0:
      result = nil
    else:
      result = insertInCache(s)
    assert(RopeInvariant(result))

  proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) = 
    var length = len(rs)
    if at > length: 
      setlen(rs, at + 1)
    else: 
      setlen(rs, length + 1)    # move old rope elements:
    for i in countdown(length, at + 1): 
      rs[i] = rs[i - 1] # this is correct, I used pen and paper to validate it
    rs[at] = r

  proc newRecRopeToStr(result: var string, resultLen: var int, r: PRope) = 
    var stack = @[r]
    while len(stack) > 0: 
      var it = pop(stack)
      while it.L >= 0:
        add(stack, it.right)
        it = it.left
      assert(it.L < 0)
      CopyMem(addr(result[resultLen]), addr(it.d[0]), -it.L)
      Inc(resultLen, -it.L)
      assert(resultLen <= len(result))

  proc ropeToStr(p: PRope): string = 
    if p == nil: 
      result = ""
    else:
      result = newString(p.L.abs)
      var resultLen = 0
      newRecRopeToStr(result, resultLen, p)

  proc con(a, b: PRope): PRope = 
    if a == nil: result = b
    elif b == nil: result = a
    else:
      result = newRope()
      result.L = a.L.abs + b.L.abs
      result.left = a
      result.right = b

  proc con(a: PRope, b: string): PRope = result = con(a, toRope(b))
  proc con(a: string, b: PRope): PRope = result = con(toRope(a), b)

  proc con(a: varargs[PRope]): PRope = 
    for i in countup(0, high(a)): result = con(result, a[i])

  proc toRope(i: BiggestInt): PRope = result = toRope($i)

  proc app(a: var PRope, b: PRope) = a = con(a, b)
  proc app(a: var PRope, b: string) = a = con(a, b)
  proc prepend(a: var PRope, b: PRope) = a = con(b, a)

  proc writeRope*(f: TFile, c: PRope) =
    var stack = @[c]
    while len(stack) > 0:
      var it = pop(stack)
      while it.L >= 0:
        add(stack, it.right)
        it = it.left
        assert(it != nil)
      assert(it.L < 0)
      write(f, cstring(it.d), -it.L)

  proc WriteRope*(head: PRope, filename: string, useWarning = false) =
    var f: tfile
    if open(f, filename, fmWrite):
      if head != nil: WriteRope(f, head)
      close(f)
    else:
      rawMessage(if useWarning: warnCannotOpenFile else: errCannotOpenFile,
                 filename)

  proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope = 
    var i = 0
    var length = len(frmt)
    result = nil
    var num = 0
    while i <= length - 1: 
      if frmt[i] == '$': 
        inc(i)                  # skip '$'
        case frmt[i]
        of '$': 
          app(result, "$")
          inc(i)
        of '#': 
          inc(i)
          app(result, args[num])
          inc(num)
        of '0'..'9': 
          var j = 0
          while true: 
            j = (j * 10) + Ord(frmt[i]) - ord('0')
            inc(i)
            if (i > length + 0 - 1) or not (frmt[i] in {'0'..'9'}): break 
          num = j
          if j > high(args) + 1: 
            internalError("ropes: invalid format string $" & $(j))
          else:
            app(result, args[j - 1])
        of 'n':
          if optLineDir notin gOptions: app(result, tnl)
          inc i
        of 'N':
          app(result, tnl)
          inc(i)
        else: InternalError("ropes: invalid format string $" & frmt[i])
      var start = i
      while i < length:
        if frmt[i] != '$': inc(i)
        else: break
      if i - 1 >= start: 
        app(result, substr(frmt, start, i - 1))
    assert(RopeInvariant(result))

  proc appf(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) = 
    app(c, ropef(frmt, args))

  const 
    bufSize = 1024              # 1 KB is reasonable

  proc auxRopeEqualsFile(r: PRope, bin: var tfile, buf: Pointer): bool = 
    if r.L < 0:
      if -r.L > bufSize: 
        internalError("ropes: token too long")
        return
      var readBytes = readBuffer(bin, buf, -r.L)
      result = readBytes == -r.L and
          equalMem(buf, addr(r.d[0]), readBytes)
    else:
      result = auxRopeEqualsFile(r.left, bin, buf)
      if result: result = auxRopeEqualsFile(r.right, bin, buf)
    
  proc RopeEqualsFile(r: PRope, f: string): bool = 
    var bin: tfile
    result = open(bin, f)
    if not result: 
      return                    # not equal if file does not exist
    var buf = alloc(BufSize)
    result = auxRopeEqualsFile(r, bin, buf)
    if result: 
      result = readBuffer(bin, buf, bufSize) == 0 # really at the end of file?
    dealloc(buf)
    close(bin)

  proc newCrcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 = 
    # XXX profiling shows this is actually expensive
    var stack: TRopeSeq = @[r]
    result = startVal
    while len(stack) > 0: 
      var it = pop(stack)
      while it.L >= 0:
        add(stack, it.right)
        it = it.left
      assert(it.L < 0)
      var i = 0
      var L = -it.L
      while i < L:
        result = updateCrc32(it.d[i], result)
        inc(i)

  proc crcFromRope(r: PRope): TCrc32 = 
    result = newCrcFromRopeAux(r, initCrc32)

  proc writeRopeIfNotEqual(r: PRope, filename: string): bool = 
    # returns true if overwritten
    var c: TCrc32
    c = crcFromFile(filename)
    if c != crcFromRope(r): 
      writeRope(r, filename)
      result = true
    else: 
      result = false