forked from cortesi/sortvis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dense
executable file
·124 lines (116 loc) · 3.04 KB
/
dense
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
#!/usr/bin/env python
import random, math, sys
from optparse import OptionParser
from libsortvis import sortable, graph
def main():
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option(
"-a",
dest="algorithm",
default=[],
type="choice",
action="append",
choices=[i.name for i in sortable.algorithms],
help="Draw only a named algorithm."
)
parser.add_option(
"-n",
dest="numelements",
default="256",
type="int",
help="Generate a random sorting sequence of length n"
)
parser.add_option(
"-f",
dest="readfile",
help="Read data from file"
)
parser.add_option(
"-g",
dest="grayscale",
default = False,
action="store_true",
)
parser.add_option(
"-o",
dest="ofname",
help="Output file name. Only usable when a single algorithm is being drawn.",
default=""
)
parser.add_option(
"-d",
dest="dump",
default=False,
action="store_true",
help="Dump sequence"
)
parser.add_option(
"-p",
dest="printout",
default=False,
action="store_true",
help="Print traversal rather than drawing graph"
)
parser.add_option(
"-t",
dest="title",
default=False,
action="store_true",
help="Add a title to image."
)
parser.add_option(
"-u",
dest="unmoved",
default=True,
action="store_false",
help="Don't draw elements until they move."
)
parser.add_option(
"-v",
dest="verbose",
default=False,
action="store_true",
help="Be verbose."
)
options, args = parser.parse_args()
if args:
parser.error("Script takes no arguments.")
if options.readfile:
txt = file(options.readfile).read().split()
lst = [int(i) for i in txt]
else:
lst = range(options.numelements)
random.shuffle(lst)
if options.dump:
for i in lst:
print i,
if options.grayscale:
ldrawer = graph.DenseGrayscale()
else:
ldrawer = graph.DenseFruitsalad()
if options.algorithm:
selected = [i.lower() for i in options.algorithm]
if options.algorithm:
todraw = [i for i in sortable.algorithms if i.name in options.algorithm]
else:
todraw = [i for i in sortable.algorithms]
if options.ofname and len(todraw) > 1:
parser.error("Cannot specify output file name when drawing more than one algorithm.")
for i in todraw:
a = i()
if options.verbose:
print a.name
sortd = a(lst)
if options.printout:
print a.name
for i in sortd:
print i.path
ldrawer.draw(
sortd,
a.name if options.title else None,
options.ofname or (a.name+".png"),
options.unmoved
)
if __name__ == "__main__":
main()