-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Automation] Enumerable map enhancements #148
base: feature/automation-networks
Are you sure you want to change the base?
[Automation] Enumerable map enhancements #148
Conversation
} | ||
|
||
/// Remove single Key from the Enumerable Map | ||
public fun remove_value<K : copy+drop, V : store+drop+copy>(map: &mut EnumerableMap<K, V>, key: K) { | ||
public fun remove_value<K: copy+drop, V: store+drop+copy>(map: &mut EnumerableMap<K, V>, key: K) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nizam-supraoracles I think it may be good to return the value being removed.
public inline fun for_each_value<K: copy+drop, V: store+drop+copy>(set: &EnumerableMap<K, V>, f: |V|) { | ||
let i = 0; | ||
let len = length(set); | ||
while (i < len) { | ||
let key = *vector::borrow(&set.list, i); | ||
f(table::borrow(&set.map, key).value); | ||
i = i + 1 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we also need for_each_value_mut
also where a mutable reference of the value
is passed to the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add for_each_keyval
, where copies of key
and value
is provided to function (original data structure must not change).
/// Filter the enumerableMap using the boolean function, removing all elements for which `p(e)` is not true. | ||
public inline fun filter<K: copy+drop, V: store+drop+copy>(set: &EnumerableMap<K, V>, p: |&V|bool): vector<V> { | ||
let result = vector<V>[]; | ||
for_each_value(set, |v| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe for_each_value_ref
would be better here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use that; however, since the next step involves using the vector::push_back
method, we need to handle pointer dereferencing with vector::push_back(&mut result, *v
). Therefore, there doesn't seem to be a specific reason to use for_each_value_ref
in this context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
would it make sense to have filter_map
option as well where it accepts 2 fucctions for filtering and then mapping the value and returns the vector of the mapped values.
this way for example automation_registry_state::get_active_task_ids
can utilize this function.
@nizam-supraoracles , I agree, this is a good suggestion. We can certainly have a |
Resolve https://github.com/Entropy-Foundation/smr-moonshot/issues/1425