Guest User

Untitled

a guest
Feb 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.03 KB | None | 0 0
  1. #![allow(dead_code)]
  2. #![allow(unused_imports)]
  3.  
  4. mod util {
  5. /// Trait Newable are all objects that have a new method
  6. pub trait Newable {
  7. fn new() -> Self;
  8. }
  9.  
  10. /// Trait Parseable are all objects that have a parse method (string to object)
  11. pub trait Parseable {
  12. fn parse(_: &str) -> Self;
  13. }
  14.  
  15. /// Trait Encodeable are all objects that have an encode method (object to string)
  16. pub trait Encodeable {
  17. fn encode(&self) -> String;
  18. }
  19.  
  20. /// Trait Equalable are all objects that can be compared to themselves
  21. pub trait Equalable {
  22. fn equals(&self, _: &Self) -> bool;
  23. }
  24.  
  25. /// Trait Hashable are all objects that a hash can be extracted from (a hash is a unique identifier for the object)
  26. pub trait Hashable {
  27. fn hash(&self) -> usize;
  28. }
  29.  
  30. /// `debug_passed` is a macro that takes an input (preferably a string) as the checkpoint name and prints "passed checkpoint <name> in file <file> line <line>"
  31. #[macro_export]
  32. macro_rules! debug_passed {
  33. ($x:expr) => {
  34. #[cfg(debug_assertions)]
  35. {
  36. println!(
  37. "Program execution passed checkpoint <{}>, in file <{}> line <{}>",
  38. $x,
  39. file!(),
  40. line!()
  41. )
  42. }
  43. };
  44. }
  45.  
  46. /// `debug_val` is a macro that prints out a value (for debugging purposes)
  47. #[macro_export]
  48. macro_rules! debug_val {
  49. ($val:expr) => {
  50. #[cfg(debug_assertions)]
  51. {
  52. match $val {
  53. tmp => {
  54. println!(
  55. "<{}> <{}> {} = {:#?}",
  56. file!(),
  57. line!(),
  58. stringify!($val),
  59. &tmp
  60. );
  61. tmp
  62. }
  63. }
  64. }
  65. };
  66. }
  67.  
  68. /// `debug_emit` is a macro that prints to stderr
  69. #[macro_export]
  70. macro_rules! debug_emit {
  71. ($val:expr) => {
  72. #[cfg(debug_assertions)]
  73. {
  74. println!("<{}> <{}>, {}", file!(), line!(), $val)
  75. };
  76. };
  77. }
  78.  
  79. /// `TwoTuple` is a 2 element tuple
  80. #[derive(Clone, Copy, Debug)]
  81. pub struct TwoTuple<DataTypeA: Clone, DataTypeB: Clone> {
  82. a: DataTypeA,
  83. b: DataTypeB,
  84. }
  85.  
  86. impl<DataTypeA: Clone, DataTypeB: Clone> TwoTuple<DataTypeA, DataTypeB> {
  87. pub fn new(input1: DataTypeA, input2: DataTypeB) -> TwoTuple<DataTypeA, DataTypeB> {
  88. TwoTuple {
  89. a: input1,
  90. b: input2,
  91. }
  92. }
  93. pub fn car(&self) -> DataTypeA {
  94. self.a.clone()
  95. }
  96. pub fn cdr(&self) -> DataTypeB {
  97. self.b.clone()
  98. }
  99. }
  100.  
  101. /// `UsizeWrapper` is a wrapper for usize satisfying various traits
  102. #[derive(Clone, Debug, PartialEq)]
  103. pub struct UsizeWrapper {
  104. data: usize,
  105. }
  106.  
  107. /// `StringWrapper` is a wrapper for string satisfying various traits
  108. #[derive(Clone, Debug, PartialEq)]
  109. pub struct StringWrapper {
  110. data: String,
  111. }
  112.  
  113. impl UsizeWrapper {
  114. pub fn from(data_1: usize) -> Self {
  115. UsizeWrapper { data: data_1 }
  116. }
  117. pub fn get_data(&self) -> usize {
  118. self.data
  119. }
  120. }
  121. impl Newable for UsizeWrapper {
  122. fn new() -> Self {
  123. UsizeWrapper { data: 0 }
  124. }
  125. }
  126. impl Parseable for UsizeWrapper {
  127. fn parse(data: &str) -> Self {
  128. UsizeWrapper {
  129. data: data.parse::<usize>().unwrap(),
  130. }
  131. }
  132. }
  133. impl Encodeable for UsizeWrapper {
  134. fn encode(&self) -> String {
  135. self.data.to_string()
  136. }
  137. }
  138.  
  139. impl Newable for StringWrapper {
  140. fn new() -> Self {
  141. StringWrapper {
  142. data: String::new(),
  143. }
  144. }
  145. }
  146. impl Parseable for StringWrapper {
  147. fn parse(data: &str) -> Self {
  148. StringWrapper {
  149. data: data.parse::<String>().unwrap(),
  150. }
  151. }
  152. }
  153. impl Encodeable for StringWrapper {
  154. fn encode(&self) -> String {
  155. self.data.clone()
  156. }
  157. }
  158.  
  159. /// Function `parse_csv_list` parses a comma seperated list
  160. pub fn parse_csv_list<DataType: Parseable + Clone>(input: &str, character: char) -> Vec<DataType> {
  161. let mut to_return: Vec<DataType> = Vec::new();
  162. for elem in input.split(character) {
  163. to_return.push(Parseable::parse(elem));
  164. }
  165. to_return
  166. }
  167. pub fn parse_csv_list_native<DataType: std::str::FromStr>(
  168. input: &str,
  169. character: char,
  170. ) -> Vec<DataType> {
  171. let mut to_return: Vec<DataType> = Vec::new();
  172. for elem in input.split(character) {
  173. to_return.push(if let Ok(a) = elem.parse::<DataType>() {
  174. a
  175. } else {
  176. panic!("Invalid token found");
  177. });
  178. }
  179. to_return
  180. }
  181. /// Function `parse_list` parses a list
  182. pub fn parse_list_multi<DataType: Parseable + Clone>(
  183. input: &str,
  184. vocab: &str,
  185. level: usize,
  186. ) -> Vec<DataType> {
  187. if level >= vocab.len() {
  188. panic!("Out of tokens");
  189. }
  190. let token = {
  191. if let Some(a) = vocab.chars().nth(level) {
  192. a
  193. } else {
  194. panic!("Out of tokens");
  195. }
  196. };
  197. let mut to_return: Vec<DataType> = Vec::new();
  198. for elem in input.split(token) {
  199. to_return.push(Parseable::parse(elem));
  200. }
  201. to_return
  202. }
  203.  
  204. /// Function `parse_table` parses a table and returns the sections
  205. pub fn parse_table_sections(input: &str) -> Vec<String> {
  206. let the_string = input.to_string();
  207. let fst = {
  208. if let Some(a) = the_string.split(';').nth(0) {
  209. a
  210. } else {
  211. panic!("Symbol table is invalid");
  212. }
  213. };
  214. let header_len = {
  215. if let Ok(a) = fst.to_string().parse::<usize>() {
  216. a
  217. } else {
  218. panic!("Header length is not an integer");
  219. }
  220. };
  221. let header_start = fst.chars().count() + 1;
  222. let header_end = header_start + header_len;
  223. let body_start = header_end + 1;
  224. let body_end = the_string.chars().count();
  225. let header = (&the_string[header_start..header_end]).to_string();
  226. let body = (&the_string[body_start..body_end]).to_string();
  227. if the_string.chars().collect::<Vec<char>>()[body_start - 1] != ';' {
  228. panic!("Symbol table is invalid");
  229. }
  230.  
  231. let tokenized_header = {
  232. let mut to_return: Vec<usize> = Vec::new();
  233. let tmp = header.split(',').collect::<Vec<&str>>();
  234. for item in tmp {
  235. if let Ok(a) = item.to_string().parse::<usize>() {
  236. to_return.push(a);
  237. } else {
  238. panic!("Symbol table is invalid");
  239. }
  240. }
  241. to_return
  242. };
  243. let mut to_return: Vec<String> = Vec::new();
  244. let mut old_offset = 0;
  245. for item in tokenized_header {
  246. to_return.push((&body[(old_offset)..(old_offset + item)]).to_string());
  247. old_offset += item;
  248. }
  249. to_return
  250. }
  251.  
  252. pub fn access_flattened_vec<'a, T>(
  253. list: &'a [T],
  254. coords: &[isize],
  255. dimensions: &[usize],
  256. offset: usize,
  257. ) -> &'a T {
  258. eprintln!("Entering function access_flattened_Vec");
  259. if coords.len() != dimensions.len() {
  260. panic!("Dimensions do not match");
  261. }
  262. let mut old_len = list.len();
  263. let sizes = dimensions
  264. .iter()
  265. .map(|&x| {
  266. old_len /= x;
  267. old_len
  268. })
  269. .collect::<Vec<usize>>();
  270. let mut addr: usize = 0;
  271. for j in 0..sizes.len() {
  272. addr += (coords[j] * (sizes[j] as isize)) as usize;
  273. }
  274. addr += offset;
  275. &list[addr]
  276. }
  277. }
  278. use std::marker::PhantomData;
  279. use std::str::FromStr;
  280.  
  281. use std::cell::Cell;
  282. use std::cell::Ref;
  283. use std::cell::RefCell;
  284. use std::cell::RefMut;
  285.  
  286. /// Trait `GridMeta` is the trait that all metadata for a Grid must satisfy
  287. pub trait GridMeta {
  288. fn get_dimensions(&self) -> &[usize];
  289. }
  290.  
  291. #[derive(Clone)]
  292. /// `GridDataType` is the datatype of the data inside of a grid (the struct Grid is just a wrapper around `GridDataType` with a few more functions)
  293. pub struct GridDataType<CellDataType> {
  294. /// Data is just the data of the grid. The data is "flattened" to 1 dimension, no matter how many dimensions the grid is
  295. pub data: Vec<CellDataType>,
  296. /// Dimensions is a vector that stores the dimensions of the data - for example, a 2x2 grid (a 2d grid, with 2 rows and 2 columns) would be [2, 2]. The convention is that the size of x is before the size of y before the size of z etc, so a 3d grid with length (x size) 3, width (y size) 4 and height (z size) 5 would be [3,4,5]
  297. pub dimensions: Vec<usize>,
  298. }
  299.  
  300. #[derive(Clone)]
  301. /// `NeighborType` is the datatype for the neighborhood of a cell
  302. pub struct NeighborType<'a, CellDataType> {
  303. /// Data contains the neighborhood
  304. pub data: Vec<&'a CellDataType>,
  305. /// Dimensions is the dimensions
  306. pub dimensions: &'a [usize],
  307. }
  308.  
  309. /// `TransitionFunc` is the transition function (AKA "rule") of the cellular automata
  310. pub type TransitionFunc<CellDataType, MetaDataType /*: util::Newable*/> =
  311. dyn Fn(&mut CellDataType, &NeighborType<CellDataType>, &MetaDataType) -> ();
  312.  
  313. /// `NeighborFuncParser` is the function that returns the neighborhood of a cell
  314. pub type NeighborFuncParser<CellDataType, MetaDataType /*: util::Newable*/> =
  315. dyn for<'a, 'b> Fn(
  316. &'b Ref<'b, GridDataType<CellDataType>>,
  317. &'a MetaDataType,
  318. usize,
  319. ) -> NeighborType<'b, CellDataType>;
  320. /*
  321. pub type NeighborFuncParser<'a, CellDataType, MetaDataType: util::Newable> =
  322. Fn(&'a GridDataType<CellDataType>, &MetaDataType, usize) -> NeighborType<'a, CellDataType>;
  323. */
  324.  
  325. /// `DynamicExecArgs` are the arguments passed to an `ExecFunc` (see `ExecFunc`) that change based on the call
  326. pub struct DynamicExecArgs<'a, CellDataType> {
  327. low: usize,
  328. high: usize,
  329. to_write: &'a mut [CellDataType],
  330. }
  331.  
  332. pub struct StaticExecArgs<'a, CellDataType, MetaDataType> {
  333. transition: &'a TransitionFunc<CellDataType, MetaDataType>,
  334. neighbor_parser: &'a NeighborFuncParser<CellDataType, MetaDataType>,
  335. to_read: Ref<'a, GridDataType<CellDataType>>,
  336. meta: &'a MetaDataType,
  337. }
  338.  
  339. /// `ExecFunc` is an internal function that does the computation for a "chunk" of data - each chunk is done in parallel.
  340. pub type ExecFunc<CellDataType, MetaDataType /*: util::Newable*/> = Fn(
  341. &StaticExecArgs<'_, CellDataType, MetaDataType>,
  342. &mut DynamicExecArgs<'_, CellDataType>,
  343. ) -> ();
  344.  
  345. /// `RunFunc` is the function that runs a function (of type `ExecFunc`) on a seperate thread (or, if there are no threads, simply runs the function)
  346. pub type RunFunc<CellDataType, MetaDataType /*: util::Newable*/> = Fn(
  347. &ExecFunc<CellDataType, MetaDataType>,
  348. &StaticExecArgs<'_, CellDataType, MetaDataType>,
  349. &mut DynamicExecArgs<'_, CellDataType>,
  350. ) -> ();
  351.  
  352. /// `HaltFunc` is the function that tells the program whether to halt or not (true = halt, false = not halt, obviously)
  353. pub type HaltFunc<CellType> = Fn(&GridDataType<CellType>) -> bool;
  354.  
  355. /// Grid is the datatype for a Grid object, where all of the cells are stored
  356. #[derive(Clone)]
  357. pub struct Grid<
  358. CellDataType: 'static + Clone + util::Newable + util::Parseable + util::Encodeable,
  359. MetaDataType: 'static + Clone + util::Newable + util::Parseable + GridMeta,
  360. > {
  361. /// Direction determines which data (data_a or data_b) will be read from, and which data will be written to (the data being read from is never the data being written to). Direction "flops" each cycle. True = write to (modify) data_a, read from data_b (so data_a is ALWAYS the most recent one), false = other way around.
  362. direction: Cell<bool>,
  363. /// Data_a contains the vector. Depending on direction, it is either read to or written to.
  364. data_a: RefCell<GridDataType<CellDataType>>,
  365. /// Data_a contains the vector. Depending on direction, it is either read to or written to.
  366. data_b: RefCell<GridDataType<CellDataType>>,
  367. /// Metadata is the metadata for the grid (for example, for a grid with a modular cell type (e.g. all integers less than 5), the modulus would be metadata)
  368. meta: MetaDataType,
  369. }
  370.  
  371. impl<
  372. CellDataType: 'static + Clone + util::Newable + util::Parseable + util::Encodeable,
  373. MetaDataType: 'static + Clone + util::Newable + util::Parseable + GridMeta,
  374. > Default for Grid<CellDataType, MetaDataType>
  375. {
  376. fn default() -> Self {
  377. Self::new()
  378. }
  379. }
  380.  
  381. impl<
  382. CellDataType: 'static + Clone + util::Newable + util::Parseable + util::Encodeable,
  383. MetaDataType: 'static + Clone + util::Newable + util::Parseable + GridMeta,
  384. > Grid<CellDataType, MetaDataType>
  385. {
  386. /// Function new creates a blank Grid
  387. pub fn new() -> Grid<CellDataType, MetaDataType> {
  388. let temp_grid_data: GridDataType<CellDataType> = GridDataType {
  389. data: Vec::new(),
  390. dimensions: Vec::new(),
  391. };
  392. Grid {
  393. data_a: RefCell::new(temp_grid_data.clone()),
  394. data_b: RefCell::new(temp_grid_data.clone()),
  395. direction: Cell::new(true),
  396. meta: util::Newable::new(),
  397. }
  398. }
  399. /// Function get_data gets the most recent data from the struct
  400. pub fn get_data(&self) -> Ref<GridDataType<CellDataType>> {
  401. let tmp = {
  402. if self.direction.get() {
  403. &self.data_a
  404. } else {
  405. &self.data_b
  406. }
  407. };
  408. if let Ok(a) = tmp.try_borrow() {
  409. a
  410. } else {
  411. panic!("Cannot borrow data");
  412. }
  413. }
  414. /// Function get_meta gets the MetaData
  415. pub fn get_meta(&self) -> &MetaDataType {
  416. &self.meta
  417. }
  418. /// Function read_from_string reads a Grid from a string - mutating the object
  419. pub fn read_from_str(&mut self, data: &str) {
  420. *self = Self::decode(data);
  421. }
  422. /// Function decode decodes the data into a Grid type.
  423. pub fn decode(input: &str) -> Grid<CellDataType, MetaDataType> {
  424. let split: Vec<String> = util::parse_table_sections(input); // Tokenize by semicolons, which seperates the data from the "metadata" - the "metadata" are things like the dimensions, and the "data" is the actual cell state
  425. if split.len() != 2 {
  426. // as of now, there are 2 "big tokens" (seperated by semicolons) - the first is the metadata (information about the rules, for example) and the second is the data.
  427. println!("ERROR - malformed input file.");
  428. panic!("ERROR - malformed input file.");
  429. }
  430. let meta_data_str: &str = &split[0];
  431. let meta_data: MetaDataType = util::Parseable::parse(meta_data_str);
  432.  
  433. let dimensions = meta_data.get_dimensions();
  434.  
  435. //let split_data: Vec<&str> = split[1].split(',').collect(); // Same as previous, but this time for the actual data, not the dimensions
  436. let data: Vec<CellDataType> = util::parse_csv_list(&split[1], ',');
  437. //let mut data: Vec<CellDataType> = Vec::new();
  438. //for j in split_data {
  439. // data.push(util::Parseable::parse(&String::from(j)[..]));
  440. //}
  441. let generated_data: GridDataType<CellDataType> = GridDataType {
  442. data,
  443. dimensions: dimensions.to_vec(),
  444. };
  445. Grid {
  446. data_a: RefCell::new(generated_data.clone()),
  447. data_b: RefCell::new(generated_data.clone()),
  448. direction: Cell::new(true),
  449. meta: meta_data,
  450. }
  451. }
  452. /// Function encode encodes the Grid in a string. INTERNAL
  453. pub fn encode(&self) -> String {
  454. let mut to_return: String = String::new();
  455. let active_data: Ref<GridDataType<CellDataType>> = {
  456. if self.direction.get() {
  457. if let Ok(a) = self.data_a.try_borrow() {
  458. a
  459. } else {
  460. panic!("Cannot borrow active data (currently data_a) immutably");
  461. }
  462. } else if let Ok(a) = self.data_b.try_borrow() {
  463. a
  464. } else {
  465. panic!("Cannot borrow active data (currently data_b) immutably");
  466. }
  467. };
  468. for j in &active_data.dimensions {
  469. to_return += &j.to_string();
  470. to_return += ",";
  471. }
  472. to_return += ";";
  473. for j in &active_data.data {
  474. to_return += &util::Encodeable::encode(j)[..];
  475. to_return += ",";
  476. }
  477. to_return
  478. }
  479. /// compute_internal is an internal computation function
  480. #[inline]
  481. fn compute_internal(
  482. static_args: &StaticExecArgs<'_, CellDataType, MetaDataType>,
  483. dynamic_args: &mut DynamicExecArgs<'_, CellDataType>,
  484. ) {
  485. for j in (dynamic_args.low)..(dynamic_args.high) {
  486. (static_args.transition)(
  487. &mut dynamic_args.to_write[j - dynamic_args.low],
  488. &(static_args.neighbor_parser)(&static_args.to_read, static_args.meta, j),
  489. static_args.meta,
  490. );
  491. }
  492. }
  493. #[inline(always)]
  494. fn mainloop_internal<'c>(
  495. addr_start: usize,
  496. addr_end: usize,
  497. static_args: &StaticExecArgs<'_, CellDataType, MetaDataType>,
  498. run: &RunFunc<CellDataType, MetaDataType>,
  499. to_modify_data: &'c mut [CellDataType],
  500. ) -> &'c mut [CellDataType] {
  501. let (head, tail): (&'c mut [CellDataType], &'c mut [CellDataType]) =
  502. //to_modify.data.split_at_mut(chunk_size * (j + 1));
  503. to_modify_data.split_at_mut(addr_end - addr_start);
  504. run(
  505. &Self::compute_internal,
  506. &static_args,
  507. &mut DynamicExecArgs::new(addr_start, addr_end, head), //&mut DynamicExecArgs::new(j * chunk_size, (j+1) * chunk_size, to_modify.data.split_at_mut(chunk_size * (j+1)).1)
  508. );
  509. tail
  510. }
  511. /// Function apply performs one iteration
  512. #[inline]
  513. pub fn apply(
  514. &self,
  515. transition: &TransitionFunc<CellDataType, MetaDataType>,
  516. neighbor_parser: &NeighborFuncParser<CellDataType, MetaDataType>,
  517. run: &RunFunc<CellDataType, MetaDataType>,
  518. distribute_count: usize,
  519. meta: &MetaDataType,
  520. )
  521. {
  522. let (mut to_modify, to_read): (
  523. RefMut<GridDataType<CellDataType>>,
  524. Ref<GridDataType<CellDataType>>,
  525. ) = {
  526. if self.direction.get() {
  527. let borrowed_mut = {
  528. if let Ok(a) = self.data_a.try_borrow_mut() {
  529. a
  530. } else {
  531. panic!("Cannot borrow to_write");
  532. }
  533. };
  534. let borrowed = {
  535. if let Ok(a) = self.data_b.try_borrow() {
  536. a
  537. } else {
  538. panic!("Cannot borrow to_read");
  539. }
  540. };
  541. (borrowed_mut, borrowed)
  542. } else {
  543. let borrowed_mut = {
  544. if let Ok(a) = self.data_b.try_borrow_mut() {
  545. a
  546. } else {
  547. panic!("Cannot borrow to_write");
  548. }
  549. };
  550. let borrowed = {
  551. if let Ok(a) = self.data_a.try_borrow() {
  552. a
  553. } else {
  554. panic!("Cannot borrow to_read");
  555. }
  556. };
  557. (borrowed_mut, borrowed)
  558. }
  559. };
  560. let mut to_modify_data = to_modify.get_data_mut_slice();
  561. let data_len = to_read.get_data().len();
  562. let static_args = StaticExecArgs::new(transition, neighbor_parser, to_read, meta);
  563. let chunk_size: usize = (data_len) / distribute_count;
  564. let mut addr = 0;
  565. for j in 0..distribute_count {
  566. addr += chunk_size;
  567. /*let (head, tail): (&mut [CellDataType], &mut [CellDataType]) =
  568. to_modify_data.split_at_mut(chunk_size);
  569. run(
  570. &Self::compute_internal,
  571. &static_args,
  572. &mut DynamicExecArgs::new(j * chunk_size, (j + 1) * chunk_size, head), //&mut DynamicExecArgs::new(j * chunk_size, (j+1) * chunk_size, to_modify.data.split_at_mut(chunk_size * (j+1)).1)
  573. );*/
  574. to_modify_data = Self::mainloop_internal(
  575. j * chunk_size,
  576. (j + 1) * chunk_size,
  577. &static_args,
  578. run,
  579. &mut to_modify_data,
  580. );
  581. }
  582. if addr != data_len {
  583. let diff = data_len - addr;
  584. /*let (head, _tail): (&mut [CellDataType], &mut [CellDataType]) =
  585. to_modify_data.split_at_mut(chunk_size);
  586. run(
  587. &Self::compute_internal,
  588. &static_args,
  589. &mut DynamicExecArgs::new(data_len - diff, data_len, head)
  590. );*/
  591. Self::mainloop_internal(diff, data_len, &static_args, run, &mut to_modify_data);
  592. }
  593. self.direction.set(!self.direction.get());
  594. }
  595. }
  596.  
  597. /// `ParseGridError` is a unit struct that signals a grid parsing error
  598. pub struct ParseGridError {}
  599.  
  600. impl<
  601. CellDataType: 'static + Clone + util::Newable + util::Parseable + util::Encodeable,
  602. MetaDataType: 'static + Clone + util::Newable + util::Parseable + GridMeta,
  603. > FromStr for Grid<CellDataType, MetaDataType>
  604. {
  605. type Err = ParseGridError;
  606. /// Function from_str parses a Grid from a string. Does not mutate the object.
  607. fn from_str(data: &str) -> Result<Grid<CellDataType, MetaDataType>, Self::Err> {
  608. let mut temp = Grid::new();
  609. temp.read_from_str(data);
  610. Ok(temp)
  611. }
  612. }
  613.  
  614. impl<CellDataType: Clone> GridDataType<CellDataType> {
  615. pub fn new(dimensions: &[usize]) -> GridDataType<CellDataType> {
  616. GridDataType {
  617. data: Vec::new(),
  618. dimensions: dimensions.to_vec(),
  619. }
  620. }
  621. pub fn from_data(data: &[CellDataType], dimensions: &[usize]) -> GridDataType<CellDataType> {
  622. GridDataType {
  623. data: data.to_vec(),
  624. dimensions: dimensions.to_vec(),
  625. }
  626. }
  627. pub fn get_data_ref(&self) -> &Vec<CellDataType> {
  628. &self.data
  629. }
  630. pub fn get_data_mut_slice(&mut self) -> &mut [CellDataType] {
  631. self.data.as_mut_slice()
  632. }
  633. pub fn get_dimensions_ref(&self) -> &Vec<usize> {
  634. &self.dimensions
  635. }
  636. pub fn get_data(&self) -> Vec<CellDataType> {
  637. self.data.clone()
  638. }
  639. pub fn get_dimensions(&self) -> Vec<usize> {
  640. self.dimensions.clone()
  641. }
  642. }
  643.  
  644. impl<'a, CellDataType: Clone> NeighborType<'a, CellDataType> {
  645. pub fn new(
  646. data_1: Vec<&'a CellDataType>,
  647. dimensions_1: &'a [usize],
  648. ) -> NeighborType<'a, CellDataType> {
  649. NeighborType {
  650. data: data_1,
  651. dimensions: &dimensions_1,
  652. }
  653. }
  654. pub fn get_data(&self) -> Vec<&'a CellDataType> {
  655. self.data.clone()
  656. }
  657. }
  658.  
  659. impl<'a, CellDataType, MetaDataType: util::Newable + util::Parseable + GridMeta>
  660. StaticExecArgs<'a, CellDataType, MetaDataType>
  661. {
  662. fn new(
  663. transition_1: &'a TransitionFunc<CellDataType, MetaDataType>,
  664. neighbor_parser_1: &'a NeighborFuncParser<CellDataType, MetaDataType>,
  665. to_read_1: Ref<'a, GridDataType<CellDataType>>,
  666. meta_1: &'a MetaDataType,
  667. ) -> StaticExecArgs<'a, CellDataType, MetaDataType> {
  668. StaticExecArgs {
  669. transition: transition_1,
  670. neighbor_parser: neighbor_parser_1,
  671. to_read: to_read_1,
  672. meta: meta_1,
  673. }
  674. }
  675. }
  676.  
  677. impl<'a, CellDataType> DynamicExecArgs<'a, CellDataType> {
  678. fn new(
  679. low_1: usize,
  680. high_1: usize,
  681. to_write_1: &'a mut [CellDataType],
  682. ) -> DynamicExecArgs<'a, CellDataType> {
  683. DynamicExecArgs {
  684. low: low_1,
  685. high: high_1,
  686. to_write: to_write_1,
  687. }
  688. }
  689. }
Add Comment
Please, Sign In to add comment