1. my class Set does Setty {
  2. has $!WHICH;
  3. multi method WHICH (Set:D:) {
  4. nqp::if(
  5. nqp::attrinited(self,Set,'$!WHICH'),
  6. $!WHICH,
  7. $!WHICH := nqp::if(
  8. nqp::istype(self.WHAT,Set),
  9. 'Set|',
  10. nqp::concat(self.^name,'|')
  11. ) ~ %!elems.keys.sort
  12. )
  13. }
  14. method iterator(Set:D:) {
  15. class :: does Rakudo::Iterator::Mappy {
  16. method pull-one() {
  17. nqp::if(
  18. $!iter,
  19. Pair.new(nqp::iterval(nqp::shift($!iter)),True),
  20. IterationEnd
  21. )
  22. }
  23. }.new(%!elems)
  24. }
  25. multi method kv(Set:D:) {
  26. Seq.new(class :: does Rakudo::Iterator::Mappy {
  27. has int $!on-value;
  28. method pull-one() is raw {
  29. nqp::if(
  30. $!on-value,
  31. nqp::stmts(
  32. ($!on-value = 0),
  33. True,
  34. ),
  35. nqp::if(
  36. $!iter,
  37. nqp::stmts(
  38. ($!on-value = 1),
  39. nqp::iterval(nqp::shift($!iter))
  40. ),
  41. IterationEnd
  42. )
  43. )
  44. }
  45. method skip-one() {
  46. nqp::if(
  47. $!on-value,
  48. nqp::not_i($!on-value = 0), # skipped a value
  49. nqp::if(
  50. $!iter, # if false, we didn't skip
  51. nqp::stmts( # skipped a key
  52. nqp::shift($!iter),
  53. ($!on-value = 1)
  54. )
  55. )
  56. )
  57. }
  58. method count-only() {
  59. nqp::p6box_i(
  60. nqp::add_i(nqp::elems($!storage),nqp::elems($!storage))
  61. )
  62. }
  63. }.new(%!elems))
  64. }
  65. multi method values(Set:D:) { True xx self.total }
  66. multi method grab(Set:D: $count?) {
  67. X::Immutable.new( method => 'grab', typename => self.^name ).throw;
  68. }
  69. multi method grabpairs(Set:D $count?) {
  70. X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
  71. }
  72. method Set { self }
  73. method SetHash { SetHash.new(self.keys) }
  74. multi method AT-KEY(Set:D: \k --> Bool:D) {
  75. %!elems.EXISTS-KEY(k.WHICH);
  76. }
  77. multi method ASSIGN-KEY(Set:D: \k,\v) {
  78. X::Assignment::RO.new(typename => self.^name).throw;
  79. }
  80. multi method DELETE-KEY(Set:D: \k) {
  81. X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
  82. }
  83. }