# # # The Nim Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # this unit handles Nim sets; it implements symbolic sets import ast, astalgo, lineinfos, bitsets, types, options proc inSet*(s: PNode, elem: PNode): bool = assert s.kind == nkCurly if s.kind != nkCurly: #internalError(s.info, "inSet") return false for i in 0..= s.len * ElemSize) or not bitSetIn(s, b): break dec(b) let aa = newIntTypeNode(a + first, elemType) aa.info = info if a == b: result.add aa else: n = newNodeI(nkRange, info) n.typ = elemType n.add aa let bb = newIntTypeNode(b + first, elemType) bb.info = info n.add bb result.add n e = b inc(e) template nodeSetOp(a, b: PNode, op: untyped) {.dirty.} = var x = toBitSet(conf, a) let y = toBitSet(conf, b) op(x, y) result = toTreeSet(conf, x, a.typ, a.info) proc unionSets*(conf: ConfigRef; a, b: PNode): PNode = nodeSetOp(a, b, bitSetUnion) proc diffSets*(conf: ConfigRef; a, b: PNode): PNode = nodeSetOp(a, b, bitSetDiff) proc intersectSets*(conf: ConfigRef; a, b: PNode): PNode = nodeSetOp(a, b, bitSetIntersect) proc symdiffSets*(conf: ConfigRef; a, b: PNode): PNode = nodeSetOp(a, b, bitSetSymDiff) proc containsSets*(conf: ConfigRef; a, b: PNode): bool = let x = toBitSet(conf, a) let y = toBitSet(conf, b) result = bitSetContains(x, y) proc equalSets*(conf: ConfigRef; a, b: PNode): bool = let x = toBitSet(conf, a) let y = toBitSet(conf, b) result = bitSetEquals(x, y) proc complement*(conf: ConfigRef; a: PNode): PNode = var x = toBitSet(conf, a) for i in 0..high(x): x[i] = not x[i] result = toTreeSet(conf, x, a.typ, a.info) proc deduplicate*(conf: ConfigRef; a: PNode): PNode = let x = toBitSet(conf, a) result = toTreeSet(conf, x, a.typ, a.info) proc cardSet*(conf: ConfigRef; a: PNode): BiggestInt = let x = toBitSet(conf, a) result = bitSetCard(x) proc setHasRange*(s: PNode): bool = assert s.kind == nkCurly if s.kind != nkCurly: return false for i in 0.. b iff not (a <= b)