sets.nim 797 B

1234567891011121314151617181920212223242526272829
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # set handling
  10. type
  11. NimSet = array[0..4*2048-1, uint8]
  12. proc countBits32(n: int32): int {.compilerproc.} =
  13. var v = n
  14. v = v -% ((v shr 1'i32) and 0x55555555'i32)
  15. v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
  16. result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
  17. proc countBits64(n: int64): int {.compilerproc.} =
  18. result = countBits32(toU32(n and 0xffffffff'i64)) +
  19. countBits32(toU32(n shr 32'i64))
  20. proc cardSet(s: NimSet, len: int): int {.compilerproc.} =
  21. result = 0
  22. for i in countup(0, len-1):
  23. inc(result, countBits32(int32(s[i])))