Commit 141e027b authored by Fernando Takagi's avatar Fernando Takagi
Browse files

Yield combinations one by one instead of storing all of them.

parent 2ccbe6ec
Loading
Loading
Loading
Loading
+23 −19
Original line number Diff line number Diff line
@@ -489,8 +489,7 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry {
			hitIndexes = append(hitIndexes, hitIndex)
		}

		hitIndexesCombinations := combinationsMinMax(hitIndexes, 1, index.MaxCount-index.Count)
		for _, hitIndexes := range hitIndexesCombinations {
		for hitIndexes := range combinationsMinMax(hitIndexes, 1, index.MaxCount-index.Count) {
			// Check the min and max counts are met across the hit.
			var hitCount int
			for _, hitIndex := range hitIndexes {
@@ -653,7 +652,11 @@ func (m *LocalMatchmaker) processCustom() [][]*MatchmakerEntry {
	return finalMatchedEntries
}

func combinationsMinMax[T any](from []T, min, max int) (combinations [][]T) {
func combinationsMinMax[T any](from []T, min, max int) <-chan []T {
	c := make(chan []T)

	go func() {
		defer close(c)
		length := uint(len(from))

		// Go through all possible combinations of from 1 (only first element in subset) to 2^length (all objects in subset)
@@ -671,7 +674,8 @@ func combinationsMinMax[T any](from []T, min, max int) (combinations [][]T) {
					combination = append(combination, from[element])
				}
			}
		combinations = append(combinations, combination)
			c <- combination
		}
	return combinations
	}()
	return c
}