1. my role QuantHash does Associative {
  2. method Int ( --> Int:D) { self.total.Int }
  3. method Num ( --> Num:D) { self.total.Num }
  4. method Numeric ( --> Numeric:D) { self.total.Numeric }
  5. method Real ( --> Real:D) { self.total.Real }
  6. method Capture() { self.Hash.Capture }
  7. multi method list(QuantHash:U:) { self.Any::list }
  8. multi method list(QuantHash:D:) { self.pairs.cache }
  9. method fmt(QuantHash: Cool $format = "%s\t\%s", $sep = "\n") {
  10. nqp::iseq_i(nqp::sprintfdirectives( nqp::unbox_s($format.Stringy)),1)
  11. ?? self.keys.fmt($format, $sep)
  12. !! self.pairs.fmt($format, $sep)
  13. }
  14. multi method AT-KEY(QuantHash:U \SELF: $key) is raw {
  15. nqp::istype(self, Set) || nqp::istype(self, Bag) || nqp::istype(self, Mix)
  16. ?? die "Cannot auto-vivify an immutable {SELF.^name}"
  17. !! (SELF = self.new).AT-KEY($key)
  18. }
  19. multi method pairs(QuantHash:D:) { Seq.new(self.iterator) }
  20. method raw_keys() {
  21. nqp::if(
  22. (my $elems := self.raw_hash),
  23. nqp::stmts(
  24. (my $keys := nqp::setelems(nqp::list_s,nqp::elems($elems))),
  25. (my int $i = -1),
  26. (my $iter := nqp::iterator($elems)),
  27. nqp::while(
  28. $iter,
  29. nqp::bindpos_s(
  30. $keys,
  31. ($i = nqp::add_i($i,1)),
  32. nqp::iterkey_s(nqp::shift($iter))
  33. )
  34. ),
  35. $keys
  36. ),
  37. nqp::list_s
  38. )
  39. }
  40. }