Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package concpar22final01
trait Problem1 extends Lib:
class DLLCombinerImplementation extends DLLCombiner:
// Copies every other Integer element of data array, starting from the first (index 0), up to the middle
def task1(data: Array[Int]) = task {
var current = first
var i = 0
while current != null && i < size / 2 do
data(i) = current.value
i += 2
current = current.getNext2
}
// Copies every other Integer element of data array, starting from the second, up to the middle
def task2(data: Array[Int]) = task {
var current = second
var i = 1
while current != null && i < size / 2 do
data(i) = current.value
i += 2
current = current.getNext2
}
// Copies every other Integer element of data array, starting from the second to last, up to the middle
def task3(data: Array[Int]) = task {
var current = secondToLast
var i = size - 2
while current != null && i >= size / 2 do
data(i) = current.value
i -= 2
current = current.getPrevious2
}
// Copies every other Integer element of data array, starting from the last, up to the middle
// This is executed on the current thread.
def task4(data: Array[Int]) =
var current = last
var i = size - 1
while current != null && i >= size / 2 do
data(i) = current.value
i -= 2
current = current.getPrevious2
def result(): Array[Int] =
val data = new Array[Int](size)
val t1 = task1(data)
val t2 = task2(data)
val t3 = task3(data)
task4(data)
t1.join()
t2.join()
t3.join()
data