summary refs log tree commit diff stats
path: root/compiler/nimsets.nim
diff options
context:
space:
mode:
authorOscar NihlgÄrd <oscarnihlgard@gmail.com>2018-04-10 10:38:16 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-10 10:38:16 +0200
commit427490a845afa13c460d71c506994a24aae900c8 (patch)
treecba7b2fbd228541a6c9aa9653aa9c5d54f3b8e4a /compiler/nimsets.nim
parent992300b30057e2b3b489b90fb0e7011607e3e8cf (diff)
downloadNim-427490a845afa13c460d71c506994a24aae900c8.tar.gz
Fix compile time set cardinality (#7558)
Diffstat (limited to 'compiler/nimsets.nim')
-rw-r--r--compiler/nimsets.nim16
1 files changed, 5 insertions, 11 deletions
diff --git a/compiler/nimsets.nim b/compiler/nimsets.nim
index bda753e85..8ec4d3c0d 100644
--- a/compiler/nimsets.nim
+++ b/compiler/nimsets.nim
@@ -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 =
@@ -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: