Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions ar.bash
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ ar::reverse() {
## @param arr The array to setify
## @param arr_set The array to write the result into
ar::set() {
local -n arr="$1"
local -n arr_set="$2"
local -A assoc
for val in "${arr[@]}"; do
assoc[$val]=1
local -n _set_arr="$1"
local -n _set_arr_set="$2"
local -A _set_assoc
for val in "${_set_arr[@]}"; do
_set_assoc[$val]=1
done
arr_set=("${!assoc[@]}")
_set_arr_set=("${!_set_assoc[@]}")
}

## @fn ar::in_set()
Expand Down Expand Up @@ -501,7 +501,7 @@ ar::is_proper_subset() {
}

## @fn ar::is_proper_superset
## @brief Return true if _proper_superset_arr1 is a superset of _proper_superset_arr2
## @brief Return 0 if _proper_superset_arr1 is a superset of _proper_superset_arr2
## @param _proper_superset_arr1 The first array name
## @param _proper_superset_arr2 The second array name
## @retval 0 if _proper_superset_arr1 is a superset of _proper_superset_arr2, non-zero otherwise
Expand All @@ -519,6 +519,23 @@ ar::is_proper_superset() {
return 0
}

## @fn ar::is_disjoint
## @brief Return 0 if _disjoint_arr1 is a superset of _disjoint_arr2
## @param _disjoint_arr1 The first array name
## @param _disjoint_arr2 The second array name
## @retval 0 if _disjoint_arr1 and _disjoint_arr2 are disjoint sets, non-zero otherwise
ar::is_disjoint() {
local -n _disjoint_arr1="$1"
local -n _disjoint_arr2="$2"
for i in "${_disjoint_arr1[@]}"; do
if ar::in_set _disjoint_arr2 "$i"; then
return 1
fi
done
return 0
}


## @fn array_to_string
## @brief Turn an array into a string with seperator
## @param vname_of_array The variable name of the array
Expand Down
45 changes: 45 additions & 0 deletions examples/longest_consecutive.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Source the library
. "../ar.bash"

max() {
local -i _first="$1"
local -i _second="$2"
if (( _second > _first)); then
echo "$_second"
else
echo "$_first"
fi
}

## @fn longest_consecutive()
## @brief Return the count of the longest consecuritve string of numbers
longest_consecutive() {
local -a _arr=("$@")
local -a _arr_set
local -i cur
local -i cnt=0
local -i res=0
# Setify array
ar::set _arr _arr_set
for val in "${_arr[@]}"; do
if ar::in_set _arr_set "$val" && ! ar::in_set _arr_set $(( val - 1 )); then
cur="$val"
cnt=0
while ar::in_set _arr_set "$cur"; do
# Remove number to avoid recomputation
ar::remove _arr_set "$cur"
(( cur++ ))
(( cnt++ ))
done
# Update optimal length
res="$(max "$res" "$cnt")"
fi
done
echo "$res"
}

main() {
echo "$(longest_consecutive 2 6 1 9 4 5 3)"
}

main "$@"
14 changes: 14 additions & 0 deletions tests/test_ar.bats
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,17 @@ setup() {
run ar::is_proper_superset first_arr second_arr
assert_failure
}

@test "ar::is_disjoint returns 0 if set1 and set2 are disjoint" {
local -a first_arr=(rice beans sausage)
local -a second_arr=(beef pasta cheese)
run ar::is_disjoint first_arr second_arr
assert_success
}

@test "ar::is_disjoint returns non-zero if set1 and set2 are not disjoint" {
local -a first_arr=(rice beans sausage)
local -a second_arr=(rice beef beans)
run ar::is_disjoint first_arr second_arr
assert_failure
}
Loading