permutation█ OVERVIEW
This library provides functions for generating permutations of string or float arrays, using an iterative approach where pine has no recursion. It supports allowing/limiting duplicate elements and handles large result sets by segmenting them into manageable chunks within custom Data types. The most combinations will vary, but the highest is around 250,000 unique combinations. depending on input array values and output length. it will return nothing if the input count is too low.
█ CONCEPTS
This library addresses two key challenges in Pine Script:
• Recursion Depth Limits: Pine has limitations on recursion depth. This library uses an iterative, stack-based algorithm to generate permutations, avoiding recursive function calls that could exceed these limits.
• Array Size Limits: Pine arrays have size restrictions. This library manages large permutation sets by dividing them into smaller segments stored within a custom Data or DataFloat type, using maps for efficient access.
█ HOW TO USE
1 — Include the Library: Add this library to your script using:
import kaigouthro/permutation/1 as permute
2 — Call the generatePermutations Function:
stringPermutations = permute.generatePermutations(array.from("a", "b", "c"), 2, 1)
floatPermutations = permute.generatePermutations(array.from(1.0, 2.0, 3.0), 2, 1)
• set : The input array of strings or floats.
• size : The desired length of each permutation.
• maxDuplicates (optional): The maximum allowed repetitions of an element within a single permutation. Defaults to 1.
3 — Access the Results: The function returns a Data (for strings) or DataFloat (for floats) object. These objects contain:
• data : An array indicating which segments are present (useful for iterating).
• segments : A map where keys represent segment indices and values are the actual permutation data within that segment.
Example: Accessing Permutations
for in stringPermutations.segments
for in currentSegment.segments
// Access individual permutations within the segment.
permutation = segmennt.data
for item in permutation
// Use the permutation elements...
█ TYPES
• PermutationState / PermutationStateFloat : Internal types used by the iterative algorithm to track the state of permutation generation.
• Data / DataFloat : Custom types to store and manage the generated permutations in segments.
█ NOTES
* The library prioritizes handling potentially large permutation sets. 250,000 i about the highest achievable.
* The segmentation logic ensures that results are accessible even when the total number of permutations exceeds Pine's array size limits.
----
Library "permutation"
This library provides functions for generating permutations of user input arrays containing either strings or floats. It uses an iterative, stack-based approach to handle potentially large sets and avoid recursion limitation. The library supports limiting the number of duplicate elements allowed in each permutation. Results are stored in a custom Data or DataFloat type that uses maps to segment large permutation sets into manageable chunks, addressing Pine Script's array size limitations.
generatePermutations(set, size, maxDuplicates)
> Generates permutations of a given size from a set of strings or floats.
Parameters:
set (array) : (array or array) The set of strings or floats to generate permutations from.
size (int) : (int) The size of the permutations to generate.
maxDuplicates (int) : (int) The maximum number of times an element can be repeated in a permutation.
Returns: (Data or DataFloat) A Data object for strings or a DataFloat object for floats, containing the generated permutations.
stringPermutations = generatePermutations(array.from("a", "b", "c"), 2, 1)
floatPermutations = generatePermutations(array.from(1.0, 2.0, 3.0), 2, 1)
generatePermutations(set, size, maxDuplicates)
Parameters:
set (array)
size (int)
maxDuplicates (int)
PermutationState
PermutationState
Fields:
data (array) : (array) The current permutation being built.
index (series int) : (int) The current index being considered in the set.
depth (series int) : (int) The current depth of the permutation (number of elements).
counts (map) : (map) Map to track the count of each element in the current permutation (for duplicates).
PermutationStateFloat
PermutationStateFloat
Fields:
data (array) : (array) The current permutation being built.
index (series int) : (int) The current index being considered in the set.
depth (series int) : (int) The current depth of the permutation (number of elements).
counts (map) : (map) Map to track the count of each element in the current permutation (for duplicates).
Data
Data
Fields:
data (array) : (array) Array to indicate which segments are present.
segments (map) : (map) Map to store permutation segments. Each segment contains a subset of the generated permutations.
DataFloat
DataFloat
Fields:
data (array) : (array) Array to indicate which segments are present.
segments (map) : (map) Map to store permutation segments. Each segment contains a subset of the generated permutations.
Permutation
combinLibrary "combin"
Description:
The combin function is a the combination function
as it calculates the number of possible combinations for two given numbers.
This function takes two arguments: the number and the number_chosen.
For example, if the number is 5 and the number chosen is 1,
there are 5 combinations, giving 5 as a result.
Reference:
ideone.com
support.microsoft.com
combin(n, kin)
Returns the number of combinations for a given number of items. Use to determine the total possible number of groups for a given number of items.
Parameters:
n : int, The number of items.
kin : int, The number of items in each combination.
Returns: int.