-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.go
371 lines (312 loc) · 7.04 KB
/
options.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) Harri Rautila, 2012
// This file is part of github.com/hrautila/linalg package.
// It is free software, distributed under the terms of GNU Lesser General Public
// License Version 3, or any later version. See the COPYING tile included in this archive.
package linalg
import (
"math"
"math/cmplx"
"strings"
)
// Interface for named options.
type Option interface {
// Name of option.
Name() string
// Integer value of option.
Int() int
// Float value of option or NaN.
Float() float64
// Float value of option or NaN.
Complex() complex128
// Bool value of option.
Bool() bool
// Option value as string.
String() string
// Test if other option is of equal
Equal(Option) bool
}
// Find named option from a list of options. Returns nil of not found.
func GetOption(name string, opts ...Option) Option {
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
return o
}
}
return nil
}
// Get integer option value. If option not present returns defval.
func GetIntOpt(name string, defval int, opts ...Option) (val int) {
val = defval
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
switch o.(type) {
case *IOpt:
val = o.Int()
return
}
}
}
return
}
// Get float option value. If option not present returns defval.
func GetFloatOpt(name string, defval float64, opts ...Option) (val float64) {
val = defval
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
switch o.(type) {
case *FOpt:
val = o.Float()
return
}
}
}
return
}
// Get boolean option value. If option not present returns defval.
func GetBoolOpt(name string, defval bool, opts ...Option) (val bool) {
val = defval
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
switch o.(type) {
case *BOpt:
val = o.Bool()
case *SOpt:
v := o.String()
val = v[0] == 't' || v[0] == 'T' || v[0] == 'y' || v[0] == 'Y'
}
return
}
}
return
}
// Get string option value. If option not present returns defval.
func GetStringOpt(name string, defval string, opts ...Option) (val string) {
val = defval
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
switch o.(type) {
case *SOpt:
val = o.String()
return
}
}
}
return
}
// Get string option value. If option not present returns defval.
func GetComplexOpt(name string, defval complex128, opts ...Option) (val complex128) {
val = defval
for _, o := range opts {
if strings.EqualFold(o.Name(), name) {
switch o.(type) {
case *COpt:
val = o.Complex()
return
}
}
}
return
}
func Equal(a, b Option) bool {
return a.Equal(b)
}
// Integer valued option.
type IOpt struct {
OptName string
Val int
}
// Return integer valued option.
func IntOpt(name string, val int) *IOpt {
return &IOpt{name, val}
}
func (O *IOpt) Name() string {
return O.OptName
}
func (O *IOpt) Int() int {
return O.Val
}
// Return NaN.
func (O *IOpt) Float() float64 {
return math.NaN()
}
// Return NaN.
func (O *IOpt) Complex() complex128 {
return cmplx.NaN()
}
// Return false.
func (O *IOpt) Bool() bool {
return false
}
func (O *IOpt) String() string {
return string(O.Val)
}
func (O *IOpt) Equal(other Option) bool {
switch other.(type) {
case *IOpt:
if !strings.EqualFold(O.OptName, other.Name()) {
return false
}
return O.Val == other.Int()
}
return false
}
// Float valued option.
type FOpt struct {
OptName string
Val float64
}
// Return integer valued option.
func FloatOpt(name string, val float64) *FOpt {
return &FOpt{name, val}
}
func (O *FOpt) Name() string {
return O.OptName
}
// Return zero.
func (O *FOpt) Int() int {
return 0
}
func (O *FOpt) Float() float64 {
return O.Val
}
// Return NaN.
func (O *FOpt) Complex() complex128 {
return cmplx.NaN()
}
// Return false.
func (O *FOpt) Bool() bool {
return false
}
func (O *FOpt) String() string {
return "" //string(O.val)
}
func (O *FOpt) Equal(other Option) bool {
switch other.(type) {
case *FOpt:
if !strings.EqualFold(O.OptName, other.Name()) {
return false
}
return O.Val == other.Float()
}
return false
}
// String valued option.
type SOpt struct {
OptName string
Val string
}
// Return string valued option.
func StringOpt(name string, val string) *SOpt {
return &SOpt{name, val}
}
func (O *SOpt) Name() string {
return O.OptName
}
// Return zero.
func (O *SOpt) Int() int {
return 0
}
func (O *SOpt) Float() float64 {
return math.NaN()
}
// Return NaN.
func (O *SOpt) Complex() complex128 {
return cmplx.NaN()
}
// Return false.
func (O *SOpt) Bool() bool {
return false
}
func (O *SOpt) String() string {
return O.Val
}
func (O *SOpt) Equal(other Option) bool {
switch other.(type) {
case *SOpt:
if !strings.EqualFold(O.OptName, other.Name()) {
return false
}
return O.Val == other.String()
}
return false
}
// Boolean valued option.
type BOpt struct {
OptName string
Val bool
}
// Return bool valued option.
func BoolOpt(name string, val bool) *BOpt {
return &BOpt{name, val}
}
func (O *BOpt) Name() string {
return O.OptName
}
func (O *BOpt) Int() int {
return 0
}
func (O *BOpt) Float() float64 {
return math.NaN()
}
func (O *BOpt) Complex() complex128 {
return cmplx.NaN()
}
func (O *BOpt) Bool() bool {
return O.Val
}
func (O *BOpt) String() string {
return "" //string(O.val)
}
func (O *BOpt) Equal(other Option) bool {
switch other.(type) {
case *BOpt:
if !strings.EqualFold(O.OptName, other.Name()) {
return false
}
return O.Val == other.Bool()
}
return false
}
// Return complex valued option.
func ComplexOpt(name string, val complex128) *COpt {
return &COpt{name, val}
}
// Complex valued option.
type COpt struct {
OptName string
Val complex128
}
func (O *COpt) Name() string {
return O.OptName
}
// Return zero.
func (O *COpt) Int() int {
return 0
}
// Return NaN.
func (O *COpt) Float() float64 {
return math.NaN()
}
// Return NaN.
func (O *COpt) Complex() complex128 {
return O.Val
}
func (O *COpt) Bool() bool {
return false
}
func (O *COpt) String() string {
return "" //string(O.val)
}
func (O *COpt) Equal(other Option) bool {
switch other.(type) {
case *COpt:
if !strings.EqualFold(O.OptName, other.Name()) {
return false
}
return O.Val == other.Complex()
}
return false
}
// Local Variables:
// tab-width: 4
// End: