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.

109 lines
2.7 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 { return usc.ChooseAtLeast(r, 1) }
  67. func (usc uniformSetChoice) ChooseAtLeast(r *rand.Rand, num int) []string {
  68. choices := []string{}
  69. indexes := r.Perm(len(usc))
  70. if num < len(indexes) {
  71. indexes = indexes[:1+randomInRange(r, num, len(indexes)-1)]
  72. }
  73. for _, i := range indexes {
  74. choices = append(choices, usc[i])
  75. }
  76. return choices
  77. }
  78. func randomInRange(r *rand.Rand, min, max int) int { return r.Intn(max-min+1) + min }
  79. type weightedChoice map[string]uint
  80. func (wc weightedChoice) Choose(r *rand.Rand) string {
  81. choices := make([]weightedrand.Choice, 0, len(wc))
  82. for k, v := range wc {
  83. choices = append(choices, weightedrand.NewChoice(k, v))
  84. }
  85. chooser, err := weightedrand.NewChooser(choices...)
  86. if err != nil {
  87. panic(err)
  88. }
  89. return chooser.PickSource(r).(string)
  90. }