sets.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. # bitops can't be imported here, therefore the code duplication.
  13. proc countBits32(n: uint32): int {.compilerproc.} =
  14. # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  15. var v = uint32(n)
  16. v = v - ((v shr 1'u32) and 0x55555555'u32)
  17. v = (v and 0x33333333'u32) + ((v shr 2'u32) and 0x33333333'u32)
  18. result = (((v + (v shr 4'u32) and 0xF0F0F0F'u32) * 0x1010101'u32) shr 24'u32).int
  19. proc countBits64(n: uint64): int {.compilerproc, inline.} =
  20. # generic formula is from: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  21. var v = uint64(n)
  22. v = v - ((v shr 1'u64) and 0x5555555555555555'u64)
  23. v = (v and 0x3333333333333333'u64) + ((v shr 2'u64) and 0x3333333333333333'u64)
  24. v = (v + (v shr 4'u64) and 0x0F0F0F0F0F0F0F0F'u64)
  25. result = ((v * 0x0101010101010101'u64) shr 56'u64).int
  26. proc cardSet(s: NimSet, len: int): int {.compilerproc, inline.} =
  27. var i = 0
  28. result = 0
  29. when defined(x86) or defined(amd64):
  30. while i < len - 8:
  31. inc(result, countBits64((cast[ptr uint64](s[i].unsafeAddr))[]))
  32. inc(i, 8)
  33. while i < len:
  34. inc(result, countBits32(uint32(s[i])))
  35. inc(i, 1)