Last active
August 29, 2015 14:20
-
-
Save beoliver/d492a643d3f662f7f06a to your computer and use it in GitHub Desktop.
async "map" over 2d array list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
// next step is to change type from A to B, | |
// map :: (a -> b) -> [a] -> [b] | |
public class Main { | |
public static void main(String[] args) { | |
PartitionedArrayList<Integer> xs = new PartitionedArrayList<Integer>(3); | |
// 3 is passed as a param, this means that no sub array will contain more than 3 Integers | |
xs.insert(1); | |
xs.insert(2); | |
xs.insert(3); | |
xs.insert(4); | |
xs.insert(5); | |
xs.insert(6); | |
xs.insert(7); | |
System.out.println(xs); | |
// [1,2,3,],[4,5,6,],[7,], | |
xs.map(new Proc<Integer>() { | |
@Override | |
public Integer call(Integer x) { | |
return x + 10; | |
}}); | |
System.out.println(xs); | |
// [11,12,13,],[14,15,16,],[17,], | |
xs.map(new Proc<Integer>() { | |
@Override | |
public Integer call(Integer x) { | |
return x * 10; | |
}}); | |
System.out.println(xs); | |
// [110,120,130,],[140,150,160,],[170,], | |
} | |
} | |
class Proc<E> { | |
// just a simple wrapper | |
public E call(E object) { | |
return null; | |
} | |
} | |
class PartitionedArrayList<E> { | |
final int partitionSize; | |
volatile ArrayList<ArrayList<E>> rows = new ArrayList<ArrayList<E>>(); | |
volatile int rowIndex = 0; // how many sub arrays do we have? | |
volatile int columnIndex = 0; // what is our position in the last array | |
public PartitionedArrayList(int partitionSize) { | |
this.partitionSize = partitionSize; | |
rows.add(new ArrayList<E>()); | |
} | |
@Override | |
public String toString() { | |
String str = ""; | |
for (ArrayList<E> row : rows) { | |
str += "["; | |
for (E item : row) { | |
str += item.toString() + ","; | |
} | |
str += "],"; | |
} | |
return str; | |
} | |
public synchronized void insert(E item) { | |
if (columnIndex == partitionSize) { | |
columnIndex = 0 ; | |
rowIndex ++ ; | |
rows.add(new ArrayList<E>()) ; | |
} | |
rows.get(rowIndex).add(item); | |
columnIndex++; | |
} | |
public void map(Proc<E> proc) { | |
ArrayList<Thread> threads = new ArrayList<>(); | |
for (ArrayList<E> row : rows) { | |
Thread t = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
int col_index = 0; | |
for (E item : row) { | |
// System.out.println("proc " + proc + " called by " + this); | |
// System.out.println("about to update " + item + " at position " + col_index); | |
row.set(col_index, proc.call(item)); | |
col_index++; | |
} | |
} | |
}); | |
t.start(); | |
threads.add(t); | |
} | |
for (Thread t : threads) { | |
try { | |
t.join(); | |
} catch (InterruptedException e) { | |
} | |
} | |
} | |
} | |
/* | |
[1,2,3,],[4,5,6,],[7,], | |
proc Main$1@59ca08c called by PartitionedArrayList$1@b5b5e4f | |
proc Main$1@59ca08c called by PartitionedArrayList$1@13646692 | |
about to update 4 | |
about to update 1 | |
proc Main$1@59ca08c called by PartitionedArrayList$1@13646692 | |
proc Main$1@59ca08c called by PartitionedArrayList$1@b5b5e4f | |
proc Main$1@59ca08c called by PartitionedArrayList$1@59815c2e | |
about to update 5 | |
about to update 7 | |
about to update 2 | |
proc Main$1@59ca08c called by PartitionedArrayList$1@13646692 | |
proc Main$1@59ca08c called by PartitionedArrayList$1@b5b5e4f | |
about to update 3 | |
about to update 6 | |
[11,12,13,],[14,15,16,],[17,], | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@60979352 | |
about to update 11 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@20f1cd53 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@60979352 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@6c5bb88c | |
about to update 17 | |
about to update 14 | |
about to update 12 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@20f1cd53 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@60979352 | |
about to update 15 | |
about to update 13 | |
proc Main$2@6e5070e8 called by PartitionedArrayList$1@20f1cd53 | |
about to update 16 | |
[110,120,130,],[140,150,160,],[170,], | |
Process finished with exit code 0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment