Archived
1
0
Fork 0

Removed useless ly field

This commit is contained in:
Julien Dessaux 2021-10-12 00:31:17 +02:00
parent a551d5980e
commit e6250b237b
2 changed files with 46 additions and 55 deletions

View file

@ -5,11 +5,11 @@ type
Field* = object Field* = object
x, y: int x, y: int
lx, ly: int lx: int
lines: seq[Line] lines: seq[Line]
func Blank*(f: var Field, x, y: int) = func Blank*(f: var Field, x, y: int) =
if y < f.y or y >= f.y+f.ly: # outside the field if y < f.y or y >= f.y+f.lines.len: # outside the field
return return
var l = addr f.lines[y-f.y] var l = addr f.lines[y-f.y]
if x < l.x or x >= l.x+l.columns.len: # outside the field if x < l.x or x >= l.x+l.columns.len: # outside the field
@ -23,14 +23,12 @@ func Blank*(f: var Field, x, y: int) =
while f.lines[i].columns.len == 0: while f.lines[i].columns.len == 0:
inc i inc i
f.y += i f.y += i
f.lines = f.lines[i..<f.ly] f.lines = f.lines[i..<f.lines.len]
f.ly -= i elif y == f.y+f.lines.len-1: # we need to trim the trailing lines
elif y == f.y+f.ly-1: # we need to trim the trailing lines var i = f.lines.len-2
var i = f.ly-2
while f.lines[i].columns.len == 0: while f.lines[i].columns.len == 0:
dec i dec i
f.ly = i+1 f.lines = f.lines[0..i]
f.lines = f.lines[0..<f.ly]
else: # it was a line in the middle else: # it was a line in the middle
l.columns = @[] l.columns = @[]
elif x == l.x: # we need to remove leading spaces elif x == l.x: # we need to remove leading spaces
@ -47,7 +45,7 @@ func Blank*(f: var Field, x, y: int) =
# we now need to calculate the new field limits # we now need to calculate the new field limits
f.x = f.lines[0].x f.x = f.lines[0].x
var x2 = f.lines[0].columns.len + f.lines[0].x var x2 = f.lines[0].columns.len + f.lines[0].x
for i in 1..<f.ly: for i in 1..<f.lines.len:
if f.lines[i].columns.len == 0: if f.lines[i].columns.len == 0:
continue continue
if f.x > f.lines[i].x: if f.x > f.lines[i].x:
@ -57,14 +55,14 @@ func Blank*(f: var Field, x, y: int) =
f.lx = x2-f.x f.lx = x2-f.x
func Get*(f: Field, x, y: int): int = func Get*(f: Field, x, y: int): int =
if y >= f.y and y < f.y+f.ly: if y >= f.y and y < f.y+f.lines.len:
let l = f.lines[y-f.y] let l = f.lines[y-f.y]
if x >= l.x and x < l.x+l.columns.len: if x >= l.x and x < l.x+l.columns.len:
return l.columns[x-l.x] return l.columns[x-l.x]
return int(' ') return int(' ')
func IsIn*(f: Field, x, y: int): bool = func IsIn*(f: Field, x, y: int): bool =
return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.lines.len
proc Load*(filename: string): ref Field = proc Load*(filename: string): ref Field =
var file: File var file: File
@ -81,14 +79,13 @@ proc Load*(filename: string): ref Field =
while true: while true:
let n = file.readChars(data, 0, 4096) let n = file.readChars(data, 0, 4096)
if n <= 0: if n <= 0:
if f.ly == 0: if f.lines.len == 0:
if l == nil: # we got en empty file! if l == nil: # we got en empty file!
return nil return nil
f.x = l.x f.x = l.x
if l != nil: if l != nil:
if f.lx < l.columns.len+l.x-f.x: if f.lx < l.columns.len+l.x-f.x:
f.lx = l.columns.len+l.x-f.x f.lx = l.columns.len+l.x-f.x
inc f.ly
break break
var i = 0 var i = 0
while i < n: while i < n:
@ -100,9 +97,9 @@ proc Load*(filename: string): ref Field =
lastReadIsCR = false lastReadIsCR = false
continue continue
if data[i] == '\n' or data[i] == '\r': if data[i] == '\n' or data[i] == '\r':
if f.ly == 0: if f.lines.len == 0:
if l == nil: return nil
return nil if f.lines.len == 1:
f.x = l.x f.x = l.x
if l != nil: if l != nil:
if f.x > l.x: if f.x > l.x:
@ -111,7 +108,6 @@ proc Load*(filename: string): ref Field =
f.lx = l.columns.len+l.x-f.x f.lx = l.columns.len+l.x-f.x
else: else:
f.lines.add(Line()) f.lines.add(Line())
inc f.ly
l = nil l = nil
trailingSpaces = 0 trailingSpaces = 0
if data[i] == '\r': if data[i] == '\r':
@ -141,14 +137,13 @@ proc Load*(filename: string): ref Field =
else: else:
l.columns.add(int(data[i])) l.columns.add(int(data[i]))
inc i inc i
f.lines = f.lines[0..<f.ly]
return f return f
func Set*(f: var Field, x, y, v: int) = func Set*(f: var Field, x, y, v: int) =
if v == int(' '): if v == int(' '):
f.Blank(x, y) f.Blank(x, y)
elif y >= f.y: elif y >= f.y:
if y < f.y+f.ly: # the line exists if y < f.y+f.lines.len: # the line exists
var l = addr f.lines[y-f.y] var l = addr f.lines[y-f.y]
if l.columns.len == 0: # An empty line is a special case if l.columns.len == 0: # An empty line is a special case
l.x = x l.x = x
@ -170,8 +165,7 @@ func Set*(f: var Field, x, y, v: int) =
if f.lx < l.columns.len+l.x-f.x: if f.lx < l.columns.len+l.x-f.x:
f.lx = l.columns.len+l.x-f.x f.lx = l.columns.len+l.x-f.x
else: # preprend columns else: # preprend columns
let oldL = l.columns.len var newline = newSeqUninitialized[int](l.columns.len+l.x-x)
var newline = newSeqUninitialized[int](oldL+l.x-x)
newline[0] = v newline[0] = v
for i in 1..<l.x-x: for i in 1..<l.x-x:
newline[i] = int(' ') newline[i] = int(' ')
@ -183,23 +177,20 @@ func Set*(f: var Field, x, y, v: int) =
f.lx = f.lx + f.x - x f.lx = f.lx + f.x - x
f.x = x f.x = x
else: # append lines else: # append lines
f.ly = y-f.y+1 f.lines.setlen(y-f.y+1)
f.lines.setlen(f.ly) f.lines[y-f.y] = Line(x: x, columns: @[v])
f.lines[f.ly-1] = Line(x: x, columns: @[v])
if f.x > x: if f.x > x:
f.lx = f.lx + f.x - x f.lx = f.lx + f.x - x
f.x = x f.x = x
if f.lx < x-f.x+1: if f.lx < x-f.x+1:
f.lx = x-f.x+1 f.lx = x-f.x+1
else: # prepend lines else: # prepend lines
let newLy = f.ly+f.y-y var newlines = newSeq[Line](f.lines.len+f.y-y)
var newlines = newSeq[Line](newLy)
newlines[0] = Line(x: x, columns: @[v]) newlines[0] = Line(x: x, columns: @[v])
for i in f.y-y..<newLy: for i in f.y-y..<newlines.len:
newlines[i] = f.lines[i-f.y+y] newlines[i] = f.lines[i-f.y+y]
f.lines = newlines f.lines = newlines
f.y = y f.y = y
f.ly = newLy
if f.x > x: if f.x > x:
f.lx = f.lx+f.x-x f.lx = f.lx+f.x-x
f.x = x f.x = x
@ -222,4 +213,4 @@ func Step*(f: Field, v: tuple[x, y: int], d: tuple[x, y: int]): (int, int) =
x = x2; y = y2 x = x2; y = y2
func GetSize*(f: Field): (int, int, int, int) = func GetSize*(f: Field): (int, int, int, int) =
return (f.x, f.y, f.lx, f.ly) return (f.x, f.y, f.lx, f.lines.len)

View file

@ -3,18 +3,18 @@ import unittest
include ../src/field include ../src/field
func `==`(a, b: Line): bool = a.x == b.x and a.columns == b.columns func `==`(a, b: Line): bool = a.x == b.x and a.columns == b.columns
func `==`(a, b: Field): bool = a.x == b.x and a.lx == b.lx and a.y == b.y and a.ly == b.ly and a.lines == b.lines func `==`(a, b: Field): bool = a.x == b.x and a.lx == b.lx and a.y == b.y and a.lines == b.lines
func cols(a: openarray[char]): seq[int] = func cols(a: openarray[char]): seq[int] =
result.setlen(a.len) result.setlen(a.len)
for i in 0..<a.len: for i in 0..<a.len:
result[i] = a[i].int() result[i] = a[i].int()
const minimal = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, columns: @[int('@')])]) const minimal = Field(x: 0, y: 0, lx: 1, lines: @[Line(x: 0, columns: @[int('@')])])
suite "Field": suite "Field":
test "Blank": test "Blank":
var f = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ var f = Field(x: -7, y: -5, lx: 17, lines: @[
Line(x: -5, columns: @[int('x')]), Line(x: -5, columns: @[int('x')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
@ -26,7 +26,7 @@ suite "Field":
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: -7, columns: @[int('y')]), Line(x: -7, columns: @[int('y')]),
]) ])
const moinsz = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ const moinsz = Field(x: -7, y: -5, lx: 17, lines: @[
Line(x: -5, columns: @[int('x')]), Line(x: -5, columns: @[int('x')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
@ -40,7 +40,7 @@ suite "Field":
]) ])
f.Blank(1, 0) f.Blank(1, 0)
check f == moinsz check f == moinsz
const moinsy = Field(x: -5, y: -5, lx: 15, ly: 8, lines: @[ const moinsy = Field(x: -5, y: -5, lx: 15, lines: @[
Line(x: -5, columns: @[int('x')]), Line(x: -5, columns: @[int('x')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
@ -52,7 +52,7 @@ suite "Field":
]) ])
f.Blank(-7, 4) f.Blank(-7, 4)
check f == moinsy check f == moinsy
const moinsx = Field(x: -3, y: -3, lx: 13, ly: 6, lines: @[ const moinsx = Field(x: -3, y: -3, lx: 13, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 9, columns: @[int('e')]), Line(x: 9, columns: @[int('e')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -62,7 +62,7 @@ suite "Field":
]) ])
f.Blank(-5, -5) f.Blank(-5, -5)
check f == moinsx check f == moinsx
const moinsf = Field(x: -2, y: -3, lx: 12, ly: 6, lines: @[ const moinsf = Field(x: -2, y: -3, lx: 12, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 9, columns: @[int('e')]), Line(x: 9, columns: @[int('e')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -72,7 +72,7 @@ suite "Field":
]) ])
f.Blank(-3, 1) f.Blank(-3, 1)
check f == moinsf check f == moinsf
const moinse = Field(x: -2, y: -3, lx: 11, ly: 6, lines: @[ const moinse = Field(x: -2, y: -3, lx: 11, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 9, columns: @[]), Line(x: 9, columns: @[]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -82,20 +82,20 @@ suite "Field":
]) ])
f.Blank(9, -2) f.Blank(9, -2)
check f == moinse check f == moinse
const moinsu = Field(x: -2, y: 0, lx: 8, ly: 3, lines: @[ const moinsu = Field(x: -2, y: 0, lx: 8, lines: @[
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
Line(x: -3, columns: @[]), Line(x: -3, columns: @[]),
Line(x: 5, columns: @[int('d')]), Line(x: 5, columns: @[int('d')]),
]) ])
f.Blank(8, -3) f.Blank(8, -3)
check f == moinsu check f == moinsu
const moinsd = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])]) const moinsd = Field(x: -2, y: 0, lx: 7, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])])
f.Blank(5, 2) f.Blank(5, 2)
check f == moinsd check f == moinsd
const moinsl = Field(x: 0, y: 0, lx: 5, ly: 1, lines: @[Line(x: 0, columns: @[int('@'), 32, 32, 32, int('r')])]) const moinsl = Field(x: 0, y: 0, lx: 5, lines: @[Line(x: 0, columns: @[int('@'), 32, 32, 32, int('r')])])
f.Blank(-2, 0) f.Blank(-2, 0)
check f == moinsl check f == moinsl
const moinsr = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, columns: @[int('@')])]) const moinsr = Field(x: 0, y: 0, lx: 1, lines: @[Line(x: 0, columns: @[int('@')])])
f.Blank(4, 0) f.Blank(4, 0)
check f == moinsr check f == moinsr
test "Get": test "Get":
@ -108,20 +108,20 @@ suite "Field":
check Load("nonexistant") == nil check Load("nonexistant") == nil
check Load("examples/invalid.b98") == nil check Load("examples/invalid.b98") == nil
check Load("examples/empty.b98") == nil check Load("examples/empty.b98") == nil
check Load("examples/minimal.b98")[] == Field(lx: 1, ly: 1, lines: @[Line(columns: @['@'].cols)]) check Load("examples/minimal.b98")[] == Field(lx: 1, lines: @[Line(columns: @['@'].cols)])
let hello = Field(lx: 24, ly: 1, lines: @[Line(columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)]) let hello = Field(lx: 24, lines: @[Line(columns: @['6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'].cols)])
check Load("examples/hello.b98")[] == hello check Load("examples/hello.b98")[] == hello
check Load("examples/rn.b98")[] == hello check Load("examples/rn.b98")[] == hello
check Load("examples/hello2.b98")[] == Field(x: 1, lx: 33, ly: 2, lines: @[ check Load("examples/hello2.b98")[] == Field(x: 1, lx: 33, lines: @[
Line(x: 33, columns: @['v'].cols), Line(x: 33, columns: @['v'].cols),
Line(x: 1, columns: @['@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', Line(x: 1, columns: @['@', ' ', '>', ' ', '#', ';', '>', ':', '#', ',', '_', 'e', '-', 'j', ';', ' ', '"', 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!',
'"', 'd', 'a', '<'].cols) '"', 'd', 'a', '<'].cols)
]) ])
check Load("examples/factorial.b98")[] == Field(x: 0, lx: 15, ly: 2, lines: @[ check Load("examples/factorial.b98")[] == Field(x: 0, lx: 15, lines: @[
Line(x: 0, columns: @['&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'].cols), Line(x: 0, columns: @['&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'].cols),
Line(x: 1, columns: @['^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'].cols) Line(x: 1, columns: @['^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'].cols)
]) ])
check Load("examples/dna.b98")[] == Field(x: 0, lx: 7, ly: 8, lines: @[ check Load("examples/dna.b98")[] == Field(x: 0, lx: 7, lines: @[
Line(x: 0, columns: @['7', '^', 'D', 'N', '>', 'v', 'A'].cols), Line(x: 0, columns: @['7', '^', 'D', 'N', '>', 'v', 'A'].cols),
Line(x: 0, columns: @['v', '_', '#', 'v', '?', ' ', 'v'].cols), Line(x: 0, columns: @['v', '_', '#', 'v', '?', ' ', 'v'].cols),
Line(x: 0, columns: @['7', '^', '<', '"', '"', '"', '"'].cols), Line(x: 0, columns: @['7', '^', '<', '"', '"', '"', '"'].cols),
@ -132,25 +132,25 @@ suite "Field":
Line(x: 0, columns: @['>', ' ', ',', '+', ',', '@', ')'].cols), Line(x: 0, columns: @['>', ' ', ',', '+', ',', '@', ')'].cols),
]) ])
test "Set": test "Set":
var f = Field(x: 0, y: 0, lx: 1, ly: 1, lines: @[Line(x: 0, columns: @['>'].cols)]) var f = Field(x: 0, y: 0, lx: 1, lines: @[Line(x: 0, columns: @['>'].cols)])
f.Set(0, 0, int('@')) f.Set(0, 0, int('@'))
check f == minimal check f == minimal
f.Set(1, 0, int(' ')) f.Set(1, 0, int(' '))
check f == minimal check f == minimal
const xappend = Field(x: 0, y: 0, lx: 5, ly: 1, lines: @[Line(x: 0, columns: @[int('@'), 32, 32, 32, int('r')])]) const xappend = Field(x: 0, y: 0, lx: 5, lines: @[Line(x: 0, columns: @[int('@'), 32, 32, 32, int('r')])])
f.Set(4, 0, int('r')) f.Set(4, 0, int('r'))
check f == xappend check f == xappend
const xprepend = Field(x: -2, y: 0, lx: 7, ly: 1, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])]) const xprepend = Field(x: -2, y: 0, lx: 7, lines: @[Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')])])
f.Set(-2, 0, int('l')) f.Set(-2, 0, int('l'))
check f == xprepend check f == xprepend
const yappend = Field(x: -2, y: 0, lx: 8, ly: 3, lines: @[ const yappend = Field(x: -2, y: 0, lx: 8, lines: @[
Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]), Line(x: -2, columns: @[int('l'), 32, int('@'), 32, 32, 32, int('r')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 5, columns: @[int('d')]), Line(x: 5, columns: @[int('d')]),
]) ])
f.Set(5, 2, int('d')) f.Set(5, 2, int('d'))
check f == yappend check f == yappend
const yprepend = Field(x: -2, y: -3, lx: 11, ly: 6, lines: @[ const yprepend = Field(x: -2, y: -3, lx: 11, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -160,7 +160,7 @@ suite "Field":
]) ])
f.Set(8, -3, int('u')) f.Set(8, -3, int('u'))
check f == yprepend check f == yprepend
const xappendEmptyline = Field(x: -2, y: -3, lx: 12, ly: 6, lines: @[ const xappendEmptyline = Field(x: -2, y: -3, lx: 12, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 9, columns: @[int('e')]), Line(x: 9, columns: @[int('e')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -170,7 +170,7 @@ suite "Field":
]) ])
f.Set(9, -2, int('e')) f.Set(9, -2, int('e'))
check f == xappendEmptyline check f == xappendEmptyline
const xprependEmptyline = Field(x: -3, y: -3, lx: 13, ly: 6, lines: @[ const xprependEmptyline = Field(x: -3, y: -3, lx: 13, lines: @[
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
Line(x: 9, columns: @[int('e')]), Line(x: 9, columns: @[int('e')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
@ -180,7 +180,7 @@ suite "Field":
]) ])
f.Set(-3, 1, int('f')) f.Set(-3, 1, int('f'))
check f == xprependEmptyline check f == xprependEmptyline
const xprependyprepend = Field(x: -5, y: -5, lx: 15, ly: 8, lines: @[ const xprependyprepend = Field(x: -5, y: -5, lx: 15, lines: @[
Line(x: -5, columns: @[int('x')]), Line(x: -5, columns: @[int('x')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),
@ -192,7 +192,7 @@ suite "Field":
]) ])
f.Set(-5, -5, int('x')) f.Set(-5, -5, int('x'))
check f == xprependyprepend check f == xprependyprepend
const xprependyappend = Field(x: -7, y: -5, lx: 17, ly: 10, lines: @[ const xprependyappend = Field(x: -7, y: -5, lx: 17, lines: @[
Line(x: -5, columns: @[int('x')]), Line(x: -5, columns: @[int('x')]),
Line(x: 0, columns: @[]), Line(x: 0, columns: @[]),
Line(x: 8, columns: @[int('u')]), Line(x: 8, columns: @[int('u')]),