setops.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. func incl*[T](x: var set[T], y: T) {.magic: "Incl".} =
  2. ## Includes element `y` in the set `x`.
  3. ##
  4. ## This is the same as `x = x + {y}`, but it might be more efficient.
  5. runnableExamples:
  6. var a = {1, 3, 5}
  7. a.incl(2)
  8. assert a == {1, 2, 3, 5}
  9. a.incl(4)
  10. assert a == {1, 2, 3, 4, 5}
  11. when not defined(nimHasCallsitePragma):
  12. {.pragma: callsite.}
  13. template incl*[T](x: var set[T], y: set[T]) {.callsite.} =
  14. ## Includes the set `y` in the set `x`.
  15. runnableExamples:
  16. var a = {1, 3, 5, 7}
  17. var b = {4, 5, 6}
  18. a.incl(b)
  19. assert a == {1, 3, 4, 5, 6, 7}
  20. x = x + y
  21. func excl*[T](x: var set[T], y: T) {.magic: "Excl".} =
  22. ## Excludes element `y` from the set `x`.
  23. ##
  24. ## This is the same as `x = x - {y}`, but it might be more efficient.
  25. runnableExamples:
  26. var b = {2, 3, 5, 6, 12, 54}
  27. b.excl(5)
  28. assert b == {2, 3, 6, 12, 54}
  29. template excl*[T](x: var set[T], y: set[T]) {.callsite.} =
  30. ## Excludes the set `y` from the set `x`.
  31. runnableExamples:
  32. var a = {1, 3, 5, 7}
  33. var b = {3, 4, 5}
  34. a.excl(b)
  35. assert a == {1, 7}
  36. x = x - y
  37. func card*[T](x: set[T]): int {.magic: "Card".} =
  38. ## Returns the cardinality of the set `x`, i.e. the number of elements
  39. ## in the set.
  40. runnableExamples:
  41. var a = {1, 3, 5, 7}
  42. assert card(a) == 4
  43. var b = {1, 3, 5, 7, 5}
  44. assert card(b) == 4 # repeated 5 doesn't count
  45. func len*[T](x: set[T]): int {.magic: "Card".}
  46. ## An alias for `card(x)`.
  47. func `*`*[T](x, y: set[T]): set[T] {.magic: "MulSet".} =
  48. ## This operator computes the intersection of two sets.
  49. runnableExamples:
  50. assert {1, 2, 3} * {2, 3, 4} == {2, 3}
  51. func `+`*[T](x, y: set[T]): set[T] {.magic: "PlusSet".} =
  52. ## This operator computes the union of two sets.
  53. runnableExamples:
  54. assert {1, 2, 3} + {2, 3, 4} == {1, 2, 3, 4}
  55. func `-`*[T](x, y: set[T]): set[T] {.magic: "MinusSet".} =
  56. ## This operator computes the difference of two sets.
  57. runnableExamples:
  58. assert {1, 2, 3} - {2, 3, 4} == {1}
  59. func contains*[T](x: set[T], y: T): bool {.magic: "InSet".} =
  60. ## One should overload this proc if one wants to overload the `in` operator.
  61. ##
  62. ## The parameters are in reverse order! `a in b` is a template for
  63. ## `contains(b, a)`.
  64. ## This is because the unification algorithm that Nim uses for overload
  65. ## resolution works from left to right.
  66. ## But for the `in` operator that would be the wrong direction for this
  67. ## piece of code:
  68. runnableExamples:
  69. var s: set[range['a'..'z']] = {'a'..'c'}
  70. assert s.contains('c')
  71. assert 'b' in s
  72. assert 'd' notin s
  73. assert set['a'..'z'] is set[range['a'..'z']]
  74. ## If `in` had been declared as `[T](elem: T, s: set[T])` then `T` would
  75. ## have been bound to `char`. But `s` is not compatible to type
  76. ## `set[char]`! The solution is to bind `T` to `range['a'..'z']`. This
  77. ## is achieved by reversing the parameters for `contains`; `in` then
  78. ## passes its arguments in reverse order.