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

Implement bitsets #351

Closed
Araq opened this issue Jan 11, 2025 · 0 comments
Closed

Implement bitsets #351

Araq opened this issue Jan 11, 2025 · 0 comments
Assignees
Labels

Comments

@Araq
Copy link
Member

Araq commented Jan 11, 2025

Size computation already available in evalexprs.nim.

One way to implement these rather easily:

func `[]`[T](a: set[T]; i: int): var uint8 {.magic: "AccessSetAsByteArray".}

func `+`*[T](a, b: set[T]): set[T] {.inline, noinit.} =
  for i in 0 ..< sizeof(a): result[i] = a[i] or b[i]

func `*`*[T](a, b: set[T]): set[T] {.inline, noinit.} =
  for i in 0 ..< sizeof(a): result[i] = a[i] and b[i]

func `-`*[T](a, b: set[T]): set[T] {.inline, noinit.} =
  for i in 0 ..< sizeof(a): result[i] = a[i] and not b[i]

func `==`*[T](a, b: set[T]): bool {.inline.} =
  for i in 0 ..< sizeof(a):
    if a[i] != b[i]: return false
  return true

func `<=`*[T](a, b: set[T]): bool {.inline.} =
  for i in 0 ..< sizeof(a):
    if (a[i] and not b[i]) != 0: return false
  return true

func `<`*[T](a, b: set[T]): bool = (a <= b) and not (a == b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants