|
| 1 | +#![allow(dead_code)] //TODO: remove after implementing all hints |
| 2 | + |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use cairo_rs::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; |
| 6 | +use felt::Felt252; |
| 7 | + |
| 8 | +/// Stores the data of a specific dictionary. |
| 9 | +pub struct DictTrackerExecScope { |
| 10 | + /// The data of the dictionary. |
| 11 | + data: HashMap<Felt252, Felt252>, |
| 12 | + /// The index of the dictionary in the dict_infos segment. |
| 13 | + #[allow(dead_code)] |
| 14 | + idx: usize, |
| 15 | +} |
| 16 | + |
| 17 | +/// Helper object to allocate, track and destruct all dictionaries in the run. |
| 18 | +#[derive(Default)] |
| 19 | +pub struct DictManagerExecScope { |
| 20 | + /// Maps between a segment index and the DictTrackerExecScope associated with it. |
| 21 | + trackers: HashMap<isize, DictTrackerExecScope>, |
| 22 | +} |
| 23 | + |
| 24 | +impl DictTrackerExecScope { |
| 25 | + /// Creates a new tracker placed in index `idx` in the dict_infos segment. |
| 26 | + pub fn new(idx: usize) -> Self { |
| 27 | + Self { |
| 28 | + data: HashMap::default(), |
| 29 | + idx, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl DictManagerExecScope { |
| 35 | + pub const DICT_DEFAULT_VALUE: usize = 0; |
| 36 | + |
| 37 | + /// Allocates a new segment for a new dictionary and return the start of the segment. |
| 38 | + pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Relocatable { |
| 39 | + let dict_segment = vm.add_memory_segment(); |
| 40 | + assert!( |
| 41 | + self.trackers |
| 42 | + .insert( |
| 43 | + dict_segment.segment_index, |
| 44 | + DictTrackerExecScope::new(self.trackers.len()) |
| 45 | + ) |
| 46 | + .is_none(), |
| 47 | + "Segment index already in use." |
| 48 | + ); |
| 49 | + dict_segment |
| 50 | + } |
| 51 | + |
| 52 | + /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. |
| 53 | + fn get_dict_tracker(&self, dict_end: Relocatable) -> &DictTrackerExecScope { |
| 54 | + self.trackers |
| 55 | + .get(&dict_end.segment_index) |
| 56 | + .expect("The given value does not point to a known dictionary.") |
| 57 | + } |
| 58 | + |
| 59 | + /// Returns a mut reference for a dict tracker corresponding to a given pointer to a dict |
| 60 | + /// segment. |
| 61 | + fn get_dict_tracker_mut(&mut self, dict_end: Relocatable) -> &mut DictTrackerExecScope { |
| 62 | + self.trackers |
| 63 | + .get_mut(&dict_end.segment_index) |
| 64 | + .expect("The given value does not point to a known dictionary.") |
| 65 | + } |
| 66 | + |
| 67 | + /// Returns the index of the dict tracker corresponding to a given pointer to a dict segment. |
| 68 | + pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> usize { |
| 69 | + self.get_dict_tracker(dict_end).idx |
| 70 | + } |
| 71 | + |
| 72 | + /// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment. |
| 73 | + pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: Felt252) { |
| 74 | + self.get_dict_tracker_mut(dict_end).data.insert(key, value); |
| 75 | + } |
| 76 | + |
| 77 | + /// Gets a value from the dict tracker corresponding to a given pointer to a dict segment. |
| 78 | + /// None if the key does not exist in the tracker data. |
| 79 | + pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option<Felt252> { |
| 80 | + self.get_dict_tracker(dict_end).data.get(key).cloned() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Helper object for the management of dict_squash hints. |
| 85 | +#[derive(Default, Debug)] |
| 86 | +pub struct DictSquashExecScope { |
| 87 | + /// A map from key to the list of indices accessing it, each list in reverse order. |
| 88 | + pub access_indices: HashMap<Felt252, Vec<Felt252>>, |
| 89 | + /// Descending list of keys. |
| 90 | + pub keys: Vec<Felt252>, |
| 91 | +} |
| 92 | + |
| 93 | +impl DictSquashExecScope { |
| 94 | + /// Returns the current key to process. |
| 95 | + pub fn current_key(&self) -> Option<Felt252> { |
| 96 | + self.keys.last().cloned() |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns and removes the current key, and its access indices. Should be called when only the |
| 100 | + /// last key access is in the corresponding indices list. |
| 101 | + pub fn pop_current_key(&mut self) -> Option<Felt252> { |
| 102 | + let key_accesses = self.access_indices.remove(&self.current_key().unwrap()); |
| 103 | + assert!( |
| 104 | + key_accesses.unwrap().len() == 1, |
| 105 | + "Key popped but not all accesses were processed." |
| 106 | + ); |
| 107 | + self.keys.pop() |
| 108 | + } |
| 109 | + |
| 110 | + /// Returns a reference to the access indices list of the current key. |
| 111 | + pub fn current_access_indices(&mut self) -> Option<&mut Vec<Felt252>> { |
| 112 | + let current_key = self.current_key()?; |
| 113 | + self.access_indices.get_mut(¤t_key) |
| 114 | + } |
| 115 | + |
| 116 | + /// Returns a reference to the last index in the current access indices list. |
| 117 | + pub fn current_access_index(&mut self) -> Option<&Felt252> { |
| 118 | + self.current_access_indices()?.last() |
| 119 | + } |
| 120 | + |
| 121 | + /// Returns and removes the current access index. |
| 122 | + pub fn pop_current_access_index(&mut self) -> Option<Felt252> { |
| 123 | + self.current_access_indices()?.pop() |
| 124 | + } |
| 125 | +} |
0 commit comments