deep_causality/types/context_types/node_types_adjustable/adjustable_data/
adjustable.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved.
3use std::ops::*;
4
5use dcl_data_structures::grid_type::ArrayGrid;
6use dcl_data_structures::prelude::PointIndex;
7
8use crate::prelude::{Adjustable, AdjustmentError, UpdateError};
9
10use super::*;
11
12impl<T> Adjustable<T> for AdjustableData<T>
13where
14    T: Default
15        + Add<T, Output = T>
16        + Sub<T, Output = T>
17        + Mul<T, Output = T>
18        + Copy
19        + Clone
20        + Hash
21        + Eq
22        + PartialEq
23        + PartialOrd,
24{
25    fn update<const W: usize, const H: usize, const D: usize, const C: usize>(
26        &mut self,
27        array_grid: &ArrayGrid<T, W, H, D, C>,
28    ) -> Result<(), UpdateError> {
29        // Create a 1D PointIndex
30        let p = PointIndex::new1d(0);
31
32        // get the data at the index position
33        let update_data = array_grid.get(p);
34
35        // Check if the new data are okay to update
36        if update_data == T::default() {
37            return Err(UpdateError("Update failed, new data is ZERO".into()));
38        }
39
40        // Update the internal data
41        self.data = update_data;
42
43        Ok(())
44    }
45
46    fn adjust<const W: usize, const H: usize, const D: usize, const C: usize>(
47        &mut self,
48        array_grid: &ArrayGrid<T, W, H, D, C>,
49    ) -> Result<(), AdjustmentError> {
50        // Create a 1D PointIndex
51        let p = PointIndex::new1d(0);
52
53        // get the data at the index position
54        let new_data = array_grid.get(p);
55
56        // Calculate the data adjustment
57        let adjusted_data = self.data + new_data;
58
59        // Check for errors i.e. div by zero / overflow and return either an error or OK().
60        if adjusted_data < T::default() {
61            return Err(AdjustmentError(
62                "Adjustment failed, result is a negative number".into(),
63            ));
64        }
65
66        // replace the internal data with the adjusted data
67        self.data = adjusted_data;
68
69        Ok(())
70    }
71}