FunctionElementsInArray
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Methods to count number of elements in arrays
library("FunctionElementsInArray")

import RicardoSantos/DebugConsole/2 as console
[__T, __C] = console.init(20)

// @function Counts the number of elements equal to provided value in array.
// @param sample float array, sample data to process.
// @param value float value to check for equality.
// @returns int.
export count(float[] sample, float value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> count_float(): "sample" has no elements.')
	//
	float[] _copy = array.copy(sample)
	int _count = 0
	for _i = 1 to _size_s
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        _count += 1
    _count
//{ usage:
float[] count_a = array.from(0.1, 2, 0.3, 0.3, 77, 0.7, 0.3)
console.queue_one(__C, str.format('there is {1} elements with value 0.3 in {0}', count_a, count(count_a, 0.3)))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array.
// @param sample int array, sample data to process.
// @param value int value to check for equality.
// @returns int.
export count (int[] sample, int value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> count_int(): "sample" has no elements.')
	//
	int[] _copy = array.copy(sample)
	int _count = 0
	for _i = 1 to _size_s
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        _count += 1
    _count
//{ usage:
int[] count_b = array.from(1, 2, 3, 3, 77, 7, 3)
console.queue_one(__C, str.format('there is {1} elements with value 3 in {0}', count_b, count(count_b, 3)))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array.
// @param sample string array, sample data to process.
// @param value string value to check for equality.
// @returns int.
export count (string[] sample, string value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> count_string(): "sample" has no elements.')
	//
	string[] _copy = array.copy(sample)
	int _count = 0
	for _i = 1 to _size_s
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        _count += 1
    _count
//{ usage:
string[] count_c = array.from("1", "2", "3", "3", "77", "7", "3")
console.queue_one(__C, str.format('there is {1} elements with value "3" in "{0}"', count_c, count(count_c, "3")))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array.
// @param sample bool array, sample data to process.
// @param value bool value to check for equality.
// @returns int.
export count (bool[] sample, bool value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> count_bool(): "sample" has no elements.')
	//
	bool[] _copy = array.copy(sample)
	int _count = 0
	for _i = 1 to _size_s
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        _count += 1
    _count
//{ usage:
bool[] count_d = array.from(false, false, true, true, false, true, false)
console.queue_one(__C, str.format('there is {1} elements with value "true" in {0}', count_d, count(count_d, true)))
//{ remarks:
//}}}


// @function Counts the number of elements equal to provided value in array.
// @param sample color array, sample data to process.
// @param value color value to check for equality.
// @returns int.
export count (color[] sample, color value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> count_color(): "sample" has no elements.')
	//
	color[] _copy = array.copy(sample)
	int _count = 0
	for _i = 1 to _size_s
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        _count += 1
    _count
//{ usage:
color[] count_e = array.from(#000000, #ffff25, #ffffff, #ffffff, #ffffff00, #000000, #ffffff)
console.queue_one(__C, str.format('there is {1} elements with value "#ffffff" in {0}', "------", count(count_e, #ffffff))) // cant stringify color
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array, and returns its indices.
// @param sample float array, sample data to process.
// @param value float value to check for equality.
// @returns int.
export extract_indices (float[] sample, float value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> extract_indices_float(): "sample" has no elements.')
	//
	float[] _copy = array.copy(sample)
	int[] _indices = array.new_int(0)
	int _count = 0
	for _i = 0 to _size_s-1
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        array.push(_indices, _idx + _i) // rectified for exclusion
	        _count += 1
    [_count, _indices]
//{ usage:
[eif_count, eif_indices] = extract_indices(count_a, 0.3)
console.queue_one(__C, str.format('there is {1} elements at indices({2}) with value 0.3 in {0}', count_a, eif_count, eif_indices))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array, and returns its indices.
// @param sample int array, sample data to process.
// @param value int value to check for equality.
// @returns int.
export extract_indices (int[] sample, int value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> extract_indices_int(): "sample" has no elements.')
	//
	int[] _copy = array.copy(sample)
	int[] _indices = array.new_int(0)
	int _count = 0
	for _i = 0 to _size_s-1
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        array.push(_indices, _idx + _i) // rectified for exclusion
	        _count += 1
    [_count, _indices]
//{ usage:
[eii_count, eii_indices] = extract_indices(count_b, 3)
console.queue_one(__C, str.format('there is {1} elements at indices({2}) with value 3 in {0}', count_b, eii_count, eii_indices))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array, and returns its indices.
// @param sample string array, sample data to process.
// @param value string value to check for equality.
// @returns int.
export extract_indices (string[] sample, string value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> extract_indices_string(): "sample" has no elements.')
	//
	string[] _copy = array.copy(sample)
	int[] _indices = array.new_int(0)
	int _count = 0
	for _i = 0 to _size_s-1
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        array.push(_indices, _idx + _i) // rectified for exclusion
	        _count += 1
    [_count, _indices]
//{ usage:
[eis_count, eis_indices] = extract_indices(count_c, "3")
console.queue_one(__C, str.format('there is {1} elements at indices({2}) with value 3 in {0}', count_c, eis_count, eis_indices))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array, and returns its indices.
// @param sample bool array, sample data to process.
// @param value bool value to check for equality.
// @returns int.
export extract_indices (bool[] sample, bool value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> extract_indices_bool(): "sample" has no elements.')
	//
	bool[] _copy = array.copy(sample)
	int[] _indices = array.new_int(0)
	int _count = 0
	for _i = 0 to _size_s-1
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        array.push(_indices, _idx + _i) // rectified for exclusion
	        _count += 1
    [_count, _indices]
//{ usage:
[eib_count, eib_indices] = extract_indices(count_d, true)
console.queue_one(__C, str.format('there is {1} elements at indices({2}) with value true in {0}', count_c, eis_count, eis_indices))
//{ remarks:
//}}}

// @function Counts the number of elements equal to provided value in array, and returns its indices.
// @param sample color array, sample data to process.
// @param value color value to check for equality.
// @returns int.
export extract_indices (color[] sample, color value) => //{
	int _size_s = array.size(sample)
	//
	switch
	    (_size_s < 1) => runtime.error('FunctionElementsInArray -> extract_indices_color(): "sample" has no elements.')
	//
	color[] _copy = array.copy(sample)
	int[] _indices = array.new_int(0)
	int _count = 0
	for _i = 0 to _size_s-1
	    int _idx = array.indexof(_copy, value)
	    if _idx < 0
	        break
	        0
	    else
	        array.remove(_copy, _idx)
	        array.push(_indices, _idx + _i) // rectified for exclusion
	        _count += 1
    [_count, _indices]
//{ usage:
[eic_count, eic_indices] = extract_indices(count_e, #ffffff)
console.queue_one(__C, str.format('there is {1} elements at indices({2}) with value #ffffff in {0}', '------', eic_count, eic_indices)) // cant stringify color
//{ remarks:
//}}}



console.update(__T, __C)