mutable

Mutable Objects in Collections

二次信任 提交于 2019-12-22 19:59:13
问题 From the docs, Storing mutable objects in collection objects can cause problems. Certain collections can become invalid or even corrupt if objects they contain mutate because, by mutating, these objects can affect the way they are placed in the collection. First, the properties of objects that are keys in hashing collections such as NSDictionary objects or NSSet objects will, if changed, corrupt the collection if the changed properties affect the results of the object’s hash or isEqual:

Caching expensive data in C++ - function-scoped statics vs mutable member variables

做~自己de王妃 提交于 2019-12-22 04:01:23
问题 I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const methods, roughly like this: double AdjustData(double d, int key) const { double factor = LongRunningOperationToFetchFactor(key); return factor * d; } I'd like AdjustData to remain const , but I want to cache out the factor so I only fetch it the first time. At present I'm using a mutable map<int, double> to store the result (the map being from key to factor ), but I

Scala: Contains in mutable and immutable sets

倾然丶 夕夏残阳落幕 提交于 2019-12-21 09:19:18
问题 I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output for equals method, I get a different behavior between mutable and immutable sets for the contains method. Here is the code snippet: class Test(text:String){ override def equals(obj:Any) = obj match { case t: Test => if (t.text == this.text) true

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

…衆ロ難τιáo~ 提交于 2019-12-20 05:28:04
问题 I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equals If ReferenceEquals(Me, other) Then Return True Return AddressId = other.AddressId End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is

Python copy.deepcopy() function not working properly [duplicate]

孤人 提交于 2019-12-20 02:34:27
问题 This question already has answers here : How to copy a python class? (8 answers) Closed 5 years ago . I have been playing with the deepcopy function and the copy function and I get the same issue with both of them. It is like the copy was a reference (or a pointer) instead of a proper copy. I am working with data records (classes) in Python, maybe it could be that.. I show you an example: >>> import copy >>> class player1: ... age = 23 ... score = 1 >>> class player2: ... age = 14 ... score =

Why can I not return a mutable reference to an outer variable from a closure?

风格不统一 提交于 2019-12-19 06:22:21
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

Why can I not return a mutable reference to an outer variable from a closure?

风流意气都作罢 提交于 2019-12-19 06:22:09
问题 I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/main.rs:4:16 | 4 | let f = || &mut y; | ^^^^^^ | note: first, the lifetime cannot outlive the lifetime as defined on the body at 4:13... --> src/main.rs:4:13 | 4 | let f = || &mut y; | ^^^^^^^^^ note: ...so that closure can access

Why is there no mutable TreeMap in Scala?

心已入冬 提交于 2019-12-18 12:05:31
问题 Is it lack of time, some technical problem or is there a reason why it should not exist? 回答1: It's just a missing case that will presumably eventually be filled in. There is no reason not to do it, and in certain cases it would be considerably faster than the immutable tree (since modifications require log(n) object creations with an immutable tree and only 1 with a mutable tree). Edit: and in fact it was filled in in 2.12. Mutable TreeMap. (There is a corresponding Set also.) 回答2: Meanwhile

In Kotlin, how do you modify the contents of a list while iterating

吃可爱长大的小学妹 提交于 2019-12-18 11:01:11
问题 I have a list: val someList = listOf(1, 20, 10, 55, 30, 22, 11, 0, 99) And I want to iterate it while modifying some of the values. I know I can do it with map but that makes a copy of the list. val copyOfList = someList.map { if (it <= 20) it + 20 else it } How do I do this without a copy? Note: this question is intentionally written and answered by the author (Self-Answered Questions), so that the idiomatic answers to commonly asked Kotlin topics are present in SO. Also to clarify some

How can I improve this design that forces me to declare a member function const and declare variables mutable?

£可爱£侵袭症+ 提交于 2019-12-18 08:17:11
问题 For some reason I am iterating over elements of a class in an std::set and would like to slightly modify the keys, knowing that the order will be unchanged. Iterators on std::set are const_iterators because if the key is modified, it might result in a bad order and therefore in set corruption. However I know for sure that my operations won't change the order of my elements in the set. For the moment, here is my solution: class Foo { public: Foo(int a, int b): a_(a),b_(b) {} ~Foo(){} bool