Use more Natural and Positive numbers in proc parameters
- Didn't go through all modules, only the main ones I thought of - Building the compiler and tests still work
This commit is contained in:
parent
99e0fb90e0
commit
22b4e4c2f2
10 changed files with 1196 additions and 1196 deletions
|
|
@ -24,7 +24,7 @@ proc `*`*(x: int, order: SortOrder): int {.inline.} =
|
|||
var y = order.ord - 1
|
||||
result = (x xor y) - y
|
||||
|
||||
proc reverse*[T](a: var openArray[T], first, last: int) =
|
||||
proc reverse*[T](a: var openArray[T], first, last: Natural) =
|
||||
## reverses the array ``a[first..last]``.
|
||||
var x = first
|
||||
var y = last
|
||||
|
|
@ -37,7 +37,7 @@ proc reverse*[T](a: var openArray[T]) =
|
|||
## reverses the array `a`.
|
||||
reverse(a, 0, a.high)
|
||||
|
||||
proc reversed*[T](a: openArray[T], first, last: int): seq[T] =
|
||||
proc reversed*[T](a: openArray[T], first, last: Natural): seq[T] =
|
||||
## returns the reverse of the array `a[first..last]`.
|
||||
result = newSeq[T](last - first + 1)
|
||||
var x = first
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import
|
|||
type
|
||||
BitScalar = int
|
||||
|
||||
const
|
||||
const
|
||||
InitIntSetSize = 8 # must be a power of two!
|
||||
TrunkShift = 9
|
||||
BitsPerTrunk = 1 shl TrunkShift # needs to be a power of 2 and
|
||||
|
|
@ -31,11 +31,11 @@ const
|
|||
|
||||
type
|
||||
PTrunk = ref TTrunk
|
||||
TTrunk {.final.} = object
|
||||
TTrunk {.final.} = object
|
||||
next: PTrunk # all nodes are connected with this pointer
|
||||
key: int # start address at bit 0
|
||||
bits: array[0..IntsPerTrunk - 1, BitScalar] # a bit vector
|
||||
|
||||
|
||||
TTrunkSeq = seq[PTrunk]
|
||||
IntSet* = object ## an efficient set of 'int' implemented as a sparse bit set
|
||||
counter, max: int
|
||||
|
|
@ -44,42 +44,42 @@ type
|
|||
|
||||
{.deprecated: [TIntSet: IntSet].}
|
||||
|
||||
proc mustRehash(length, counter: int): bool {.inline.} =
|
||||
proc mustRehash(length, counter: int): bool {.inline.} =
|
||||
assert(length > counter)
|
||||
result = (length * 2 < counter * 3) or (length - counter < 4)
|
||||
|
||||
proc nextTry(h, maxHash: THash): THash {.inline.} =
|
||||
result = ((5 * h) + 1) and maxHash
|
||||
proc nextTry(h, maxHash: THash): THash {.inline.} =
|
||||
result = ((5 * h) + 1) and maxHash
|
||||
|
||||
proc intSetGet(t: IntSet, key: int): PTrunk =
|
||||
proc intSetGet(t: IntSet, key: int): PTrunk =
|
||||
var h = key and t.max
|
||||
while t.data[h] != nil:
|
||||
if t.data[h].key == key:
|
||||
while t.data[h] != nil:
|
||||
if t.data[h].key == key:
|
||||
return t.data[h]
|
||||
h = nextTry(h, t.max)
|
||||
result = nil
|
||||
|
||||
proc intSetRawInsert(t: IntSet, data: var TTrunkSeq, desc: PTrunk) =
|
||||
proc intSetRawInsert(t: IntSet, data: var TTrunkSeq, desc: PTrunk) =
|
||||
var h = desc.key and t.max
|
||||
while data[h] != nil:
|
||||
while data[h] != nil:
|
||||
assert(data[h] != desc)
|
||||
h = nextTry(h, t.max)
|
||||
assert(data[h] == nil)
|
||||
data[h] = desc
|
||||
|
||||
proc intSetEnlarge(t: var IntSet) =
|
||||
proc intSetEnlarge(t: var IntSet) =
|
||||
var n: TTrunkSeq
|
||||
var oldMax = t.max
|
||||
t.max = ((t.max + 1) * 2) - 1
|
||||
newSeq(n, t.max + 1)
|
||||
for i in countup(0, oldMax):
|
||||
for i in countup(0, oldMax):
|
||||
if t.data[i] != nil: intSetRawInsert(t, n, t.data[i])
|
||||
swap(t.data, n)
|
||||
|
||||
proc intSetPut(t: var IntSet, key: int): PTrunk =
|
||||
proc intSetPut(t: var IntSet, key: int): PTrunk =
|
||||
var h = key and t.max
|
||||
while t.data[h] != nil:
|
||||
if t.data[h].key == key:
|
||||
while t.data[h] != nil:
|
||||
if t.data[h].key == key:
|
||||
return t.data[h]
|
||||
h = nextTry(h, t.max)
|
||||
if mustRehash(t.max + 1, t.counter): intSetEnlarge(t)
|
||||
|
|
@ -94,43 +94,43 @@ proc intSetPut(t: var IntSet, key: int): PTrunk =
|
|||
t.data[h] = result
|
||||
|
||||
proc contains*(s: IntSet, key: int): bool =
|
||||
## returns true iff `key` is in `s`.
|
||||
## returns true iff `key` is in `s`.
|
||||
var t = intSetGet(s, `shr`(key, TrunkShift))
|
||||
if t != nil:
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
result = (t.bits[`shr`(u, IntShift)] and `shl`(1, u and IntMask)) != 0
|
||||
else:
|
||||
else:
|
||||
result = false
|
||||
|
||||
proc incl*(s: var IntSet, key: int) =
|
||||
|
||||
proc incl*(s: var IntSet, key: int) =
|
||||
## includes an element `key` in `s`.
|
||||
var t = intSetPut(s, `shr`(key, TrunkShift))
|
||||
var u = key and TrunkMask
|
||||
t.bits[`shr`(u, IntShift)] = t.bits[`shr`(u, IntShift)] or
|
||||
`shl`(1, u and IntMask)
|
||||
|
||||
proc excl*(s: var IntSet, key: int) =
|
||||
proc excl*(s: var IntSet, key: int) =
|
||||
## excludes `key` from the set `s`.
|
||||
var t = intSetGet(s, `shr`(key, TrunkShift))
|
||||
if t != nil:
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
t.bits[`shr`(u, IntShift)] = t.bits[`shr`(u, IntShift)] and
|
||||
not `shl`(1, u and IntMask)
|
||||
|
||||
proc containsOrIncl*(s: var IntSet, key: int): bool =
|
||||
proc containsOrIncl*(s: var IntSet, key: int): bool =
|
||||
## returns true if `s` contains `key`, otherwise `key` is included in `s`
|
||||
## and false is returned.
|
||||
var t = intSetGet(s, `shr`(key, TrunkShift))
|
||||
if t != nil:
|
||||
if t != nil:
|
||||
var u = key and TrunkMask
|
||||
result = (t.bits[`shr`(u, IntShift)] and `shl`(1, u and IntMask)) != 0
|
||||
if not result:
|
||||
if not result:
|
||||
t.bits[`shr`(u, IntShift)] = t.bits[`shr`(u, IntShift)] or
|
||||
`shl`(1, u and IntMask)
|
||||
else:
|
||||
else:
|
||||
incl(s, key)
|
||||
result = false
|
||||
|
||||
|
||||
proc initIntSet*: IntSet =
|
||||
## creates a new int set that is empty.
|
||||
newSeq(result.data, InitIntSetSize)
|
||||
|
|
@ -140,14 +140,14 @@ proc initIntSet*: IntSet =
|
|||
|
||||
proc assign*(dest: var IntSet, src: IntSet) =
|
||||
## copies `src` to `dest`. `dest` does not need to be initialized by
|
||||
## `initIntSet`.
|
||||
## `initIntSet`.
|
||||
dest.counter = src.counter
|
||||
dest.max = src.max
|
||||
newSeq(dest.data, src.data.len)
|
||||
|
||||
|
||||
var it = src.head
|
||||
while it != nil:
|
||||
|
||||
|
||||
var h = it.key and dest.max
|
||||
while dest.data[h] != nil: h = nextTry(h, dest.max)
|
||||
assert(dest.data[h] == nil)
|
||||
|
|
@ -168,7 +168,7 @@ iterator items*(s: IntSet): int {.inline.} =
|
|||
while r != nil:
|
||||
var i = 0
|
||||
while i <= high(r.bits):
|
||||
var w = r.bits[i]
|
||||
var w = r.bits[i]
|
||||
# taking a copy of r.bits[i] here is correct, because
|
||||
# modifying operations are not allowed during traversation
|
||||
var j = 0
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
|
|||
assert(length > counter)
|
||||
result = (length * 2 < counter * 3) or (length - counter < 4)
|
||||
|
||||
proc rightSize*(count: int): int {.inline.} =
|
||||
proc rightSize*(count: Natural): int {.inline.} =
|
||||
## Return the value of `initialSize` to support `count` items.
|
||||
##
|
||||
## If more items are expected to be added, simply add that
|
||||
|
|
|
|||
|
|
@ -128,7 +128,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
|
|||
assert(length > counter)
|
||||
result = (length * 2 < counter * 3) or (length - counter < 4)
|
||||
|
||||
proc rightSize*(count: int): int {.inline.} =
|
||||
proc rightSize*(count: Natural): int {.inline.} =
|
||||
## Return the value of `initialSize` to support `count` items.
|
||||
##
|
||||
## If more items are expected to be added, simply add that
|
||||
|
|
|
|||
|
|
@ -386,7 +386,7 @@ proc split*(s: string, sep: string): seq[string] {.noSideEffect,
|
|||
## `split iterator <#split.i,string,string>`_.
|
||||
accumulateResult(split(s, sep))
|
||||
|
||||
proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||
proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
||||
rtl, extern: "nsuToHex".} =
|
||||
## Converts `x` to its hexadecimal representation.
|
||||
##
|
||||
|
|
@ -403,7 +403,7 @@ proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
|||
# handle negative overflow
|
||||
if n == 0 and x < 0: n = -1
|
||||
|
||||
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
||||
proc intToStr*(x: int, minchars: Positive = 1): string {.noSideEffect,
|
||||
rtl, extern: "nsuIntToStr".} =
|
||||
## Converts `x` to its decimal representation.
|
||||
##
|
||||
|
|
@ -499,7 +499,7 @@ proc parseEnum*[T: enum](s: string, default: T): T =
|
|||
return e
|
||||
result = default
|
||||
|
||||
proc repeat*(c: char, count: int): string {.noSideEffect,
|
||||
proc repeat*(c: char, count: Natural): string {.noSideEffect,
|
||||
rtl, extern: "nsuRepeatChar".} =
|
||||
## Returns a string of length `count` consisting only of
|
||||
## the character `c`. You can use this proc to left align strings. Example:
|
||||
|
|
@ -514,7 +514,7 @@ proc repeat*(c: char, count: int): string {.noSideEffect,
|
|||
result = newString(count)
|
||||
for i in 0..count-1: result[i] = c
|
||||
|
||||
proc repeat*(s: string, n: int): string {.noSideEffect,
|
||||
proc repeat*(s: string, n: Natural): string {.noSideEffect,
|
||||
rtl, extern: "nsuRepeatStr".} =
|
||||
## Returns String `s` concatenated `n` times. Example:
|
||||
##
|
||||
|
|
@ -523,7 +523,7 @@ proc repeat*(s: string, n: int): string {.noSideEffect,
|
|||
result = newStringOfCap(n * s.len)
|
||||
for i in 1..n: result.add(s)
|
||||
|
||||
template spaces*(n: int): string = repeat(' ',n)
|
||||
template spaces*(n: Natural): string = repeat(' ',n)
|
||||
## Returns a String with `n` space characters. You can use this proc
|
||||
## to left align strings. Example:
|
||||
##
|
||||
|
|
@ -535,15 +535,15 @@ template spaces*(n: int): string = repeat(' ',n)
|
|||
## echo text1 & spaces(max(0, width - text1.len)) & "|"
|
||||
## echo text2 & spaces(max(0, width - text2.len)) & "|"
|
||||
|
||||
proc repeatChar*(count: int, c: char = ' '): string {.deprecated.} = repeat(c, count)
|
||||
proc repeatChar*(count: Natural, c: char = ' '): string {.deprecated.} = repeat(c, count)
|
||||
## deprecated: use repeat() or spaces()
|
||||
|
||||
proc repeatStr*(count: int, s: string): string {.deprecated.} = repeat(s, count)
|
||||
proc repeatStr*(count: Natural, s: string): string {.deprecated.} = repeat(s, count)
|
||||
## deprecated: use repeat(string, count) or string.repeat(count)
|
||||
|
||||
proc align*(s: string, count: int, padding = ' '): string {.
|
||||
proc align*(s: string, count: Natural, padding = ' '): string {.
|
||||
noSideEffect, rtl, extern: "nsuAlignString".} =
|
||||
## Aligns a string `s` with `padding`, so that is of length `count`.
|
||||
## Aligns a string `s` with `padding`, so that it is of length `count`.
|
||||
##
|
||||
## `padding` characters (by default spaces) are added before `s` resulting in
|
||||
## right alignment. If ``s.len >= count``, no spaces are added and `s` is
|
||||
|
|
@ -682,7 +682,7 @@ proc endsWith*(s, suffix: string): bool {.noSideEffect,
|
|||
inc(i)
|
||||
if suffix[i] == '\0': return true
|
||||
|
||||
proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
|
||||
proc continuesWith*(s, substr: string, start: Natural): bool {.noSideEffect,
|
||||
rtl, extern: "nsuContinuesWith".} =
|
||||
## Returns true iff ``s`` continues with ``substr`` at position ``start``.
|
||||
##
|
||||
|
|
@ -693,8 +693,8 @@ proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
|
|||
if s[i+start] != substr[i]: return false
|
||||
inc(i)
|
||||
|
||||
proc addSep*(dest: var string, sep = ", ", startLen = 0) {.noSideEffect,
|
||||
inline.} =
|
||||
proc addSep*(dest: var string, sep = ", ", startLen: Natural = 0)
|
||||
{.noSideEffect, inline.} =
|
||||
## Adds a separator to `dest` only if its length is bigger than `startLen`.
|
||||
##
|
||||
## A shorthand for:
|
||||
|
|
@ -784,7 +784,7 @@ proc findAux(s, sub: string, start: int, a: SkipTable): int =
|
|||
inc(j, a[s[j+m]])
|
||||
return -1
|
||||
|
||||
proc find*(s, sub: string, start: int = 0): int {.noSideEffect,
|
||||
proc find*(s, sub: string, start: Natural = 0): int {.noSideEffect,
|
||||
rtl, extern: "nsuFindStr".} =
|
||||
## Searches for `sub` in `s` starting at position `start`.
|
||||
##
|
||||
|
|
@ -793,7 +793,7 @@ proc find*(s, sub: string, start: int = 0): int {.noSideEffect,
|
|||
preprocessSub(sub, a)
|
||||
result = findAux(s, sub, start, a)
|
||||
|
||||
proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect,
|
||||
proc find*(s: string, sub: char, start: Natural = 0): int {.noSideEffect,
|
||||
rtl, extern: "nsuFindChar".} =
|
||||
## Searches for `sub` in `s` starting at position `start`.
|
||||
##
|
||||
|
|
@ -802,7 +802,7 @@ proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect,
|
|||
if sub == s[i]: return i
|
||||
return -1
|
||||
|
||||
proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect,
|
||||
proc find*(s: string, chars: set[char], start: Natural = 0): int {.noSideEffect,
|
||||
rtl, extern: "nsuFindCharSet".} =
|
||||
## Searches for `chars` in `s` starting at position `start`.
|
||||
##
|
||||
|
|
@ -933,7 +933,7 @@ proc replaceWord*(s, sub: string, by = ""): string {.noSideEffect,
|
|||
var j = findAux(s, sub, i, a)
|
||||
if j < 0: break
|
||||
# word boundary?
|
||||
if (j == 0 or s[j-1] notin wordChars) and
|
||||
if (j == 0 or s[j-1] notin wordChars) and
|
||||
(j+sub.len >= s.len or s[j+sub.len] notin wordChars):
|
||||
add result, substr(s, i, j - 1)
|
||||
add result, by
|
||||
|
|
@ -976,7 +976,7 @@ proc parseOctInt*(s: string): int {.noSideEffect,
|
|||
of '\0': break
|
||||
else: raise newException(ValueError, "invalid integer: " & s)
|
||||
|
||||
proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||
proc toOct*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
||||
rtl, extern: "nsuToOct".} =
|
||||
## Converts `x` into its octal representation.
|
||||
##
|
||||
|
|
@ -992,7 +992,7 @@ proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
|
|||
shift = shift + 3
|
||||
mask = mask shl 3
|
||||
|
||||
proc toBin*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||
proc toBin*(x: BiggestInt, len: Positive): string {.noSideEffect,
|
||||
rtl, extern: "nsuToBin".} =
|
||||
## Converts `x` into its binary representation.
|
||||
##
|
||||
|
|
@ -1221,7 +1221,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
|||
## of significant digits to be printed.
|
||||
## `precision`'s default value is the maximum number of meaningful digits
|
||||
## after the decimal point for Nim's ``biggestFloat`` type.
|
||||
##
|
||||
##
|
||||
## If ``precision == 0``, it tries to format it nicely.
|
||||
const floatFormatToChar: array[FloatFormatMode, char] = ['g', 'f', 'e']
|
||||
var
|
||||
|
|
@ -1286,7 +1286,7 @@ proc findNormalized(x: string, inArray: openArray[string]): int =
|
|||
return -1
|
||||
|
||||
proc invalidFormatString() {.noinline.} =
|
||||
raise newException(ValueError, "invalid format string")
|
||||
raise newException(ValueError, "invalid format string")
|
||||
|
||||
proc addf*(s: var string, formatstr: string, a: varargs[string, `$`]) {.
|
||||
noSideEffect, rtl, extern: "nsuAddf".} =
|
||||
|
|
@ -1416,7 +1416,7 @@ when isMainModule:
|
|||
|
||||
doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
|
||||
doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
|
||||
|
||||
|
||||
type MyEnum = enum enA, enB, enC, enuD, enE
|
||||
doAssert parseEnum[MyEnum]("enu_D") == enuD
|
||||
|
||||
|
|
|
|||
1930
lib/pure/unicode.nim
1930
lib/pure/unicode.nim
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue