old version of ropes.nim to prevent merge conflicts

This commit is contained in:
Araq 2013-01-12 17:23:16 +01:00
commit 1115c8bdf8

View file

@ -58,536 +58,266 @@
import import
msgs, strutils, platform, hashes, crc, options msgs, strutils, platform, hashes, crc, options
const
PayloadSize = 64_000 # dummy size for range checking
type type
TFormatStr* = string # later we may change it to CString for better TFormatStr* = string # later we may change it to CString for better
# performance of the code generator (assignments # performance of the code generator (assignments
# copy the format strings # copy the format strings
# though it is not necessary) # though it is not necessary)
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
when true: TRopeSeq* = seq[PRope]
# 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 con*(a, b: PRope): PRope proc ropeLen(a: PRope): int =
proc con*(a: PRope, b: string): PRope if a == nil: result = 0
proc con*(a: string, b: PRope): PRope else: result = a.length
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 = proc newRope(data: string = nil): PRope =
if a == nil: result = 0 new(result)
else: result = a.length if data != nil:
result.length = len(data)
result.data = data
proc newRope(data: string = nil): PRope = proc newMutableRope*(capacity = 30): PRope =
new(result) ## creates a new rope that supports direct modifications of the rope's
if data != nil: ## 'data' and 'length' fields.
result.length = len(data) new(result)
result.data = data result.data = newStringOfCap(capacity)
proc newMutableRope*(capacity = 30): PRope = proc freezeMutableRope*(r: PRope) {.inline.} =
## creates a new rope that supports direct modifications of the rope's r.length = r.data.len
## 'data' and 'length' fields.
new(result)
result.data = newStringOfCap(capacity)
proc freezeMutableRope*(r: PRope) {.inline.} = var
r.length = r.data.len cache: array[0..2048*2 -1, PRope]
var proc RopeInvariant(r: PRope): bool =
cache: array[0..2048*2 -1, PRope] 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 RopeInvariant(r: PRope): bool = proc insertInCache(s: string): PRope =
if r == nil: var h = hash(s) and high(cache)
result = true result = cache[h]
else: if isNil(result) or result.data != s:
result = true # result = newRope(s)
# if r.data <> snil then cache[h] = result
# 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 = proc toRope(s: string): PRope =
var h = hash(s) and high(cache) if s.len == 0:
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 result = nil
var num = 0 else:
while i <= length - 1: result = insertInCache(s)
if frmt[i] == '$': assert(RopeInvariant(result))
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]) = proc RopeSeqInsert(rs: var TRopeSeq, r: PRope, at: Natural) =
app(c, ropef(frmt, args)) 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
const proc newRecRopeToStr(result: var string, resultLen: var int, r: PRope) =
bufSize = 1024 # 1 KB is reasonable 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 auxRopeEqualsFile(r: PRope, bin: var tfile, buf: Pointer): bool = proc ropeToStr(p: PRope): string =
if r.data != nil: if p == nil:
if r.length > bufSize: result = ""
internalError("ropes: token too long") else:
return result = newString(p.length)
var readBytes = readBuffer(bin, buf, r.length) var resultLen = 0
result = readBytes == r.length and newRecRopeToStr(result, resultLen, p)
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 = proc con(a, b: PRope): PRope =
var bin: tfile if a == nil: result = b
result = open(bin, f) elif b == nil: result = a
if not result: else:
return # not equal if file does not exist result = newRope()
var buf = alloc(BufSize) result.length = a.length + b.length
result = auxRopeEqualsFile(r, bin, buf) result.left = a
if result: result.right = b
result = readBuffer(bin, buf, bufSize) == 0 # really at the end of file?
dealloc(buf)
close(bin)
proc crcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 = proc con(a: PRope, b: string): PRope = result = con(a, toRope(b))
if r.data != nil: proc con(a: string, b: PRope): PRope = result = con(toRope(a), b)
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 = proc con(a: varargs[PRope]): PRope =
# XXX profiling shows this is actually expensive for i in countup(0, high(a)): result = con(result, a[i])
var stack: TRopeSeq = @[r]
result = startVal proc toRope(i: BiggestInt): PRope = result = toRope($i)
while len(stack) > 0:
var it = pop(stack) proc app(a: var PRope, b: PRope) = a = con(a, b)
while it.data == nil: proc app(a: var PRope, b: string) = a = con(a, b)
add(stack, it.right) proc prepend(a: var PRope, b: PRope) = a = con(b, a)
it = it.left
assert(it.data != nil) proc writeRope*(f: TFile, c: PRope) =
var i = 0 var stack = @[c]
var L = len(it.data) while len(stack) > 0:
while i < L: var it = pop(stack)
result = updateCrc32(it.data[i], result) 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) 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 crcFromRope(r: PRope): TCrc32 = proc appf(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) =
result = newCrcFromRopeAux(r, initCrc32) app(c, ropef(frmt, args))
proc writeRopeIfNotEqual(r: PRope, filename: string): bool = const
# returns true if overwritten bufSize = 1024 # 1 KB is reasonable
var c: TCrc32
c = crcFromFile(filename)
if c != crcFromRope(r):
writeRope(r, filename)
result = true
else:
result = false
else: proc auxRopeEqualsFile(r: PRope, bin: var tfile, buf: Pointer): bool =
# optimized but broken version: 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)
type proc RopeEqualsFile(r: PRope, f: string): bool =
PRope* = ref TRope var bin: tfile
TRope*{.acyclic.} = object of TObject # the empty rope is represented result = open(bin, f)
# by nil to safe space if not result:
left, right: PRope return # not equal if file does not exist
L: int # < 0 if a leaf var buf = alloc(BufSize)
d: array [0..PayloadSize, char] # != nil if a leaf result = auxRopeEqualsFile(r, bin, buf)
if result:
result = readBuffer(bin, buf, bufSize) == 0 # really at the end of file?
dealloc(buf)
close(bin)
TRopeSeq* = seq[PRope] 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 con*(a, b: PRope): PRope proc newCrcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 =
proc con*(a: PRope, b: string): PRope # XXX profiling shows this is actually expensive
proc con*(a: string, b: PRope): PRope var stack: TRopeSeq = @[r]
proc con*(a: varargs[PRope]): PRope result = startVal
proc app*(a: var PRope, b: PRope) while len(stack) > 0:
proc app*(a: var PRope, b: string) var it = pop(stack)
proc prepend*(a: var PRope, b: PRope) while it.data == nil:
proc toRope*(s: string): PRope add(stack, it.right)
proc toRope*(i: BiggestInt): PRope it = it.left
proc ropeLen*(a: PRope): int assert(it.data != nil)
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 i = 0
var length = len(frmt) var L = len(it.data)
result = nil while i < L:
var num = 0 result = updateCrc32(it.data[i], result)
while i <= length - 1: inc(i)
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]) = proc crcFromRope(r: PRope): TCrc32 =
app(c, ropef(frmt, args)) result = newCrcFromRopeAux(r, initCrc32)
const proc writeRopeIfNotEqual(r: PRope, filename: string): bool =
bufSize = 1024 # 1 KB is reasonable # returns true if overwritten
var c: TCrc32
proc auxRopeEqualsFile(r: PRope, bin: var tfile, buf: Pointer): bool = c = crcFromFile(filename)
if r.L < 0: if c != crcFromRope(r):
if -r.L > bufSize: writeRope(r, filename)
internalError("ropes: token too long") result = true
return else:
var readBytes = readBuffer(bin, buf, -r.L) result = false
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