added tools and web dirs
This commit is contained in:
parent
300430fbba
commit
66a7e3d37c
489 changed files with 4593 additions and 9878 deletions
84
lib/system/sets.nim
Normal file → Executable file
84
lib/system/sets.nim
Normal file → Executable file
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2006 Andreas Rumpf
|
||||
# (c) Copyright 2009 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
|
|
@ -10,79 +10,19 @@
|
|||
# set handling
|
||||
|
||||
type
|
||||
TMyByte = int8
|
||||
TNimSet = array [0..4*2048-1, TMyByte]
|
||||
TNimSet = array [0..4*2048-1, int8]
|
||||
|
||||
# implementation:
|
||||
proc countBits32(n: int32): int {.compilerproc.} =
|
||||
var v = n
|
||||
v = v -% ((v shr 1'i32) and 0x55555555'i32)
|
||||
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
|
||||
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
|
||||
|
||||
proc countBits(n: int32): int {.exportc: "countBits".}
|
||||
# We use a prototype here, not in "cntbits.nim", because that is included
|
||||
# in math.nim too. So when linking with math.nim it'd give a duplicated
|
||||
# symbol error which we avoid by renaming here.
|
||||
proc countBits64(n: int64): int {.compilerproc.} =
|
||||
result = countBits32(toU32(n and 0xffff'i64)) +
|
||||
countBits32(toU32(n shr 16'i64))
|
||||
|
||||
include "system/cntbits"
|
||||
|
||||
proc unionSets(res: var TNimSet, a, b: TNimSet, len: int) {.
|
||||
compilerproc, inline.} =
|
||||
for i in countup(0, len-1): res[i] = a[i] or b[i]
|
||||
|
||||
proc diffSets(res: var TNimSet, a, b: TNimSet, len: int) {.
|
||||
compilerproc, inline.} =
|
||||
for i in countup(0, len-1): res[i] = a[i] and not b[i]
|
||||
|
||||
proc intersectSets(res: var TNimSet, a, b: TNimSet, len: int) {.
|
||||
compilerproc, inline.} =
|
||||
for i in countup(0, len-1): res[i] = a[i] and b[i]
|
||||
|
||||
proc symdiffSets(res: var TNimSet, a, b: TNimSet, len: int) {.
|
||||
compilerproc, inline.} =
|
||||
for i in countup(0, len-1): res[i] = a[i] xor b[i]
|
||||
|
||||
proc containsSets(a, b: TNimSet, len: int): bool {.compilerproc, inline.} =
|
||||
# s1 <= s2 ?
|
||||
for i in countup(0, len-1):
|
||||
if (a[i] and not b[i]) != 0'i8: return false
|
||||
return true
|
||||
|
||||
proc containsSubsets(a, b: TNimSet, len: int): bool {.compilerproc, inline.} =
|
||||
# s1 < s2 ?
|
||||
result = false # assume they are equal
|
||||
for i in countup(0, len-1):
|
||||
if (a[i]) and not b[i]) != 0'i32: return false
|
||||
if a[i] != b[i]: result = true # they are not equal
|
||||
|
||||
proc equalSets(a, b: TNimSet, len: int): bool {.compilerproc, inline.} =
|
||||
for i in countup(0, len-1):
|
||||
if a[i] != b[i]: return false
|
||||
return true
|
||||
|
||||
proc cardSet(s: TNimSet, len: int): int {.compilerproc, inline.} =
|
||||
proc cardSet(s: TNimSet, len: int): int {.compilerproc.} =
|
||||
result = 0
|
||||
for i in countup(0, len-1):
|
||||
inc(result, countBits(ze(s[i])))
|
||||
|
||||
const
|
||||
WORD_SIZE = sizeof(TMyByte)*8
|
||||
|
||||
proc inSet(s: TNimSet, elem: int): bool {.compilerproc, inline.} =
|
||||
return (s[elem /% WORD_SIZE] and (1 shl (elem %% WORD_SIZE))) != 0
|
||||
|
||||
proc inclSets(s: var TNimSet, e: int) {.compilerproc, inline.} =
|
||||
s[e /% WORD_SIZE] = s[e /% WORD_SIZE] or toU8(1 shl (e %% WORD_SIZE))
|
||||
|
||||
proc inclRange(s: var TNimSet, first, last: int) {.compilerproc.} =
|
||||
# not very fast, but it is seldom used
|
||||
for i in countup(first, last): inclSets(s, i)
|
||||
|
||||
proc smallInclRange(s: var int, first, last: int) {.compilerproc.} =
|
||||
# not very fast, but it is seldom used
|
||||
for i in countup(first, last):
|
||||
s = s or (1 shl (i %% sizeof(int)*8))
|
||||
|
||||
proc exclSets(s: var TNimSet, e: int) {.compilerproc, inline.} =
|
||||
s[e /% WORD_SIZE] = s[e /% WORD_SIZE] and
|
||||
not toU8(1 shl (e %% WORD_SIZE))
|
||||
|
||||
proc smallContainsSubsets(a, b: int): bool {.compilerProc, inline.} =
|
||||
# not used by new code generator
|
||||
return ((a and not b) != 0) and (a != b)
|
||||
inc(result, countBits32(int32(ze(s[i]))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue