You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.5 KiB

  1. package main
  2. import (
  3. "math/rand"
  4. "sort"
  5. "github.com/mroth/weightedrand"
  6. )
  7. // combinations takes input in the form of a map of item lists, and returns a
  8. // list of all combinations of each item for each key. E.g.:
  9. //
  10. // {"foo": [1, 2, 3], "bar": [4, 5, 6]}
  11. //
  12. // Will return the following maps:
  13. //
  14. // {"foo": 1, "bar": 4}
  15. // {"foo": 1, "bar": 5}
  16. // {"foo": 1, "bar": 6}
  17. // {"foo": 2, "bar": 4}
  18. // {"foo": 2, "bar": 5}
  19. // {"foo": 2, "bar": 6}
  20. // {"foo": 3, "bar": 4}
  21. // {"foo": 3, "bar": 5}
  22. // {"foo": 3, "bar": 6}
  23. func combinations(items map[string][]interface{}) []map[string]interface{} {
  24. keys := []string{}
  25. for key := range items {
  26. keys = append(keys, key)
  27. }
  28. sort.Strings(keys)
  29. return combiner(map[string]interface{}{}, keys, items)
  30. }
  31. // combiner is a utility function for combinations.
  32. func combiner(head map[string]interface{}, pending []string, items map[string][]interface{}) []map[string]interface{} {
  33. if len(pending) == 0 {
  34. return []map[string]interface{}{head}
  35. }
  36. key, pending := pending[0], pending[1:]
  37. result := []map[string]interface{}{}
  38. for _, value := range items[key] {
  39. path := map[string]interface{}{}
  40. for k, v := range head {
  41. path[k] = v
  42. }
  43. path[key] = value
  44. result = append(result, combiner(path, pending, items)...)
  45. }
  46. return result
  47. }
  48. // uniformChoice chooses a single random item from the argument list, uniformly weighted.
  49. type uniformChoice []interface{}
  50. func (uc uniformChoice) Choose(r *rand.Rand) interface{} {
  51. return uc[r.Intn(len(uc))]
  52. }
  53. // probSetChoice picks a set of strings based on each string's probability (0-1).
  54. type probSetChoice map[string]float64
  55. func (pc probSetChoice) Choose(r *rand.Rand) []string {
  56. choices := []string{}
  57. for item, prob := range pc {
  58. if r.Float64() <= prob {
  59. choices = append(choices, item)
  60. }
  61. }
  62. return choices
  63. }
  64. // uniformSetChoice picks a set of strings with uniform probability, picking at least one.
  65. type uniformSetChoice []string
  66. func (usc uniformSetChoice) Choose(r *rand.Rand) []string {
  67. choices := []string{}
  68. indexes := r.Perm(len(usc))
  69. if len(indexes) > 1 {
  70. indexes = indexes[:1+r.Intn(len(indexes)-1)]
  71. }
  72. for _, i := range indexes {
  73. choices = append(choices, usc[i])
  74. }
  75. return choices
  76. }
  77. type weightedChoice map[string]uint
  78. func (wc weightedChoice) Choose(r *rand.Rand) string {
  79. choices := make([]weightedrand.Choice, 0, len(wc))
  80. for k, v := range wc {
  81. choices = append(choices, weightedrand.NewChoice(k, v))
  82. }
  83. chooser, err := weightedrand.NewChooser(choices...)
  84. if err != nil {
  85. panic(err)
  86. }
  87. return chooser.PickSource(r).(string)
  88. }