Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compile time set cardinality #7558

Merged
merged 1 commit into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions compiler/bitsets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ proc bitSetExcl*(x: var TBitSet, elem: BiggestInt)
proc bitSetIn*(x: TBitSet, e: BiggestInt): bool
proc bitSetEquals*(x, y: TBitSet): bool
proc bitSetContains*(x, y: TBitSet): bool
proc bitSetCard*(x: TBitSet): BiggestInt
# implementation

proc bitSetIn(x: TBitSet, e: BiggestInt): bool =
Expand Down Expand Up @@ -69,3 +70,27 @@ proc bitSetContains(x, y: TBitSet): bool =
if (x[i] and not y[i]) != int8(0):
return false
result = true

# Number of set bits for all values of int8
const populationCount: array[low(int8)..high(int8), int8] = [
1.int8, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GULPF

  • how was this generated? (i'm assuming not by hand...)
  • could this be generated via CTFE?
  • if not generated by CTFE, at least needs a test that tries all int8 values

Copy link
Member Author

@GULPF GULPF Apr 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generated it with some quick & dirty code that I didn't want to keep around forever. I agree that it would be better to write some proper code and generate it at compile time, since it's now basically impossible to verify it by just reading it. However, it doesn't really make much of a difference. This table only needs to be verified once, because it will never ever change (but as a counter point, I have added hard-to-verify constants that are wrong before).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok how about at least adding the quick and dirty code in a comment in that function? better than nothing IMO

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This table only needs to be verified once, because it will never ever change

Unfortunately it wasn't verified at all afaict. :-) Please use compile-time evaluation for this.


proc bitSetCard(x: TBitSet): BiggestInt =
for it in x:
result.inc populationCount[it]
16 changes: 5 additions & 11 deletions compiler/nimsets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ proc intersectSets*(a, b: PNode): PNode
proc symdiffSets*(a, b: PNode): PNode
proc containsSets*(a, b: PNode): bool
proc equalSets*(a, b: PNode): bool
proc cardSet*(s: PNode): BiggestInt
proc cardSet*(a: PNode): BiggestInt
# implementation

proc inSet(s: PNode, elem: PNode): bool =
Expand Down Expand Up @@ -156,16 +156,10 @@ proc deduplicate*(a: PNode): PNode =
toBitSet(a, x)
result = toTreeSet(x, a.typ, a.info)

proc cardSet(s: PNode): BiggestInt =
# here we can do better than converting it into a compact set
# we just count the elements directly
result = 0
for i in countup(0, sonsLen(s) - 1):
if s.sons[i].kind == nkRange:
result = result + getOrdValue(s.sons[i].sons[1]) -
getOrdValue(s.sons[i].sons[0]) + 1
else:
inc(result)
proc cardSet(a: PNode): BiggestInt =
var x: TBitSet
toBitSet(a, x)
result = bitSetCard(x)

proc setHasRange(s: PNode): bool =
if s.kind != nkCurly:
Expand Down
4 changes: 4 additions & 0 deletions tests/sets/tsets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,7 @@ var
#import compiler.msgs

echo warnUninit in gNotes

# 7555
doAssert {-1.int8, -2, -2}.card == 2
doAssert {1, 2, 2, 3..5, 4..6}.card == 6