mutable

Using mutable to allow modification of object in unordered_set

怎甘沉沦 提交于 2021-02-20 02:13:33
问题 Please consider the following code: #include <iostream> #include <unordered_set> struct MyStruct { int x, y; double mutable z; MyStruct(int x, int y) : x{ x }, y{ y }, z{ 0.0 } { } }; struct MyStructHash { inline size_t operator()(MyStruct const &s) const { size_t ret = s.x; ret *= 2654435761U; return ret ^ s.y; } }; struct MyStructEqual { inline bool operator()(MyStruct const &s1, MyStruct const &s2) const { return s1.x == s2.x && s1.y == s2.y; } }; int main() { std::unordered_set<MyStruct,

Is it safe to modify mutable members of objects inside sets?

允我心安 提交于 2021-02-19 00:39:12
问题 I was curious as to whether the following scenario is safe. I have the following class definitions: class ActiveStatusEffect { public: StatusEffect* effect; mutable int ReminaingTurns; ActiveStatusEffect() : ReminaingTurns(0) { } //Other unimportant stuff down here } I then store a group of these inside an std::set as follows: struct ASECmp { bool operator ()(const StatusEffects::ActiveStatusEffect &eff1, const StatusEffects::ActiveStatusEffect &eff2) { return eff1.effect->GetPriority() <

Why does it work when I append a new element to a TUPLE?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-16 20:08:14
问题 Since Tuples are non-mutable data types in Python and Tuple comprehensions aren't a thing then why do List comprehensions with circle brackets instead of square brackets work fine and produce regular Tuples? I thought circular brackets were used to define Tuples not Lists (I know I'm not wrong there). From my understanding I'm appending values to a Tuple that has already been defined and I'm able to update a Tuple and that's not supposed to happen (in Python) as Tuples are non-mutable. I'm

Why does it work when I append a new element to a TUPLE?

本秂侑毒 提交于 2021-02-16 20:08:06
问题 Since Tuples are non-mutable data types in Python and Tuple comprehensions aren't a thing then why do List comprehensions with circle brackets instead of square brackets work fine and produce regular Tuples? I thought circular brackets were used to define Tuples not Lists (I know I'm not wrong there). From my understanding I'm appending values to a Tuple that has already been defined and I'm able to update a Tuple and that's not supposed to happen (in Python) as Tuples are non-mutable. I'm

python: how to change the value of function's input parameter?

笑着哭i 提交于 2021-02-16 12:44:40
问题 I tried to modify the value of a string inside a function, like below: >>> def appendFlag(target, value): ... target += value ... target += " " ... >>> appendFlag <function appendFlag at 0x102933398> >>> appendFlag(m,"ok") >>> m '' Well, seems the "target" is only changed within the function, but how to make the new value viable outside the function? Thanks. 回答1: This is handled in python by returning. def appendFlag(target, value): target += value target += " " return target you can use it

python: how to change the value of function's input parameter?

霸气de小男生 提交于 2021-02-16 12:40:10
问题 I tried to modify the value of a string inside a function, like below: >>> def appendFlag(target, value): ... target += value ... target += " " ... >>> appendFlag <function appendFlag at 0x102933398> >>> appendFlag(m,"ok") >>> m '' Well, seems the "target" is only changed within the function, but how to make the new value viable outside the function? Thanks. 回答1: This is handled in python by returning. def appendFlag(target, value): target += value target += " " return target you can use it

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

▼魔方 西西 提交于 2021-02-08 04:13:42
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

时光总嘲笑我的痴心妄想 提交于 2021-02-08 04:07:50
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

徘徊边缘 提交于 2021-02-08 04:05:27
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

Mutable, random-access array/vector with high performance in haskell

 ̄綄美尐妖づ 提交于 2021-02-08 02:00:40
问题 This a topic on Haskell discussed a lot (e.g. mutable-array-implementation), but I am still not sure what is the best practice for the case requiring frequent modification and random-access of an array/vector. Say a vector of length 1,000,000. Operation on it involves accessing a (small, e.g 1000) subset of it based on input, and modifying the values based on the input. Furthermore, such operation are repeated 2,000,000 times. The task itself can be implemented in pure data structure such as