mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* Return Auth service Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Compose to run with SpiceDB and Auth svc Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth gRPC API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Users' policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Groups to internal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use shared groups in Users Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove unused code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use pkg Groups in Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove Things groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Make imports consistent Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Groups networking Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Remove things groups-specific API Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Things Clients to the root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Move Clients to Users root Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Temporarily remove tracing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix imports Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add buffer config for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth type for Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use Auth for login Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add temporary solution for refresh token Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Tokenizer interface Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Updade tokens issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix token issuing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update JWT validator and refactor Tokenizer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename access timeout Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Rename login to authenticate Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update Identify to use SubjectID Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add Auth to Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Use the Auth service for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update auth schema Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix Auth for Groups Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add auth for addons (#14) Signed-off-by: Arvindh <arvindh91@gmail.com> Speparate Login and Refresh tokens Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Merge authN and authZ requests for things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add connect and disconnect Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update sharing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Fix policies addition and removal Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Update relation with roels Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Add gRPC to Things Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Assign and Unassign members to group and Listing of Group members (#15) * add auth for addons Signed-off-by: Arvindh <arvindh91@gmail.com> * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Move coap mqtt and ws policies to spicedb (#16) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Remove old policies Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> NOISSUE - Things authorize to return thingID (#18) This commit modifies the authorize endpoint to the grpc endpoint to return thingID. The authorize endpoint allows adapters to get the publisher of the message. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Add Groups to users service (#17) * add assign and unassign to group Signed-off-by: Arvindh <arvindh91@gmail.com> * add group incomplete repo implementation Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 1 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users stable 2 Signed-off-by: Arvindh <arvindh91@gmail.com> * groups for users & things Signed-off-by: Arvindh <arvindh91@gmail.com> * Amend signature Signed-off-by: Arvindh <arvindh91@gmail.com> * fix merge error Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix es code (#21) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Fix Bugs (#20) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Test e2e (#19) * fix: connect method Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix: e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * fix changes in sdk and e2e Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(docker): remove unnecessary port mapping Remove the port mapping for MQTT broker in the docker-compose.yml file. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Enable group listing Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(responses): update ChannelsPage struct The ChannelsPage struct in the responses.go file has been updated. The "Channels" field has been renamed to "Groups" to provide more accurate naming. This change ensures consistency and clarity in the codebase. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(things): add UpdateClientSecret method Add the UpdateClientSecret method to the things service. This method allows updating the client secret for a specific client identified by the provided token, id, and key parameters. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use smaller buffers for gRPC Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean up tests (#22) Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add Connect Disconnect endpoints (#23) * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix bugs Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Things share with users (#25) * fix list of things in a channel and Add connect disconnect endpoint Signed-off-by: Arvindh <arvindh91@gmail.com> * add: things share with other users Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Rename gRPC Services (#24) * Rename things and users auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * docs: add authorization docs for gRPC services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Rename things and users grpc services Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove mainflux.env package Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: Listing of things, channels, groups, users (#26) * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> * add: listing of channels, users, groups, things Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Users (#27) * feat(groups): rename redis package to events - Renamed the `redis` package to `events` in the `internal/groups` directory. - Updated the file paths and names accordingly. - This change reflects the more accurate purpose of the package and improves code organization. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): Modify identity method Change request and response of identity method Add accessToken and refreshToken to Token response Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * clean up users, remove dead code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(users): add unit tests for user service This commit adds unit tests for the user service in the `users` package. The tests cover various scenarios and ensure the correct behavior of the service. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh <arvindh91@gmail.com> * add: list of user group Signed-off-by: Arvindh <arvindh91@gmail.com> * fix: otel handler operator name for endpoints Signed-off-by: Arvindh <arvindh91@gmail.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Things Service (#28) * Rework things service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * add tests Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Clean Up Auth Service (#30) * clean up auth service Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): remove unused import Remove the unused import of `emptypb` in `auth.pb.go`. This import is not being used in the codebase and can be safely removed. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * NOISSUE - Update API docs (#31) Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TODO comments and cleanup the code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update dependenices Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> --------- Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Co-authored-by: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Arvindh <30824765+arvindh123@users.noreply.github.com>
395 lines
9.5 KiB
Go
395 lines
9.5 KiB
Go
// Copyright 2019 Jimmy Zelinskie
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package stringz implements a collection of utility functions for
|
|
// manipulating strings and lists of strings.
|
|
package stringz
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// ErrInconsistentUnpackLen is returned when Unpack is provided two slices
|
|
// without the same length.
|
|
var ErrInconsistentUnpackLen = errors.New("the length of the unpacked is not equal to the provided input")
|
|
|
|
// SliceContains returns true if the provided string is in the provided string
|
|
// slice.
|
|
func SliceContains(ys []string, x string) bool {
|
|
for _, y := range ys {
|
|
if x == y {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SliceIndex returns the index of the first instance of x in ys, or -1 if it
|
|
// is not present.
|
|
func SliceIndex(ys []string, x string) int {
|
|
for i, y := range ys {
|
|
if x == y {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// Dedup returns a new slice with any duplicates removed.
|
|
func Dedup(xs []string) []string {
|
|
set := make(map[string]struct{}, 0)
|
|
ys := make([]string, 0, len(xs))
|
|
for _, x := range xs {
|
|
if _, alreadyExists := set[x]; alreadyExists {
|
|
continue
|
|
}
|
|
ys = append(ys, x)
|
|
set[x] = struct{}{}
|
|
}
|
|
|
|
return ys
|
|
}
|
|
|
|
// DefaultEmpty returns the fallback when val is empty string.
|
|
//
|
|
// This function is inspired by Python's `dict.get()`.
|
|
func DefaultEmpty(val, fallback string) string {
|
|
return Default(val, fallback, "")
|
|
}
|
|
|
|
// Default returns a fallback value when the provided value is equal to any
|
|
// of the zero values.
|
|
func Default(val, fallback string, zeroValues ...string) string {
|
|
for _, zeroValue := range zeroValues {
|
|
if val == zeroValue {
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
return val
|
|
}
|
|
|
|
// SliceEqual returns true if two string slices are the same.
|
|
// This function is sensitive to order.
|
|
func SliceEqual(xs, ys []string) bool {
|
|
if len(xs) != len(ys) {
|
|
return false
|
|
}
|
|
|
|
for i, x := range xs {
|
|
if x != ys[i] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// MatrixEqual returns true if two [][]string are equal.
|
|
// This function is sensitive to order.
|
|
func MatrixEqual(xs, ys [][]string) bool {
|
|
if len(xs) != len(ys) {
|
|
return false
|
|
}
|
|
|
|
for i, x := range xs {
|
|
if !SliceEqual(x, ys[i]) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// TrimPrefixIndex trims everything before the provided index.
|
|
func TrimPrefixIndex(s, index string) string {
|
|
i := strings.Index(s, index)
|
|
if i <= 0 {
|
|
return s
|
|
}
|
|
return s[i+len(index):]
|
|
}
|
|
|
|
// TrimSurrounding returns a string with both a prefix and suffix trimmed from
|
|
// it.
|
|
//
|
|
// Do not confuse this with strings.Trim() which removes characters in a cutset
|
|
// rather than working on prefixes and suffixes.
|
|
func TrimSurrounding(s, surrounding string) string {
|
|
s = strings.TrimPrefix(s, surrounding)
|
|
return strings.TrimSuffix(s, surrounding)
|
|
}
|
|
|
|
// SliceMap is a functional-style mapping function for slices of strings.
|
|
//
|
|
// This is particularly useful when you would normally use a for-loop, but want
|
|
// `defer` to execute for each iteration.
|
|
func SliceMap(xs []string, fn func(string) error) error {
|
|
for _, x := range xs {
|
|
err := fn(x)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Join is strings.Join, but variadic.
|
|
func Join(prefix string, xs ...string) string { return strings.Join(xs, prefix) }
|
|
|
|
// CopyStringMap returns a new copy of a map of strings.
|
|
func CopyStringMap(xs map[string]string) map[string]string {
|
|
// Zero allocation path.
|
|
if xs == nil {
|
|
return nil
|
|
}
|
|
|
|
ys := make(map[string]string, len(xs))
|
|
for k, v := range xs {
|
|
ys[k] = v
|
|
}
|
|
return ys
|
|
}
|
|
|
|
// Unpack assigns a slice into local variables.
|
|
func Unpack(xs []string, vars ...*string) error {
|
|
if len(xs) != len(vars) {
|
|
return ErrInconsistentUnpackLen
|
|
}
|
|
for i, x := range xs {
|
|
*vars[i] = x
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SplitExact splits the string `s` into `len(vars)` number of strings and
|
|
// unpacks them into those vars.
|
|
//
|
|
// Returns ErrInconsistentUnpackLen if len(vars) doesn't match the number of
|
|
// split segments.
|
|
func SplitExact(s, sep string, vars ...*string) error {
|
|
exploded := strings.Split(s, sep)
|
|
return Unpack(exploded, vars...)
|
|
}
|
|
|
|
// SplitInto splits the string `s` into `len(vars)` number of strings and
|
|
// unpacks them into those vars. If there are more substrings that would be
|
|
// split after len(vars), they will be all be put into the final variable.
|
|
//
|
|
// Returns ErrInconsistentUnpackLen if len(vars) is greater than the number
|
|
// of split substrings.
|
|
func SplitInto(s, sep string, vars ...*string) error {
|
|
exploded := strings.SplitN(s, sep, len(vars))
|
|
return Unpack(exploded, vars...)
|
|
}
|
|
|
|
// SlicePermutations returns all permutations of a string slice.
|
|
//
|
|
// It is equivalent to `SliceCombinationsR(xs, len(xs))`.
|
|
func SlicePermutations(xs []string) [][]string {
|
|
return SlicePermutationsR(xs, len(xs))
|
|
}
|
|
|
|
// SlicePermutationsR returns successive r-length permutations of elements in
|
|
// the provided string slice.
|
|
//
|
|
// If r is less than 0 or larger than the length of the pool, nil is returned.
|
|
//
|
|
// The permutation tuples are emitted in lexicographic ordering according to
|
|
// the order of the input iterable. So, if the input iterable is sorted, the
|
|
// combination tuples will be produced in sorted order.
|
|
//
|
|
// Elements are treated as unique based on their position, not on their value.
|
|
// So if the input elements are unique, there will be no repeat values in each
|
|
// permutation.
|
|
//
|
|
// This is the algorithm used in Python's itertools library:
|
|
// itertools.permutations(iterable, r=None)
|
|
func SlicePermutationsR(pool []string, r int) [][]string {
|
|
if r <= 0 || pool == nil || r > len(pool) {
|
|
return nil
|
|
}
|
|
n := len(pool)
|
|
|
|
indices := make([]int, n)
|
|
for i := range pool {
|
|
indices[i] = i
|
|
}
|
|
|
|
var ys [][]string
|
|
|
|
{
|
|
var y []string
|
|
for _, i := range indices[:r] {
|
|
y = append(y, pool[i])
|
|
}
|
|
ys = append(ys, y)
|
|
}
|
|
|
|
cycles := make([]int, n-(n-r))
|
|
for i := range cycles {
|
|
cycles[i] = n - i
|
|
}
|
|
|
|
for {
|
|
broke := false
|
|
|
|
for i := r - 1; i >= 0; i-- {
|
|
cycles[i] = cycles[i] - 1
|
|
if cycles[i] == 0 {
|
|
indices = append(indices[:i], append(indices[i+1:], indices[i:i+1]...)...)
|
|
cycles[i] = n - i
|
|
} else {
|
|
j := cycles[i]
|
|
indices[i], indices[len(indices)-j] = indices[len(indices)-j], indices[i]
|
|
|
|
var y []string
|
|
for _, i := range indices[:r] {
|
|
y = append(y, pool[i])
|
|
}
|
|
ys = append(ys, y)
|
|
|
|
broke = true
|
|
break
|
|
}
|
|
}
|
|
if !broke {
|
|
return ys
|
|
}
|
|
}
|
|
}
|
|
|
|
// SliceCombinationsR returns r-length subsequences of elements from the
|
|
// provided string slice.
|
|
//
|
|
// If r is less than 0 or larger than the length of the pool, nil is returned.
|
|
//
|
|
// The combinations are emitted in lexicographic ordering according to the
|
|
// order of the input iterable. So, if the input iterable is sorted, the
|
|
// combination tuples will be produced in sorted order.
|
|
//
|
|
// Elements are treated as unique based on their position, not on their value.
|
|
// So if the input elements are unique, there will be no repeat values in each
|
|
// combination.
|
|
//
|
|
// This is the algorithm used in Python's itertools library:
|
|
// itertools.combinations(iterable, r)
|
|
func SliceCombinationsR(pool []string, r int) [][]string {
|
|
if r <= 0 || pool == nil || r > len(pool) {
|
|
return nil
|
|
}
|
|
n := len(pool)
|
|
|
|
indices := make([]int, r)
|
|
for i := range indices {
|
|
indices[i] = i
|
|
}
|
|
|
|
var ys [][]string
|
|
|
|
{
|
|
var y []string
|
|
for _, j := range indices {
|
|
y = append(y, pool[j])
|
|
}
|
|
ys = append(ys, y)
|
|
}
|
|
|
|
for {
|
|
var i int
|
|
broke := false
|
|
for i = r - 1; i >= 0; i-- {
|
|
if indices[i] != i+n-r {
|
|
broke = true
|
|
break
|
|
}
|
|
}
|
|
if !broke {
|
|
return ys
|
|
}
|
|
|
|
indices[i] = indices[i] + 1
|
|
for j := i + 1; j < r; j++ {
|
|
indices[j] = indices[j-1] + 1
|
|
}
|
|
|
|
var y []string
|
|
for _, j := range indices {
|
|
y = append(y, pool[j])
|
|
}
|
|
ys = append(ys, y)
|
|
}
|
|
}
|
|
|
|
// SliceCombinationsWithReplacement returns r-length subsequences of elements
|
|
// from the provided string slice allowing individual elements to be repeated
|
|
// more than once.
|
|
//
|
|
// If r is less than 0 or larger than the length of the pool, nil is returned.
|
|
//
|
|
// The combination tuples are emitted in lexicographic ordering according to
|
|
// the order of the input iterable. So, if the input iterable is sorted, the
|
|
// combination tuples will be produced in sorted order.
|
|
//
|
|
// Elements are treated as unique based on their position, not on their value.
|
|
// So if the input elements are unique, the generated combinations will also be
|
|
// unique.
|
|
//
|
|
// This is the algorithm used in Python's itertools library:
|
|
// itertools.combinations_with_replacement(iterable, r)
|
|
func SliceCombinationsWithReplacement(pool []string, r int) [][]string {
|
|
if r <= 0 || pool == nil || r > len(pool) {
|
|
return nil
|
|
}
|
|
n := len(pool)
|
|
|
|
indices := make([]int, r)
|
|
var ys [][]string
|
|
|
|
{
|
|
var y []string
|
|
for _, j := range indices {
|
|
y = append(y, pool[j])
|
|
}
|
|
ys = append(ys, y)
|
|
}
|
|
|
|
for {
|
|
var i int
|
|
broke := false
|
|
for i = r - 1; i >= 0; i-- {
|
|
if indices[i] != n-1 {
|
|
broke = true
|
|
break
|
|
}
|
|
}
|
|
if !broke {
|
|
return ys
|
|
}
|
|
|
|
newIndices := make([]int, r-i)
|
|
for j := range newIndices {
|
|
newIndices[j] = indices[i] + 1
|
|
}
|
|
indices = append(indices[:i], newIndices...)
|
|
|
|
var y []string
|
|
for _, j := range indices {
|
|
y = append(y, pool[j])
|
|
}
|
|
ys = append(ys, y)
|
|
}
|
|
}
|