Generic GetRandomItems with seeding, partial FilterLocations for querying

This commit is contained in:
2023-12-12 01:27:15 -06:00
parent 7240eaeea2
commit 9f2941cbd2

View File

@@ -1,8 +1,12 @@
package main package main
import ( import (
"fmt"
"io" "io"
"log"
"math/rand"
"net/http" "net/http"
"reflect"
"strings" "strings"
) )
@@ -70,3 +74,44 @@ func SetTypicalHeaders(req *http.Request, contentType *string, referrer *string,
req.Header.Set("Referer", *referrer) req.Header.Set("Referer", *referrer)
} }
} }
func GetRandomItems[T any](arr []T, N int, seed_value int64) ([]T, error) {
randgen := rand.New(rand.NewSource(seed_value))
arrValue := reflect.ValueOf(arr)
if arrValue.Kind() != reflect.Slice {
return nil, fmt.Errorf("input is not a slice")
}
if arrValue.Len() < N {
return nil, fmt.Errorf("array length is less than N")
}
selectedIndices := make(map[int]bool)
selectedItems := make([]T, 0, N)
for len(selectedItems) < N {
randomIndex := randgen.Intn(arrValue.Len())
// Check if the index is not already selected
if !selectedIndices[randomIndex] {
selectedIndices[randomIndex] = true
selectedItems = append(selectedItems, arrValue.Index(randomIndex).Interface().(T))
}
}
return selectedItems, nil
}
func FilterLocations(all_locations []Location, query string, limit int, seed_value int64) []Location {
if len(query) == 0 {
randomized, err := GetRandomItems(all_locations, limit, seed_value)
if err != nil {
log.Fatal(err)
}
return randomized
}
filtered_locations := make([]Location, 0, limit)
return filtered_locations
}