mutable

mutable boost::mutex is it possible to separate lock and wait functions?

…衆ロ難τιáo~ 提交于 2019-12-11 08:37:24
问题 So I have functions like read that can be called at the same time from multiple threads. but also I have a function to write that needs to lock all that read functions. Where to get example of creating such archetecture? I get that we can have: mutable boost::mutex the_read_mutex; mutable boost::mutex the_write_mutex; and: void write() { // make all new readers wait and wait for all other currently running read threads(); } void read() { // do not make all new readers wait, and wait for all

Boost-range not working with C++1y init-capture mutable lambda

百般思念 提交于 2019-12-11 06:55:58
问题 I want to compute the element-wise difference of two vectors using Boost.Range and C++1y lambdas with init-capture . The simpler case of subtracting a fixed (i.e. the first) element of one vector works. However, when I try to compute the "vectorized difference" by increasing the iterator over the second range (and making the lambda mutable ), I get a compiler error. Sample code (note that I didn't use generalized lambdas so that both g++ 4.8 and Clang SVN can parse this code): #include

Why does this function mutate data?

三世轮回 提交于 2019-12-11 06:18:54
问题 function bubbleSort(toSort) { let sort = toSort; let swapped = true; while(swapped) { swapped = false; for(let i = 0; i < sort.length; i++) { if(sort[i-1] > sort[i]) { let temp = sort[i-1]; sort[i-1] = sort[i]; sort[i] = temp; swapped = true; } } } return sort; } let asdf = [1,4,3,2]; let asd = bubbleSort(asdf); console.log(asdf, asd); The output to this code is: [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]. What I would expect: [ 1, 4, 3, 2 ] [ 1, 2, 3, 4 ]. What I'm wondering, is why does this mutate the

Cannot get model based test working

与世无争的帅哥 提交于 2019-12-11 05:54:34
问题 As an exercise I wanted to implement a 2-3 finger tree. That should be the perfect opportunity to try out FsCheck's model-based testing. I decided to try the newer experimental version. So far I only coded one command for the test machine because I already fail at making that work—one the other hand it keeps the post short. The full code is available on GitHub. open CmdQ open Fuchu open FsCheck open FsCheck.Experimental type TestType = uint16 type ModelType = ResizeArray<TestType> type

Mutability of lists in python

霸气de小男生 提交于 2019-12-11 05:34:11
问题 Example one: Changing the value that has been appended to b changes the value in the original list l >>> l = [1 , 2, 3] >>> b = [] >>> b.append(l) >>> b[0].append(4) >>> b [[1, 2, 3, 4]] >>> l [1, 2, 3, 4] Example 2: l1 is appended to ans and then the value of l1 is modified. However, the value in ans remains the same. >>> l1 = [1, 2, 3] >>> ans = [] >>> ans.append(l1) >>> ans [[1, 2, 3]] >>> l1 = [2, 3, 4] >>> ans [[1, 2, 3]] I seem to be missing some fundamental point about the mutability

Returning a mutable reference that is behind an immutable reference, passed to the function

时间秒杀一切 提交于 2019-12-11 03:04:29
问题 How is returning a mutable reference that is behind an immutable reference, passed as an argument to the function, handled? struct Foo { i: i32 } struct Bar<'b> { f: &'b mut Foo } impl<'a: 'b, 'b> Bar<'b> { fn func(&'a self) -> &'b mut Foo { self.f } } fn main() { let mut foo = Foo { i: 1 }; let bar = Bar { f: &mut foo }; bar.func(); } gives the following error: error[E0389]: cannot borrow data mutably in a `&` reference --> src/main.rs:9:14 | 8 | fn func(&'a self) -> &'b mut Foo { | --------

F#: How to Call a function with Argument Byref Int

隐身守侯 提交于 2019-12-11 02:26:52
问题 I have this code: let sumfunc(n: int byref) = let mutable s = 0 while n >= 1 do s <- n + (n-1) n <- n-1 printfn "%i" s sumfunc 6 I get the error: (8,10): error FS0001: This expression was expected to have type 'byref<int>' but here has type 'int' So from that I can tell what the problem is but I just dont know how to solve it. I guess I need to specify the number 6 to be a byref<int> somehow. I just dont know how. My main goal here is to make n or the function argument mutable so I can change

Setting a mutable field in a nested function - deftype - clojure

核能气质少年 提交于 2019-12-11 01:48:51
问题 EDIT: After posting the previous version of my question I discovered that the real problem is with nested functions. If I have a closure within a deftype I can not update any mutable fields from within that closure. E.g. the following works: (deftype Test [^:unsynchronized-mutable x] TestInterface (perform [this o] (set! x o))) but this does not: (deftype Test [^:unsynchronized-mutable x] TestInterface (perform [this o] (fn [] (set! x o)) nil)) ; throws a compiler error about assigning to non

Project Euler 7 Scala Problem

ぃ、小莉子 提交于 2019-12-10 23:03:27
问题 I was trying to solve Project Euler problem number 7 using scala 2.8 First solution implemented by me takes ~8 seconds def problem_7:Int = { var num = 17; var primes = new ArrayBuffer[Int](); primes += 2 primes += 3 primes += 5 primes += 7 primes += 11 primes += 13 while (primes.size < 10001){ if (isPrime(num, primes)) primes += num if (isPrime(num+2, primes)) primes += num+2 num += 6 } return primes.last; } def isPrime(num:Int, primes:ArrayBuffer[Int]):Boolean = { // if n == 2 return false;

Swift mutable set: Duplicate element found

六月ゝ 毕业季﹏ 提交于 2019-12-10 18:32:57
问题 My app uses mutable sets of custom elements. Once I had a crash with error „Duplicate element found in Set. Elements may have been mutated after insertion.“ Searching for an explanation, I found this post, which I don’t fully understand. My impression is that one should not modify an element of a set, since this would modify also the hash value of the set, so that further accesses might fail. My questions: Is it allowed to modify an element of a mutable set, or which modification are allowed,