aboutsummaryrefslogtreecommitdiffstats
path: root/src/comparable
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-01-21 08:59:49 +0100
committerMattias Andrée <maandree@operamail.com>2014-01-21 08:59:49 +0100
commit2638bc8806d4453fba67cd7e9bba0f36c7612fa7 (patch)
tree7b2d18de1ce23b90aa1152314cb4f78a112647ad /src/comparable
parentadd linear search (diff)
downloadalgorithms-and-data-structures-2638bc8806d4453fba67cd7e9bba0f36c7612fa7.tar.gz
algorithms-and-data-structures-2638bc8806d4453fba67cd7e9bba0f36c7612fa7.tar.bz2
algorithms-and-data-structures-2638bc8806d4453fba67cd7e9bba0f36c7612fa7.tar.xz
add binary search
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/comparable43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/comparable b/src/comparable
index 51762d3..0d3c21e 100644
--- a/src/comparable
+++ b/src/comparable
@@ -1,5 +1,7 @@
# -*- shell-script -*-
+Telement="${T}"
+Tarray="${Telement}[]"
Tparam=""
Targ=""
function T-array
@@ -8,13 +10,16 @@ function T-array
for elem in "$@"; do
elems="${elems:+${elems}, }${elem}"
done
- if [ $T = T ]; then
+ if [ "${T}" = T ]; then
echo "((${T}[])(new Object[] { ${elems} }))"
else
echo "(new ${T}[] { ${elems} })"
fi
}
-if [ $T = boolean ]; then
+if [ "${T}" = boolean ]; then
+ function cmp
+ { echo "(($1) == ($2) ? 0 : ($1) ? 1 : -1)"
+ }
function greater
{ echo "($1) & !($2)"
}
@@ -33,10 +38,13 @@ if [ $T = boolean ]; then
function less
{ echo "!($1) & ($2)"
}
-elif [ $T = T ] || [ $T = T+ ]; then
- if [ $T = T ]; then
+elif [[ "${T}" = @(T|T+|T++) ]]; then
+ if [ "${T}" = T ]; then
Tparam="<T> "
Targ=", Comparator<T> comparator"
+ function cmp
+ { echo "comparator.compare($1, $2)"
+ }
function greater
{ echo "comparator.compare($1, $2) > 0"
}
@@ -56,8 +64,18 @@ elif [ $T = T ] || [ $T = T+ ]; then
{ echo "comparator.compare($1, $2) < 0"
}
else
- Tparam="<T extends Comparable<? super T>> "
- Targ=""
+ if [ "${T}" = T++ ]; then
+ Tparam="<T> "
+ Targ=""
+ Telement="Comparable<T>"
+ Tarray="${Telement}[]"
+ else
+ Tparam="<T extends Comparable<? super T>> "
+ Targ=""
+ fi
+ function cmp
+ { echo "($1).compareTo($2)"
+ }
function greater
{ echo "($1).compareTo($2) > 0"
}
@@ -79,6 +97,19 @@ elif [ $T = T ] || [ $T = T+ ]; then
T=T
fi
else
+ if [ "${T}" = long ]; then
+ function cmp
+ { echo "(int)(($1) - ($2) | ($1) - ($2) >> 32L)"
+ }
+ elif [[ "${T}" = @(float|double) ]]; then
+ function cmp
+ { echo "(($1) < ($2) ? -1 : ($1) > ($2) ? 1 : 0)"
+ }
+ else
+ function cmp
+ { echo "(($1) - ($2))"
+ }
+ fi
function greater
{ echo "($1) > ($2)"
}