forked from tommybennett/algorithm-mnemonics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.sh
executable file
·204 lines (185 loc) · 4.62 KB
/
list.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#! /bin/bash
################################################################################
# Name: list.sh
#
# Create documentation from Algorithm Mnemonics XML document.
#
# Usage: list.sh [-cm]
#
# Where: -c Write categories
# -m Write markdown format
#
# Author: Tommy Bennett, tommy_bennett@icloud.com
# Date: 2016-10-15
################################################################################
# Constants
declare -r ALGORITHM_MNEMONICS="algorithm_mnemonics.xml"
declare -r SCRIPT_NAME="list.sh"
declare -r SCRIPT_DESCRIPTION="Algorithm Mnemonics Documentation"
declare -r VERSION="version 1.0"
declare -a NAME
declare -a EXPANDEDNAME
declare -r NONMODIFYING=0
declare -r SORTING=1
declare -r SORTEDRANGES=2
declare -r MODIFYING=3
declare -r REMOVING=4
declare -r MUTATING=5
declare -r NUMERIC=6
declare -r IDIOMS=7
declare -r STRINGS=8
declare -r STREAMS=9
declare -r CONTAINERS=10
# Prints the usage information to standard output.
#
function usage() {
echo "$SCRIPT_NAME ($SCRIPT_DESCRIPTION) $VERSION"
echo
echo "usage: $SCRIPT_NAME [-cm]"
echo
echo "where: -c Write categories"
echo " -m Write markdown format"
echo
exit 1
}
# Prints the version information to standard output.
#
function version() {
echo "$SCRIPT_NAME ($SCRIPT_DESCRIPTION) $VERSION"
echo "Copyright (C) 2016 Tommy Bennett."
echo "License GPLv3+: GNU GPL version 3 or" \
"later <http://gnu.org/licenses/gpl.html>"
echo "This is free software: you are free to change and redistribute it."
echo "There is NO WARRANTY, to the extent permitted by law."
echo
echo "Written by Tommy Bennett."
exit 1
}
# Create algorithm name array
#
function create_name() {
NAME[$NONMODIFYING]="fre:cnt:cni:mne:mxe:mme:fnd:fni:fin:fne:srh:srn:ffo:ajf:eql:ipr:msm:iss:isu:ipt:ppt:ihp:ihu:alo:ano:nno:lxc"
NAME[$SORTING]="srt:sts:pst:psc:nth:ptn:spt:ptc:mkh:phh:pph:sth"
NAME[$SORTEDRANGES]="bns::inc:lwb:upb:eqr:erl:mrg:stu:stn:std:ssd:ipm:ucp"
NAME[$MODIFYING]="cpy:cpi:cpn:cpb:mov:mvb:tfm:mrg:swp:swr:fil:fln:gnr:gnn:rpl:rpi:rpc:rci:ita"
NAME[$REMOVING]="rmv:rmi:rmc:rmf:uqe"
NAME[$MUTATING]="rvr:rvc:rte:rtc:nxp:prp:shf"
NAME[$NUMERIC]="acm:acl"
NAME[$STRINGS]="ltr:trm:lwr:upr"
NAME[$STREAMS]="sto:sti:oit"
NAME[$IDIOMS]="erm"
NAME[$CONTAINERS]="stv"
}
# Create the expanded name array
#
function create_expanded_name() {
mnemonics=$(print_all)
for n in $mnemonics
do
if [ -z "$name" ]; then
name=$n
find_category $name
else
if [ -z "${EXPANDEDNAME[$index]}" ]; then
EXPANDEDNAME[$index]="$name $n"
else
EXPANDEDNAME[$index]="${EXPANDEDNAME[$index]}:$name $n"
fi
name=""
fi
done
}
# Print category name
#
function print_category() {
case $1 in
$NONMODIFYING ) echo "Nonmodifying" ;;
$SORTING ) echo "Sorting" ;;
$SORTEDRANGES ) echo "Sorted Ranges" ;;
$MODIFYING ) echo "Modifying" ;;
$REMOVING ) echo "Removing" ;;
$MUTATING ) echo "Mutating" ;;
$NUMERIC ) echo "Numeric" ;;
$IDIOMS ) echo "Idioms" ;;
$STRINGS ) echo "Strings" ;;
$STREAMS ) echo "Streams" ;;
$CONTAINERS ) echo "Containers" ;;
esac
}
# Print all algorithm mnemonics
#
function print_all() {
grep "n=" $ALGORITHM_MNEMONICS | awk -F'"' '{print $2" "$4}' | sort
}
# Print all algorithm mnemonics with category
#
function print_all_with_category() {
local IFS=":"
end=${#NAME[@]}
for (( i=0; i<end; i++ ))
do
if [ $print_markdown -eq 1 ]; then
echo -n "### "
fi
print_category $i
echo
for n in ${EXPANDEDNAME[$i]}
do
echo -n "$n" | tr " " "\t"
echo " "
done
echo
done
}
# Find the algorithm category
#
function find_category() {
local IFS=":"
end=${#NAME[@]}
for (( i=0; i<end; i++ ))
do
for n in ${NAME[$i]}
do
if [ "$1" == "$n" ]; then
index=$i
return
fi
done
done
echo "Error: $1 not found"
exit 1
}
# Print algorithm mnemonics by category
#
function print_categories() {
create_name
create_expanded_name
print_all_with_category
}
################################################################################
print_categories=0
print_markdown=0
# Process long options
for option in "$@"; do
case $option in
"--version" ) version ;;
"--help" ) usage ;;
esac
done
# Process single-letter options
while getopts "cm" option; do
case $option in
c ) print_categories=1 ;;
m ) print_markdown=1 ;;
\? ) usage ;;
esac
done
shift $((OPTIND - 1))
if [ $print_markdown -eq 1 ]; then
echo "## STL Instruction Set"
fi
if [ $print_categories -eq 1 ]; then
print_categories
else
print_all
fi