amfnengine 0.3.5

Amortization Functions (AmFn) Engine
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! List of parameters.
// Copyright (c) 2021 ShiftLeft Software
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use rust_decimal::prelude::*;
use std::cell::Cell;

use super::ElemParameter;
use crate::ListTrait;

pub struct ListParameter {
    /// The list of parameters.
    list_parameter: Vec<ElemParameter>,

    /// The index of the currently selected parameter element.
    list_index: Cell<usize>,
}

/// List of parameters default implementation.

impl Default for ListParameter {
    /// Create a new symbol element.
    ///
    /// # Return
    ///
    /// * See description.

    fn default() -> Self {
        ListParameter::new()
    }
}

/// List of parameters list implementation.

impl ListTrait for ListParameter {
    /// Clear all parameters from the parameter list.

    fn clear(&mut self) {
        self.list_parameter.clear();
        self.list_index.set(usize::MAX);
    }

    /// Get the count of the parameter list.
    ///
    /// # Return
    ///
    /// * See description.

    fn count(&self) -> usize {
        self.list_parameter.len()
    }

    /// Get the index of the selected parameter (starting from 0).
    ///
    /// # Return
    ///
    /// * See description.

    fn index(&self) -> usize {
        self.list_index.get()
    }

    /// Select a parameter based upon an index value.
    ///
    /// # Arguments
    ///
    /// * `index_param` - Index value of the parameter to select (starting from 0).
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    fn get_element(&self, index_param: usize) -> bool {
        if index_param >= self.list_parameter.len() {
            return false;
        }

        self.set_index(index_param);

        true
    }

    /// Set the list index.
    ///
    /// # Arguments
    ///
    /// * `index_param` - See description.

    fn set_index(&self, index_param: usize) -> bool {
        if index_param >= self.list_parameter.len() {
            return false;
        }

        self.list_index.set(index_param);

        true
    }
}

/// List of parameters implementation.

impl ListParameter {
    /// Create a new parameter list.
    ///
    /// # Arguments
    ///
    /// * `elem_level_param` - Element level
    ///
    /// # Return
    ///
    /// * See description.

    pub fn new() -> ListParameter {
        ListParameter {
            list_parameter: Vec::new(),
            list_index: Cell::new(usize::MAX),
        }
    }

    /// Add a new parameter into the parameter list.
    /// If the name results in a duplicate entry, an
    /// incrementing number starting from 2 is appended to the
    /// name until a non-duplicate entry is found.
    ///
    /// # Arguments
    ///
    /// * `name_param` - Name of the parameter.
    /// * `label_param` - Label of the parameter.
    /// * `desc_param` - Description of the parameter.
    /// * `updating_json_param` - Updating from json.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn add_parameter(
        &mut self,
        name_param: &str,
        label_param: &str,
        desc_param: &str,
        updating_json_param: bool,
    ) -> bool {
        let mut name: String = String::from(name_param);
        let mut update_element: bool = false;

        if self.get_element_by_name(name_param, false) {
            // Check for duplicate name
            if updating_json_param {
                self.get_element_by_name(name_param, true);
                update_element = true;
            } else {
                let mut temp_name;
                let mut name_index: usize = 2;
                loop {
                    temp_name = format!("{}{}", name_param, name_index);
                    if !self.get_element_by_name(temp_name.as_str(), false) {
                        break;
                    }
                    name_index += 1;
                }
                name = temp_name;
            }
        }
        if update_element {
            match self.list_parameter.get_mut(self.list_index.get()) {
                None => {
                    return false;
                }
                Some(o) => {
                    o.set_name(name.as_str());
                    return true;
                }
            }
        }
        let new_elem_param: ElemParameter =
            ElemParameter::new(name.as_str(), label_param, desc_param);

        self.list_parameter.push(new_elem_param);

        match self
            .list_parameter
            .iter()
            .position(|e| e.name() == name.as_str())
        {
            None => false,
            Some(o) => {
                self.list_index.set(o);
                true
            }
        }
    }

    /// Performs a deep copy of this parameter list and returns to new parameter list.
    ///
    /// # Arguments
    ///
    /// * `elem_level_param` - Element level
    /// * `updating_json_param` - Updating from json.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn copy(&self, updating_json_param: bool) -> ListParameter {
        let mut list_parameter = ListParameter::new();

        self.copy_list_parameter(&mut list_parameter, updating_json_param);

        list_parameter
    }

    /// Performs a deep copy of this parameter list into the parameter list parameter.
    ///
    /// # Arguments
    ///
    /// * `list_parameter` - The parameter list to copy into.
    /// * `updating_json_param` - Updating from json.

    pub fn copy_list_parameter(
        &self,
        list_parameter: &mut ListParameter,
        updating_json_param: bool,
    ) {
        for elem in self.list_parameter.iter() {
            if list_parameter.get_element_by_name(elem.name(), false) {
                continue; // Already present
            }

            list_parameter.add_parameter(
                elem.name(),
                elem.label(),
                elem.description(),
                updating_json_param,
            );

            match elem.param_type() {
                crate::TokenType::Integer => {
                    list_parameter.set_integeri(elem.param_integeri());
                }
                crate::TokenType::Decimal => {
                    list_parameter.set_decimal(elem.param_decimal());
                }
                _ => {
                    list_parameter.set_string(elem.param_string());
                }
            }
        }
    }

    /// Tests if this parameter list and another are equal.
    ///
    /// # Arguments
    ///
    /// * `list_parameter` - List to compare.
    ///
    /// # Return
    ///
    /// * True if equals, otherwise false.

    pub fn equal(&self, list_parameter: &ListParameter) -> bool {
        if self.count() != list_parameter.count() {
            return false;
        }

        let mut index: usize = 0;
        while index < self.count() {
            match self.list_parameter.get(index) {
                None => {
                    return false;
                }
                Some(o) => match list_parameter.list_parameter.get(index) {
                    None => {
                        return false;
                    }
                    Some(o2) => {
                        if !o.equal(o2) {
                            return false;
                        }
                    }
                },
            }

            index += 1;
        }

        true
    }

    /// Get the name of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn name(&self) -> &str {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.name(),
        }
    }

    /// Get the label of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn label(&self) -> &str {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.label(),
        }
    }

    /// Get the description of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn description(&self) -> &str {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.description(),
        }
    }

    /// Get the type of the parameter.
    ///
    /// # Return
    ///
    /// * See description.
    ///     

    pub fn param_type(&self) -> crate::TokenType {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.param_type(),
        }
    }

    /// Get the integer value of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn param_integeri(&self) -> i32 {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.param_integeri(),
        }
    }

    /// Get the integer value of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn param_integer(&self) -> usize {
        self.param_integeri() as usize
    }

    /// Get the decimal value of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn param_decimal(&self) -> Decimal {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.param_decimal(),
        }
    }

    /// Get the string value of the parameter.
    ///
    /// # Return
    ///
    /// * See description.

    pub fn param_string(&self) -> &str {
        match self.list_parameter.get(self.list_index.get()) {
            None => {
                panic!("Parameter list index not set");
            }
            Some(o) => o.param_string(),
        }
    }

    /// Select a parameter based upon a name.
    ///
    /// # Arguments
    ///
    /// * `name_param` - The name of the parameter to select.
    /// * `select_param` - If true select element, otherwise restore current element.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn get_element_by_name(&self, name_param: &str, select_param: bool) -> bool {
        for (index, elem) in self.list_parameter.iter().enumerate() {
            if name_param == elem.name() {
                if select_param {
                    self.set_index(index);
                }
                return true;
            }
        }

        false
    }

    /// Move the selected parameter up or down in the parameter list.
    ///
    /// # Arguments
    ///
    /// * `is_up` - Move up, otherwise down.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn move_param(&mut self, is_up: bool) -> bool {
        let name: String = match self.list_parameter.get_mut(self.list_index.get()) {
            None => return false,
            Some(o) => String::from(o.name()),
        };

        if is_up {
            if self.list_index.get() == 0 {
                return false;
            }
            let elem = self.list_parameter.remove(self.list_index.get());
            self.list_parameter.insert(self.list_index.get() - 1, elem);
        } else {
            if self.list_index.get() + 1 >= self.list_parameter.len() {
                return false;
            }
            let elem = self.list_parameter.remove(self.list_index.get());
            self.list_parameter.insert(self.list_index.get() + 1, elem);
        }

        match self.list_parameter.iter().position(|e| e.name() == name) {
            None => false,
            Some(o) => {
                self.list_index.set(o);
                true
            }
        }
    }

    /// Remove the selected parameter from the parameter list.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn remove(&mut self) -> bool {
        if self.list_index.get() >= self.list_parameter.len() {
            return false;
        }

        self.list_parameter.remove(self.list_index.get());
        if self.list_index.get() > 0 {
            self.list_index.set(self.list_index.get() - 1);
        }

        true
    }

    /// Set the name of the parameter.
    /// Duplicate names are not allowed.
    ///
    /// # Arguments
    ///
    /// * `name_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_name(&mut self, name_param: &str) -> bool {
        if self.get_element_by_name(name_param, false) {
            return false;
        }

        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_name(name_param);
                true
            }
        }
    }

    /// Set the label of the parameter.
    ///
    /// # Arguments
    ///
    /// * `name_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_label(&mut self, label_param: &str) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_label(label_param);
                true
            }
        }
    }

    /// Set the description of the parameter.
    ///
    /// # Arguments
    ///
    /// * `name_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_description(&mut self, desc_param: &str) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_description(desc_param);
                true
            }
        }
    }

    /// Set the type of parameter.
    ///
    /// # Arguments
    ///
    /// * `value_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_type(&mut self, value_param: crate::TokenType) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_type(value_param);
                true
            }
        }
    }

    /// Set the integer value.
    ///
    /// # Arguments
    ///
    /// * `value_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_integeri(&mut self, value_param: i32) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_integeri(value_param);
                true
            }
        }
    }

    /// Set the integer value.
    ///
    /// # Arguments
    ///
    /// * `value_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_integer(&mut self, value_param: usize) -> bool {
        self.set_integeri(value_param as i32)
    }

    /// Set the decimal value.
    ///
    /// # Arguments
    ///
    /// * `value_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_decimal(&mut self, value_param: Decimal) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_decimal(value_param);
                true
            }
        }
    }

    /// Set the string value.
    ///
    /// # Arguments
    ///
    /// * `value_param` - See description.
    ///
    /// # Return
    ///
    /// * True if successful, otherwise false.

    pub fn set_string(&mut self, value_param: &str) -> bool {
        match self.list_parameter.get_mut(self.list_index.get()) {
            None => false,
            Some(o) => {
                o.set_string(value_param);
                true
            }
        }
    }
}