<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=WillZhao</id>
	<title>CSE231 Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://classes.engineering.wustl.edu/cse231/core/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=WillZhao"/>
	<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Special:Contributions/WillZhao"/>
	<updated>2026-11-27T08:39:14Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=2222</id>
		<title>Floodfill Application</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Floodfill_Application&amp;diff=2222"/>
		<updated>2018-07-27T09:05:52Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: /* Mistakes to Avoid */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Floodfill is an problem that is well-solved recursively.  It allows us an opportunity to think about the similarities and differences between creating a task for each sub-problem and wrapping all of your recursive calls in a single async.  Further, we will harken back to this studio later in the semester when we discuss determinism.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
In this studio, you will be using a parallel and recursive method to create an app that fills in a space with color, much like the paint bucket in [https://www.gimp.org/ GNU Image Manipulation Program].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;hoVbNF8CpBk&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:FloodFillTool.png]]&lt;br /&gt;
[[File:FloodFillShape.png]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
https://en.wikipedia.org/wiki/Flood_fill&lt;br /&gt;
&lt;br /&gt;
==Video==&lt;br /&gt;
[https://wustl.app.box.com/s/d800l5t04y2dthi4q3t4pddph9ms1d8p Demo Video]&lt;br /&gt;
&lt;br /&gt;
=Mistakes to Avoid=&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt;  to compare colors.  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
&lt;br /&gt;
==Sequential Implementation Increased Stack Size Requirement==&lt;br /&gt;
For this project, if you decide to first build a sequential version, you will need to change your VM Arguments slightly. In addition to the normal argument that needs to be pasted in for every assignment, you need to add &amp;lt;code&amp;gt;-Xss4m&amp;lt;/code&amp;gt;.  This will increase the size of the call stack, essentially increasing the maximum depth of your recursion.&lt;br /&gt;
&lt;br /&gt;
=The Core Questions=&lt;br /&gt;
*What are the tasks?&lt;br /&gt;
*What is the data?&lt;br /&gt;
*Is the data mutable?&lt;br /&gt;
*If so, how is it shared?&lt;br /&gt;
&lt;br /&gt;
=Code To Use=&lt;br /&gt;
[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/pixels/MutablePixels.html MutablePixels]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/pixels/MutablePixels.html#isInBounds-int-int- isInBounds(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/pixels/MutablePixels.html#getColor-int-int- getColor(x,y)]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/pixels/MutablePixels.html#setColor-int-int-javafx.scene.paint.Color- setColor(x,y,color)]&lt;br /&gt;
&lt;br /&gt;
[https://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/Color.html Color]&lt;br /&gt;
:[https://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/Color.html#equals-java.lang.Object- equals(other)]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
The class is composed of two methods: floodFill and floodFillKernel.  The entire process in the kernel is triggered from the floodFill method. &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	public static void floodFill(MutablePixels pixels, Color nextColor, int x, int y)&lt;br /&gt;
			throws InterruptedException, ExecutionException {&lt;br /&gt;
		Color prevColor = pixels.getColor(x, y);&lt;br /&gt;
		if (nextColor.equals(prevColor)) {&lt;br /&gt;
			// pass&lt;br /&gt;
		} else {&lt;br /&gt;
			finish(() -&amp;gt; {&lt;br /&gt;
				floodFillKernel(pixels, prevColor, nextColor, x, y);&lt;br /&gt;
			});&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is so that the kernel can run recursively and asynchronously so the floodFill method can wrap the kernel in an all-inclusive finish.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|FloodFiller|floodFillKernel|floodfill.studio}}&lt;br /&gt;
&lt;br /&gt;
The kernel should check if the given pixel is in bounds. If it is, it should check if the given pixel matches the targetColor (the color we wish to change). If it is, it should then replace the color with the replacementColor and asynchronously call the method again to repeat the process for the surrounding pixels. The purpose of the assignment is to understand recursion and how to make it work in parallel.&lt;br /&gt;
&lt;br /&gt;
{{Parallel|floodFillKernel}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Visualization==&lt;br /&gt;
[[File:FloodFillViz.png]]&lt;br /&gt;
{{Viz|FloodFillVizApp|floodfill.viz}}&lt;br /&gt;
===JavaFX Setup===&lt;br /&gt;
In order to run the visualization app you will need to [[Eclipse_Tips#JavaFX_Setup|setup e(fx)clipse]].&lt;br /&gt;
&lt;br /&gt;
==Correctness==&lt;br /&gt;
A test suite exists but is limited and not all that exciting.  Demo to an instructor to get credit for your studio locked in.&lt;br /&gt;
{{TestSuite|FloodFillTestSuite|floodfill}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterator,_Collection,_and_Map&amp;diff=2221</id>
		<title>Iterator, Collection, and Map</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Iterator,_Collection,_and_Map&amp;diff=2221"/>
		<updated>2018-07-27T08:43:35Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: /* LinkedCollection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;credit for this assignment: Ben Choi and Dennis Cosgrove&lt;br /&gt;
&lt;br /&gt;
=Motivation=&lt;br /&gt;
We gain experience with interfaces and classes.  In addition we will deal with generic types.  &lt;br /&gt;
&lt;br /&gt;
We implement our own Iterator, Collection, and Map @NotThreadSafe classes.  Later in the semester we will implement a @ThreadSafe version of a Map.&lt;br /&gt;
&lt;br /&gt;
In the creation of our Map class, we will gain experience with hashing which we will leverage throughout the semester.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
* [[Rundown_on_Everything_Java]]&lt;br /&gt;
* [[Interfaces]]&lt;br /&gt;
&amp;lt;youtube&amp;gt;njTh_OwMljA&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;shs0KM3wKv8&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Java Util Documentation==&lt;br /&gt;
* [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html Iterator]&lt;br /&gt;
* [https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html Collection]&lt;br /&gt;
* [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map]&lt;br /&gt;
==VisuAlgo==&lt;br /&gt;
* [https://visualgo.net/en/list linked list] &lt;br /&gt;
* [https://visualgo.net/en/hashtable hash table]&lt;br /&gt;
==Related Videos==&lt;br /&gt;
&amp;lt;youtube&amp;gt;A-rTJTHq5B4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;PfKnQzbztfo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Mistakes To Avoid=&lt;br /&gt;
{{Warning | Do NOT use &amp;lt;nowiki&amp;gt;==&amp;lt;/nowiki&amp;gt;  Use [https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#equals-java.lang.Object-java.lang.Object- Objects.equals(a,b)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT use % which can return a negative number.  Use [https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int- Math.floorMod(x,y)]. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT parallelize. }}&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT simply remove and add for put on a Map.  Look for the entry and set its value if you find it.  Otherwise add it. }}&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
To be an [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable] object a class must implement all of the abstract methods on Iterable.  There happens to be only 1: &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Iterator&amp;lt;T&amp;gt; iterator()&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you have an instance of Iterable you can then obviously invoke the iterator() method, then loop over the resulting iterator to process the elements in the Iterable:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	private static void printAllViaIterator(Iterable&amp;lt;String&amp;gt; iterable) {&lt;br /&gt;
		Iterator&amp;lt;String&amp;gt; iterator = iterable.iterator();&lt;br /&gt;
		while(iterator.hasNext()) {&lt;br /&gt;
			String element = iterator.next();&lt;br /&gt;
			System.out.println(element);&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a bit of sweet syntactic sugar, any Iterable can be used in a [https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html for-each loop]:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	private static void printAllViaForEach(Iterable&amp;lt;String&amp;gt; iterable) {&lt;br /&gt;
		for (String element : iterable) {&lt;br /&gt;
			System.out.println(element);&lt;br /&gt;
		}&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Any class that implements Iterable must implement the iterator() method.  A Collection isa Iterable.  The interface Collection extends the interface Iterable.  To be an Iterable&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;		List&amp;lt;String&amp;gt; states = new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
		states.add(&amp;quot;Delaware&amp;quot;);&lt;br /&gt;
		states.add(&amp;quot;Idaho&amp;quot;);&lt;br /&gt;
		states.add(&amp;quot;Alaska&amp;quot;);&lt;br /&gt;
		&lt;br /&gt;
		printAllViaIterator(states);&lt;br /&gt;
		printAllViaForEach(states);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{ Tip | It is good practice to declare variables, parameters, and fields to be of the least specific type possible (but no less specific :). This is commonly an interface. }}&lt;br /&gt;
&lt;br /&gt;
{{ Warning | A common mistake we see is students attempting to create an instance of an interface when they need to create an instance of a class (e.g. &amp;lt;nowiki&amp;gt;List&amp;lt;String&amp;gt; states = new LinkedList&amp;lt;&amp;gt;();&amp;lt;/nowiki&amp;gt;). }}&lt;br /&gt;
&lt;br /&gt;
==LinkedCollection==&lt;br /&gt;
The most notable subinterface of Iterable is [https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html Collection].  Collection in turn has often used subiterfaces [https://docs.oracle.com/javase/8/docs/api/java/util/List.html List], [https://docs.oracle.com/javase/8/docs/api/java/util/Set.html Set], and [https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Queue].&lt;br /&gt;
&lt;br /&gt;
The [https://docs.oracle.com/javase/8/docs/api/java/util/package-tree.html class and interface heirarchy doc] has the complete tree for the java.util package.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|LinkedCollection|iterator&amp;lt;br&amp;gt;size&amp;lt;br&amp;gt;add|util.lab.collection}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public Iterator&amp;lt;E&amp;gt; iterator()}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int size()}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public boolean add(E item)}}&lt;br /&gt;
&lt;br /&gt;
We will be creating our own implementation of Collection &amp;lt;code&amp;gt;class LinkedNodesCollection&amp;lt;/code&amp;gt;.  Like many of the Collections you may have used in the past (e.g. LinkedList, HashSet) we will inherit functionality from [https://docs.oracle.com/javase/8/docs/api/java/util/AbstractCollection.html AbstractCollection].  In fact, we will only need to implement three of the public methods: size(), iterator(), and add().  The bulk of AbstractCollection's implementation relies on our correct implementation of iterator().&lt;br /&gt;
&lt;br /&gt;
Correctly implementing size() should be trivial.  Just be sure to increment and decrement size when adding or removing elements respectively.&lt;br /&gt;
&lt;br /&gt;
{{Warning | remove is achieved via the iterator.  Be sure to keep the Collection up to date by invoking decrementSize(). }}&lt;br /&gt;
&lt;br /&gt;
add() will want to use the provided &amp;lt;code&amp;gt;class LinkedNode&amp;lt;E&amp;gt;&amp;lt;/code&amp;gt;.  Simply insert new elements in at the tail of your collection as [https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html#offerLast(E) deque.offerLast(element)] does.&lt;br /&gt;
&lt;br /&gt;
Providing the required iterator() method will in turn require us to build [[#LinkedNodesIterator]].&lt;br /&gt;
&lt;br /&gt;
Note: The “head” is a sentinel node, meaning its sole purpose is to point its next reference to the true first node of the list.&lt;br /&gt;
&lt;br /&gt;
===Empty===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Collection&amp;lt;String&amp;gt; collection = new LinkedCollection&amp;lt;&amp;gt;();&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
would result in this data structure:&lt;br /&gt;
&lt;br /&gt;
[[File:LinkedCollectionEmpty.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
===A===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Collection&amp;lt;String&amp;gt; collection = new LinkedNodesCollection&amp;lt;&amp;gt;();&lt;br /&gt;
collection.add(&amp;quot;A&amp;quot;);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
would result in this data structure:&lt;br /&gt;
&lt;br /&gt;
[[File:LinkedCollectionA.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
===AB===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Collection&amp;lt;String&amp;gt; collection = new LinkedNodesCollection&amp;lt;&amp;gt;();&lt;br /&gt;
collection.add(&amp;quot;A&amp;quot;);&lt;br /&gt;
collection.add(&amp;quot;B&amp;quot;);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
would result in this data structure:&lt;br /&gt;
&lt;br /&gt;
[[File:LinkedCollectionAB.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
===ABC===&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Collection&amp;lt;String&amp;gt; collection = new LinkedNodesCollection&amp;lt;&amp;gt;();&lt;br /&gt;
collection.add(&amp;quot;A&amp;quot;);&lt;br /&gt;
collection.add(&amp;quot;B&amp;quot;);&lt;br /&gt;
collection.add(&amp;quot;C&amp;quot;);&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
would result in this data structure:&lt;br /&gt;
&lt;br /&gt;
[[File:LinkedCollectionABC.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
==LinkedNodesIterator==&lt;br /&gt;
* [https://wustl.box.com/s/039hubq2m836wdwjvrvxvtgpp6hij34v Video Demo: Using Iterable]&lt;br /&gt;
* [https://wustl.box.com/s/73996oeuhgnkg38og0l3amkr1ox0ev3d Video Demo: Implementing ArrayIterator]&lt;br /&gt;
{{CodeToImplement|LinkedNodesIterator|hasNext&amp;lt;br&amp;gt;next&amp;lt;br&amp;gt;remove|util.lab.collection}}&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;code&amp;gt;hasNext()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt;===&lt;br /&gt;
{{Sequential|public boolean hasNext()}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public E next()}}&lt;br /&gt;
&lt;br /&gt;
Iterator should keep track of the previous, current, and (optionally) next node as you march through the LinkedNodesCollection. This means you will need to think about what member variables to create for this class.&lt;br /&gt;
&lt;br /&gt;
{{Warning | The iterator should start at the head (sentinel) node. }}&lt;br /&gt;
&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Return A====&lt;br /&gt;
[[File:LinkedCollectionABC iteratorNextA.svg|800px]]&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Return B====&lt;br /&gt;
[[File:LinkedCollectionABC iteratorNextB.svg|800px]]&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Return C====&lt;br /&gt;
[[File:LinkedCollectionABC iteratorNextC.svg|800px]]&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Throw NoSuchElementException====&lt;br /&gt;
[[File:LinkedCollectionABC iteratorNextException.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
Check out the docs for [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#next-- next()] of when to throw [https://docs.oracle.com/javase/8/docs/api/java/util/NoSuchElementException.html NoSuchElementException] and [https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#remove-- remove()] for when to throw [https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalStateException.html IllegalStateException]. Think of edge cases like when the collection is empty or when the iterator tries to remove the sentinel node.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Iterator&amp;lt;Integer&amp;gt; iterator = collection.iterator();&lt;br /&gt;
if(iterator.hasNext()==false) {&lt;br /&gt;
    iterator.next(); // NOTE: should throw NoSuchElementException&lt;br /&gt;
}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;code&amp;gt;remove()&amp;lt;/code&amp;gt;===&lt;br /&gt;
{{Sequential|public void remove()}}&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Return C (i.e. just returned B)====&lt;br /&gt;
note: this is the same diagram from above.&lt;br /&gt;
&lt;br /&gt;
[[File:LinkedCollectionABC iteratorNextC.svg|800px]]&lt;br /&gt;
====State of the Iterator When &amp;lt;code&amp;gt;next()&amp;lt;/code&amp;gt; Would Return C (i.e. just returned B) Then &amp;lt;code&amp;gt;remove()&amp;lt;/code&amp;gt; Was Invoked====&lt;br /&gt;
[[File:LinkedCollectionABC iteratorRemovedB.svg|800px]]&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Iterator&amp;lt;Integer&amp;gt; iterator = collection.iterator();&lt;br /&gt;
iterator.remove(); // NOTE: should throw IllegalStateException&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;Iterator&amp;lt;Integer&amp;gt; iterator = collection.iterator();&lt;br /&gt;
iterator.next();&lt;br /&gt;
iterator.remove();&lt;br /&gt;
iterator.remove(); // NOTE: should throw IllegalStateException&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==BucketsHashMap==&lt;br /&gt;
You will most likely want to complete your Collection and Iterator implementations before moving on to the Map implementations as they will be built with them.&lt;br /&gt;
&lt;br /&gt;
A Map (also sometimes referred to as a Dictionary or a Hashtable) is an abstract data structure which has the capabilities of associating a value with a key: &amp;lt;code&amp;gt;map.put(key, value)&amp;lt;/code&amp;gt;, looking up the associated value with a key: &amp;lt;code&amp;gt;map.get(key)&amp;lt;/code&amp;gt;, and removing &amp;lt;code&amp;gt;map.remove(key)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The standard Java Collections Framework [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map] provides a number of other nice methods, but put, get, and remove are the core of any Map in any programming language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
https://en.wikipedia.org/wiki/Hash_table#/media/File:Hash_table_5_0_1_1_1_1_1_LL.svg&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
*For the compute method, a BiFunction is simply Java’s '''functional interface''' for something that takes in two input parameters and spits out a result. &amp;lt;code&amp;gt;? super K&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;? super V&amp;lt;/code&amp;gt; indicates a class K or V extends ('''superclass'''), while &amp;lt;code&amp;gt;? extends V&amp;lt;/code&amp;gt; indicates a class that extends V ('''subclass'''). The &amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt; is referred to as a '''wildcard generic'''. Follow the links below for Oracle’s documentation on the topic.&lt;br /&gt;
**[https://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html Wildcard Guidelines]&lt;br /&gt;
**[https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html Wildcards]&lt;br /&gt;
*When writing the compute method, use remappingFunction’s apply method to generate a new value and set it as the new value for the entry if it is not null. The method should return this new value. Otherwise, the method should return null and the entry should be removed from the map.&lt;br /&gt;
*Lambda expressions come in handy when using the compute method, but you will not use lambda expressions to author the method.&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|BucketsHashMap|getBucketIndex&amp;lt;br&amp;gt;getBucketFor&amp;lt;br&amp;gt;size&amp;lt;br&amp;gt;put&amp;lt;br&amp;gt;remove&amp;lt;br&amp;gt;get|util.lab.map}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private int getBucketIndex(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|private Collection&amp;lt;Entry&amp;lt;K, V&amp;gt;&amp;gt; getBucketFor(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int size()}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public V put(K key, V value)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public V remove(Object key)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public V get(Object key)}}&lt;br /&gt;
&lt;br /&gt;
A simple way to provide the correct semantics of a map is to simply keep a single collection of key value pairs.  The standard interface for a key value pair is [https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html Map's Entry] with the critical methods:&lt;br /&gt;
&lt;br /&gt;
* K getKey()&lt;br /&gt;
* V getValue()&lt;br /&gt;
* V setValue(V value)&lt;br /&gt;
&lt;br /&gt;
We have provided an implementation of Entry for you called [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/util/KeyMutableValuePair.html KeyMutableValuePair&amp;lt;K,V&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
 BucketsHashMap will use an entry’s key to place an entry into one of several buckets. &lt;br /&gt;
&lt;br /&gt;
If you have not taken CSE 247, this might not make as much sense. In short, these buckets exist to cut down the runtime of searching for entries within the map, as it is much quicker to search 1/total number of buckets than one large bucket which contains every entry.&lt;br /&gt;
&lt;br /&gt;
[https://en.wikipedia.org/wiki/Hash_table hash table on wikipedia]&lt;br /&gt;
&lt;br /&gt;
[[File:Hash_table_5_0_1_1_1_1_1_LL.svg|frame|[https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg mediawiki hash table image]]]&lt;br /&gt;
&lt;br /&gt;
In this class, you will use the entry’s key’s hashcode to determine which of the buckets it should fall into. In order to calculate the index of the bucket that the entry belongs in, we will make use of the hash function, which in turn uses Math.floorMod to calculate a modulo (remainder or % in Java) based on the number of buckets and the key’s hashcode. We will also make use of the getListFor method, which utilizes the hash method in order to return a '''pointer''' to the correct bucket.&lt;br /&gt;
&lt;br /&gt;
reference material: [https://www.cs.cmu.edu/~adamchik/15-121/lectures/Hashing/hashing.html Concept of Hashing]&lt;br /&gt;
&lt;br /&gt;
{{Warning | Do NOT implement put as a remove followed by an add.  While correct, it is undesirable.  First look for the key and set its value if you find it before resorting to adding a new entry when you cannot find a previous entry with the key. }}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
Throughout the process, refer to the associated tests to check your progress. Feel free to run tests individually depending on which method you are working on or run the test suite in order to run every test in the associated package.&lt;br /&gt;
{{TestSuite|UtilTestSuite|util.lab}}&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
As always, please make sure to cite your work appropriately.&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
&lt;br /&gt;
LinkedNodesIterator subtotal: 25&lt;br /&gt;
* Correct hasNext (5)&lt;br /&gt;
* Correct next (10)&lt;br /&gt;
* Correct remove (10)&lt;br /&gt;
&lt;br /&gt;
LinkedNodesCollection subtotal: 20&lt;br /&gt;
* Correct iterator (5)&lt;br /&gt;
* Correct size (5)&lt;br /&gt;
* Correct add (10)&lt;br /&gt;
&lt;br /&gt;
BucketsHashMap subtotal: 45&lt;br /&gt;
* Correct hash (2.5)&lt;br /&gt;
* Correct getBucketFor (2.5)&lt;br /&gt;
* Correct size (5)&lt;br /&gt;
* Correct put (15)&lt;br /&gt;
* Correct remove (10)&lt;br /&gt;
* Correct get (10)&lt;br /&gt;
&lt;br /&gt;
Whole project:&lt;br /&gt;
* Clarity and efficiency (10)&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Nucleobase_Counting&amp;diff=2220</id>
		<title>Nucleobase Counting</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Nucleobase_Counting&amp;diff=2220"/>
		<updated>2018-07-27T08:14:46Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: /* Threshold */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
* get our feet wet with parallel programming&lt;br /&gt;
* gain experience with the async and finish constructs&lt;br /&gt;
* take different approaches to splitting up the work&lt;br /&gt;
&lt;br /&gt;
We will solve the problem sequentially, then split the work up into 2 tasks, then coarsen the work n-ways, and finally split up the work in a divide-and-conquer recursive style.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&lt;br /&gt;
==Bioinformatics==&lt;br /&gt;
For this assignment, you will be writing sequential and parallel code to count nucleobases in a human X chromosome.&lt;br /&gt;
 &lt;br /&gt;
DNA is made up of four nucleobases: cytosine, guanine, adenine, and thymine. A strand of DNA can thus be represented as a string of letters representing these nucleobases, for example: “ACCGCATAAAGTCC.” However, DNA sequencing is typically not 100% accurate, so some of the nucleobases are not read with high certainty. These bases can be represented as an “N.” A sequence then might look something like “NCCGCATNAAGTCC.” Your goal is to write code that counts the number of occurrences a particular nucleobase or uncertain reads.&lt;br /&gt;
&lt;br /&gt;
We will be using actual data pulled from the US National Library of Medicine, a database maintained by the National Institute of Health. We have already provided you the code that you need to access the chromosome from the database and check your work. You must implement a sequential solution and three parallel solutions to count the given bases in these sequences.&lt;br /&gt;
&lt;br /&gt;
For some more optional background on DNA and nucleotide bases, please check out&lt;br /&gt;
*https://en.wikipedia.org/wiki/DNA&lt;br /&gt;
*https://en.wikipedia.org/wiki/FASTA_format&lt;br /&gt;
&lt;br /&gt;
==Parallel Programming==&lt;br /&gt;
===Parallel Constructs===&lt;br /&gt;
* [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/v5/V5.html#finish-edu.wustl.cse231s.v5.api.CheckedRunnable- finish]&lt;br /&gt;
* [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/v5/V5.html#async-edu.wustl.cse231s.v5.api.CheckedRunnable- async]&lt;br /&gt;
* Feel free to use the [[Reference_Page#Async|course reference page]] if you need a reminder on how to use asyncs and finishes&lt;br /&gt;
&lt;br /&gt;
===Related Videos===&lt;br /&gt;
&amp;lt;youtube&amp;gt;ShcnjoO89ZU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;DjVep9485l4&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;1x-jGwcW4WU&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;0_JrsJrqgDg&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;8oq9CKgIOiE&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;--GfpYtL0lI&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--&lt;br /&gt;
* [https://wustl.app.box.com/s/2w4c9xn5du3sqwmmji7v8w35ntpq1j7z Finish &amp;amp; Async: Lower Upper Split]&lt;br /&gt;
* [https://wustl.app.box.com/s/dx0f8ycz82e3r894rnpk0cjw8tj8jqzu Dealing With Final: Using Array Slots]&lt;br /&gt;
* [https://wustl.app.box.com/s/wix18u57v7x1rdx1d1dgfedf6ibr0n92 Dealing With Final: IntegerRange]&lt;br /&gt;
* [https://wustl.app.box.com/s/p3ooceekguiaygg4hggw76rbpt28kf6o CSE]&lt;br /&gt;
* [https://wustl.app.box.com/s/smckzu78cvg8seocgluuo1afydgd0gmt Finish &amp;amp; Async Coarsening: N-Way Split]&lt;br /&gt;
* [https://wustl.app.box.com/s/wqrauxpsz8xskt897qyukm6rkx9u4zmn Divide and Conquer: Array Sum Example]&lt;br /&gt;
--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=The Core Questions=&lt;br /&gt;
*What are the tasks?&lt;br /&gt;
*What is the data?&lt;br /&gt;
*Is the data mutable?&lt;br /&gt;
*If so, how is it shared?&lt;br /&gt;
&lt;br /&gt;
=Mistakes to Avoid=&lt;br /&gt;
{{Warning | Be sure to remove each NotYetImplementedException as you implement your solutions.}}&lt;br /&gt;
{{Warning | Do NOT copy the data.  We are simply reading the chromosome data, so there is no reason to copy it.}}&lt;br /&gt;
{{Warning | Do NOT share data beyond what is necessary.  Do NOT use static class fields when you could use local variables instead.}}&lt;br /&gt;
&lt;br /&gt;
=Getting Started=&lt;br /&gt;
*[[Initial_Setup]]&lt;br /&gt;
*[[Cloning_Repo]]&lt;br /&gt;
*[[JavaFX_Setup]]&lt;br /&gt;
&lt;br /&gt;
=Code to Implement=&lt;br /&gt;
==Midpoint==&lt;br /&gt;
{{CodeToImplement|MidpointUtils|caclulateMidpoint|midpoint.lab}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static int calculateMidpoint(int a, int b)}}&lt;br /&gt;
&lt;br /&gt;
In this method, you will need to calculate the midpoint between two numbers. This is as simple as finding the average between the two numbers. There is no need to worry about rounding correctly, just drop everything after the decimal point if the midpoint is not automatically an int.&lt;br /&gt;
&lt;br /&gt;
There are at least two correct ways to implement the midpoint:  &lt;br /&gt;
&lt;br /&gt;
: Option A: &amp;lt;math&amp;gt;\frac{min+max}{2}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: Option B: &amp;lt;math&amp;gt;min+\frac{max-min}{2}&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is hard to find 1D references for midpoint.  [http://mathworld.wolfram.com/ Wolfram MathWorld] has a breakdown of [http://mathworld.wolfram.com/Midpoint.html Midpoint in 2D and 3D].  Pick any dimension you like.&lt;br /&gt;
&lt;br /&gt;
==Threshold==&lt;br /&gt;
{{CodeToImplement|GreaterThanThresholdPredicate|constructor&amp;lt;br&amp;gt;test|midpoint.lab}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public GreaterThanThresholdPredicate(int threshold)}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public boolean test(int value)}}&lt;br /&gt;
&lt;br /&gt;
Class GreaterThanThresholdPredicate implements [https://docs.oracle.com/javase/8/docs/api/java/util/function/IntPredicate.html Java IntPredicate interface]. The test method should return a boolean value that indicates whether the input argument is '''greater than''' the threshold provided in the class constructor.&lt;br /&gt;
&lt;br /&gt;
==Count Range==&lt;br /&gt;
{{CodeToImplement|NucleobaseCounting|countRange&amp;lt;br&amp;gt;countSequential&amp;lt;br&amp;gt;countParallelLowerUpperSplit&amp;lt;br&amp;gt;countParallelNWaySplit&amp;lt;br&amp;gt;countParallelDivideAndConquerKernel|count.lab}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static int countRangeSequential(byte[] chromosome, Nucleobase targetNucleobase, int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
This utility method should count the number of times a particular nucleobase occurs in the array between [min, maxExclusive).&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;Use a for loop to iterate from min to maxExclusive.&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sequential Solution==&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public static int countSequential(byte[] chromosome, Nucleobase targetNucleobase)}}&lt;br /&gt;
&lt;br /&gt;
This solution should be achieved with a simple call to one of the utility methods you wrote.&lt;br /&gt;
&amp;lt;spoiler show=&amp;quot;spoiler&amp;quot; hide=&amp;quot;spoiler&amp;quot;&amp;gt;Invoke countRangeSequential from 0 to the chromosome array length.&amp;lt;/spoiler&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Parallel Solutions==&lt;br /&gt;
&lt;br /&gt;
You need to implement three different parallel solutions to this problem. The first will involve splitting the array into two equal halves, then going through each half of the array in parallel. The second will involve splitting the array into n different pieces, then going through each of those pieces of the array in parallel.  The third and final implementation will recursively divide the array until a threshold is reached.&lt;br /&gt;
 &lt;br /&gt;
===Lower/Upper Split===&lt;br /&gt;
{{Parallel|public static int countParallelLowerUpperSplit(byte[] chromosome, Nucleobase targetNucleobase)}}&lt;br /&gt;
&lt;br /&gt;
In order to start with then two equal halves solution, please modify the &amp;lt;code&amp;gt;countParallelUpperLowerSplit&amp;lt;/code&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
===Coarsening N-Way Split===&lt;br /&gt;
{{Parallel|public static int countParallelNWaySplit(byte[] chromosome, Nucleobase targetNucleobase, int numTasks)}}&lt;br /&gt;
&lt;br /&gt;
After you’re finished with that, please modify the &amp;lt;code&amp;gt;countParallelNWaySplit&amp;lt;/code&amp;gt; method for splitting the work up into n tasks solution.&lt;br /&gt;
&lt;br /&gt;
If the chromosome was 1,000,000 long, and you were asked to split it up in 10 tasks, each task should obviously process 100,000.  Things get slightly tricky if the chromosome were 1,000,005 long.&lt;br /&gt;
&lt;br /&gt;
===Divide and Conquer===&lt;br /&gt;
In a pattern that we will use repeatedly in the course, we have a public method that is asked to count an entire chromosome.  This method just calls the kernel method to do all of the work:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;	public static int countParallelDivideAndConquer(byte[] chromosome, Nucleobase targetNucleobase, IntPredicate isParallelRangeLengthPredicate)&lt;br /&gt;
			throws InterruptedException, ExecutionException {&lt;br /&gt;
		return countParallelDivideAndConquerKernel(chromosome, targetNucleobase, isParallelRangeLengthPredicate, 0, chromosome.length);&lt;br /&gt;
	}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Parallel|static int countParallelDivideAndConquerKernel(byte[] chromosome, Nucleobase targetNucleobase, IntPredicate isParallelRangeLengthPredicate,&lt;br /&gt;
			int min, int maxExclusive)}}&lt;br /&gt;
&lt;br /&gt;
The kernel method will recursively divide the work up in parallel until the range drops below the threshold specified by isParallelRangeLengthPredicate.  At that point it will simply use the sequential count range utility method.&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
{{TestSuite|CountTestSuite|count.lab}}&lt;br /&gt;
&lt;br /&gt;
Launch CountTestSuite.java as a JUnit Test to run all of the tests.  CountTestSuite.java is located in src/test/java count.lab package.  You can initiate this via right clicking on CountTestSuite.java and selecting &amp;quot;Run As...&amp;quot; -&amp;gt; &amp;quot;JUnit Test&amp;quot;.&lt;br /&gt;
[[http://www.cse.wustl.edu/~cosgroved/courses/cse231/f17/hw1/run-junit-test-suite.png screenshot]]&lt;br /&gt;
&lt;br /&gt;
==Performance==&lt;br /&gt;
{{Performance|NucleobaseCountTiming|count.lab}}&lt;br /&gt;
After your JUnit tests are passing and you are confident that your solutions are correct, move on to checking out the timing performance of your different solutions with different task counts.&lt;br /&gt;
&lt;br /&gt;
=Rubric=&lt;br /&gt;
&lt;br /&gt;
As always, please make sure to cite your work appropriately.&lt;br /&gt;
&lt;br /&gt;
Total points: 100&lt;br /&gt;
* Correct countRangeSequential (10)&lt;br /&gt;
* Correct countSequential (5)&lt;br /&gt;
* Correct calculateMidpoint (5)&lt;br /&gt;
* Correct countParallelLowerUpperSplit (10)&lt;br /&gt;
* Parallel countParallelLowerUpperSplit (10)&lt;br /&gt;
* Correct countParallelNWaySplit (15)&lt;br /&gt;
* Parallel countParallelNWaySplit (10)&lt;br /&gt;
* Correct GreaterThanThresholdPredicate (5)&lt;br /&gt;
* Correct countParallelDivideAndConquerKernel (10)&lt;br /&gt;
* Parallel countParallelDivideAndConquerKernel (10)&lt;br /&gt;
* Clarity and efficiency (10)&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Reference_Page&amp;diff=2219</id>
		<title>Reference Page</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Reference_Page&amp;diff=2219"/>
		<updated>2018-07-27T03:18:08Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: /* Callable */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is a collection of all the the different tools we use in this course and their syntax.&lt;br /&gt;
&lt;br /&gt;
While this page is here to assist you whenever you forget how to create a lambda or register a phaser, try not to become too reliant on it. If you learn and understand the syntax for the tools we build in this class, it will make the assignments easier and faster to get through. Also, this page will not be available during exams, and while there is always leniency with written code, there must be some demonstration of understanding how to use our parallel constructs!&lt;br /&gt;
&lt;br /&gt;
=Java Basics=&lt;br /&gt;
&lt;br /&gt;
Sometimes the problem isn't with doing things in parallel, but just getting Java to cooperate with what you want it to do. This section should hopefully help you figure out how to use the Java concepts needed to complete the assignments in the class.&lt;br /&gt;
&lt;br /&gt;
This page mainly just covers syntax though. For a better understanding of how some of these tools actually function and why we use them, check out [[Rundown on Everything Java]].&lt;br /&gt;
&lt;br /&gt;
===Object Constructors===&lt;br /&gt;
There will be a lot of classes for students to make/complete throughout the semester. Many times, you will be responsible for filling out the constructor. If given something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;private int value;&lt;br /&gt;
&lt;br /&gt;
public Foo(int parameter){&lt;br /&gt;
	//TO DO&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While all sorts of code and commands may be used in the constructor, before it is over, all of the class's private variables should have been assigned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;public Foo(int parameter){&lt;br /&gt;
	this.value = parameter;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, outside of this class, you can create an object of type Foo by doing the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Foo userMadeObject = new Foo(1);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Ternary Operators===&lt;br /&gt;
A handy and quick way to do things based on a boolean operation. The two blocks of code are equivalent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;if(testCondition){&lt;br /&gt;
	int finalResult = valueIfTrue;&lt;br /&gt;
} else{&lt;br /&gt;
	int finalResult = valueIfFalse;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;int finalResult = (testCondition) ? valueIfTrue : valueIfFalse&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===For-each Loop===&lt;br /&gt;
&lt;br /&gt;
For loops don't always need to go from one integer to the next. In fact, if we have any iterable object (array, list, set, etc.), we can just loop through all the entries available in that object. Use this to make your life much simpler!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;List&amp;lt;T&amp;gt; filledList;&lt;br /&gt;
for(T singleEntry: filledList){&lt;br /&gt;
	doSomething(singleEntry);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Runtime===&lt;br /&gt;
In order to get the number of processors your computer has:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;int numProcessors = Runtime.getRuntime().availableProcessors();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Generics===&lt;br /&gt;
Check out the dedicated [[Generics]] page for a deep dive on how Java's generic types work!&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
Check out the dedicated [[Interfaces]] page for an in-depth discussion on interfaces in Java!&lt;br /&gt;
&lt;br /&gt;
===Lambda===&lt;br /&gt;
Lambdas are very complex things, and they are also incredibly important when it comes to understanding how our code works in this course. If you are looking for more information on what lambdas actually are, '''go check out the [[Lambdas]] wiki page, or the lambdas section in the [[Rundown_on_Everything_Java#Lambdas| Rundown on Everything Java]] page!'''&lt;br /&gt;
&lt;br /&gt;
The syntax for a lambda can be seen as:&lt;br /&gt;
&amp;lt;source&amp;gt;(T parameter) -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the curly braces, you will have access to that parameter variable. Most times you use lambdas though, there are no parameters to pass through, so you can just do&lt;br /&gt;
&amp;lt;source&amp;gt;() -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It's strongly encouraged to check out the [[Eclipse_Tips#Content_Assist_lambda_.28.29-.3E.7B.7D| Eclipse tip on how to use content assist lambdas]].&lt;br /&gt;
&lt;br /&gt;
===Anonymous Classes===&lt;br /&gt;
We use anonymous classes throughout the course as a convenient way to instantiate objects of classes that have methods we need to define. There are two ways to do this, one manually labeling the method we're defining, and one with lambda. These are two similar creations of a Function object (which has the method &amp;quot;apply&amp;quot;) that adds one to whatever number is passed in:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Function&amp;lt;Integer, Integer&amp;gt; increment = new Function&amp;lt;Integer, Integer&amp;gt;{&lt;br /&gt;
	public Integer apply(Integer oldValue){&lt;br /&gt;
		return oldValue + 1;&lt;br /&gt;
	}&lt;br /&gt;
};&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Function&amp;lt;Integer, Integer&amp;gt; increment = (oldValue) -&amp;gt; {&lt;br /&gt;
	return oldValue + 1;&lt;br /&gt;
};&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since the class we are defining only has one method, if we use a lambda, it knows that the lambda refers to that one specific method. Check out the section on [Lambdas#Anonymous_Classes|Anonymous Classes] for more information!&lt;br /&gt;
&lt;br /&gt;
===Runnable===&lt;br /&gt;
A [https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html Java Runnable] is an object with just a &amp;lt;code&amp;gt;.run()&amp;lt;/code&amp;gt; method. As discussed above, that means we can instantiate a Runnable using an anonymous class created by a lambda.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Runnable body = () -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
}&lt;br /&gt;
body.run();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Callable===&lt;br /&gt;
A [https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html Java Callable] is very similar to a Runnable. It is an object with only a &amp;lt;code&amp;gt;.call()&amp;lt;/code&amp;gt; method. A key difference between this and a Runnable is that a Callable has a return value when called.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Callable&amp;lt;V&amp;gt; body = () -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
	return foo;&lt;br /&gt;
}&lt;br /&gt;
V result = body.call();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Java Basic Util Data Structures=&lt;br /&gt;
&lt;br /&gt;
Java's Data Structures are a core part of this class, as we will be constantly working with data and making our own data. In no time at all will you become very familiar with some of the more useful data structures!&lt;br /&gt;
&lt;br /&gt;
Remember that mainly syntax is shown here, with [[Rundown on Everything Java]] being the best place to go for more detailed information on certain topics.&lt;br /&gt;
&lt;br /&gt;
===Lists===&lt;br /&gt;
Many times you will need to make some kind of list to keep information in. The two we recommend using are Linked Lists and Array Lists.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;List&amp;lt;T&amp;gt; listA = new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
List&amp;lt;T&amp;gt; listB = new ArrayList&amp;lt;&amp;gt;();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====List Iterator====&lt;br /&gt;
All lists (and many other objects) have iterators we can use to easily traverse through the list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;List&amp;lt;T&amp;gt; list;&lt;br /&gt;
ListIterator&amp;lt;T&amp;gt; iterator = list.listIterator();&lt;br /&gt;
while(iterator.hasNext){&lt;br /&gt;
	T itemA = iterator.next();&lt;br /&gt;
	doSomething(itemA);&lt;br /&gt;
}&lt;br /&gt;
while(iterator.hasPrevious){&lt;br /&gt;
	T itemB = iterator.previous();&lt;br /&gt;
	doSomething(itemB);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Maps===&lt;br /&gt;
Many times you will need to make some kind of map to store information in. Depending on the race conditions of the assignment, you usually will use Hash Maps or Concurrent Hash Maps.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Map&amp;lt;K,V&amp;gt; map = new HashMap&amp;lt;&amp;gt;();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Compute====&lt;br /&gt;
A very important application of a map is its compute method. This allows us to ''change'' a value in a map rather than having to remove it and add it back. Here is the code for the default implementation of the compute method, taken from the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#compute-K-java.util.function.BiFunction- Map JavaDocs]. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;V oldValue = map.get(key);&lt;br /&gt;
V newValue = remappingFunction.apply(key, oldValue);&lt;br /&gt;
if (oldValue != null ) {&lt;br /&gt;
	if (newValue != null)&lt;br /&gt;
		map.put(key, newValue);&lt;br /&gt;
	else&lt;br /&gt;
		map.remove(key);&lt;br /&gt;
} else {&lt;br /&gt;
	if (newValue != null)&lt;br /&gt;
		map.put(key, newValue);&lt;br /&gt;
	else&lt;br /&gt;
		return null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Most students tend to get tripped up on the use of a BiFunction. This BiFunction is essential giving the map a formula to use in order to get the new value it needs to set in the map. If we wanted to call compute on a key and increase its value by one (assuming the value is an integer), it would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;map.compute(keyName, (key, value) -&amp;gt; {&lt;br /&gt;
	return value + 1;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that the BiFunction is simply given as a lambda with two parameters passed in. Also important is that this lambda has a return value. Whatever is returned is what the new value in the map will become. &lt;br /&gt;
&lt;br /&gt;
Other methods in Map that you may find helpful are &amp;lt;code&amp;gt;computeIfAbsent&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;replace&amp;lt;/code&amp;gt;. Check out the [https://docs.oracle.com/javase/8/docs/api/java/util/Map.html Map JavaDocs] for more detailed information on how those methods work.&lt;br /&gt;
&lt;br /&gt;
===Entries===&lt;br /&gt;
In a lot of assignments, you will need to make entries of different types. Almost always, the &amp;lt;code&amp;gt;KeyValuePair&amp;lt;K,V&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;KeyMutableValuePair&amp;lt;K,V&amp;gt;&amp;lt;/code&amp;gt; will be your best options.&lt;br /&gt;
&amp;lt;source&amp;gt;KeyValuePair&amp;lt;String, int&amp;gt; newEntry = new KeyValuePair&amp;lt;&amp;gt;(&amp;quot;A&amp;quot;, 1);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Looping through entries is also very easy to do!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Map&amp;lt;K,V&amp;gt; map = mapFilledWithEntries();&lt;br /&gt;
for(Entry&amp;lt;K,V&amp;gt; entry: map.entrySet()){&lt;br /&gt;
	K entryKey = entry.getKey();&lt;br /&gt;
	V entryValue = entry.getValue();&lt;br /&gt;
	doSomething(entryKey, entryValue);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=V5/Habanero=&lt;br /&gt;
&lt;br /&gt;
Habanero is [http://pasiphae.cs.rice.edu/ Rice's] collection of parallel tools. When you watch the RiceX videos, it is the Habanero version of things you will be seeing. For this course, we've designed [https://www.cse.wustl.edu/~cosgroved/courses/cse231/current/apidocs/edu/wustl/cse231s/v5/V5.html V5]. It is similar to Habanero in most ways, but has small changes to hopefully make your lives easier when it comes to using the tools in this class. Listed below is the syntax for V5's version, but the differences between the two versions are noted to help clarify.&lt;br /&gt;
&lt;br /&gt;
===Async===&lt;br /&gt;
&lt;br /&gt;
Asyncs are a fundamental building block of task level parallelism. The execution of an async will spawn a new child task, and any code written within the braces of an async will be done in that new task parallel to the parent task. The actual [https://www.cse.wustl.edu/~cosgroved/courses/cse231/summer18/apidocs/edu/wustl/cse231s/v5/V5.html#async-edu.wustl.cse231s.v5.api.CheckedRunnable- async] method is called like this:&lt;br /&gt;
&amp;lt;code&amp;gt;async(Runnable body)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
But as discussed above in the [[#Runnable|Runnable]] section, a Runnable can be defined just using a [[#Lambda|lambda]]. For the runnables in asyncs, there are no parameters passed in, so an async with the lambda is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;async( () -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
});&lt;br /&gt;
doSomethingElse();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, both &amp;lt;code&amp;gt;doSomething()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doSomethingElse()&amp;lt;/code&amp;gt; are being done at the same time as one another.&lt;br /&gt;
&lt;br /&gt;
It is also worth noting that with V5, we can use asyncs ''without'' having a new task spawn. This can be really helpful when building and debugging your programs, as it will give you quick and simple means to switch back and forth between parallel and sequential. To do this, use the following version of the method: &amp;lt;code&amp;gt;async(Boolean isParallelDesired, Runnable body)&amp;lt;/code&amp;gt;. In the following example, switching the first line can switch whether or not the two things are run in parallel or not!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;boolean isParallelDesired = true;&lt;br /&gt;
async(isParallelDesired, () -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
});&lt;br /&gt;
doSomethingElse();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Finish===&lt;br /&gt;
&lt;br /&gt;
Finish blocks will wait until everything within the brackets (no matter the number of tasks) executes and completes before moving on to any of the code after it. This is essential in preventing data races and will be used frequently in the course.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;finish(Runnable body)&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;source&amp;gt;finish( () -&amp;gt; {&lt;br /&gt;
	async( () -&amp;gt; {&lt;br /&gt;
		doSomething();&lt;br /&gt;
	});&lt;br /&gt;
	asynch( () -&amp;gt; {&lt;br /&gt;
		doSomethingElse();&lt;br /&gt;
	});&lt;br /&gt;
});&lt;br /&gt;
doMoreSomethingElses();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;doSomething();&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doSomethingElse();&amp;lt;/code&amp;gt; will run in parallel with each other, but &amp;lt;code&amp;gt;doMoreSomethingElses();&amp;lt;/code&amp;gt; will not begin until the first two things have fully completed. &lt;br /&gt;
&lt;br /&gt;
===Parallel Loops===&lt;br /&gt;
{{Warning| CSE 231s is all exclusive max, all the time.  forall, forasync, and forseq are all exclusive max.}}&lt;br /&gt;
&lt;br /&gt;
====Forall====&lt;br /&gt;
&lt;br /&gt;
A forall loop is a simplified way to do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;finish((), -&amp;gt; {&lt;br /&gt;
	for(int i = minInclusive; i &amp;lt; maxExclusive; i++){&lt;br /&gt;
		final int i_ = i;&lt;br /&gt;
		async( () -&amp;gt; {&lt;br /&gt;
			doSomething(i_);&lt;br /&gt;
		});&lt;br /&gt;
	}&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do that same code with a forall loop, we simply do&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forall(minInclusive, maxExclusive, (i) -&amp;gt; {&lt;br /&gt;
	doSomething(i);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note how this time in the lambda, we are passing a parameter through in the parentheses. This 'i' passed through is the variable that is being incremented. You can also do a forall loop iterating through an iterable object, such as an array or list. That would look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T[] array = new T[n];&lt;br /&gt;
forall(array, (item) -&amp;gt; {&lt;br /&gt;
	doSomething(item);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, &amp;lt;code&amp;gt;item&amp;lt;/code&amp;gt; is something of type T from the array. This loop will iterate through all the indicies in the array and spawn a new task for each one.&lt;br /&gt;
&lt;br /&gt;
Similar to general asyncs, by doing &amp;lt;code&amp;gt;forall(boolean isParallelDesired, int minInclusive, int maxExclusive, Consumer body)&amp;lt;/code&amp;gt;, we can control whether the code runs in parallel or sequentially. Use this to make debugging a lot simpler!&lt;br /&gt;
&lt;br /&gt;
====Forall Chunked====&lt;br /&gt;
&lt;br /&gt;
Sometimes, you don't want to make a task for each any everything in the iteratable object, as the overhead of spawning tasks outweighs the benefits from parallelism. Chunking allows the creation of the tasks to be manually decided (either by the user or the computer). To use chunking, just pass a ChunkedOption before the object/values to iterate over.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forall(chunked(), array, (item) -&amp;gt; {&lt;br /&gt;
	doSomething(item);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also do &amp;lt;code&amp;gt;chunked(100)&amp;lt;/code&amp;gt; to specify the size each chunk should be.&lt;br /&gt;
&lt;br /&gt;
Passing through &amp;lt;code&amp;gt;boolean isParallelDesired&amp;lt;/code&amp;gt; as the first parameter can cause the loop to run in parallel or sequential. &lt;br /&gt;
&lt;br /&gt;
====Forall 2d====&lt;br /&gt;
&lt;br /&gt;
Instead of doing two forall loops, we can achieve the same result using a single forall2d loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forall(1stMinInclusive, 1stMaxExclusive, (i) -&amp;gt; {&lt;br /&gt;
	forall(2ndMinInclusive, 2ndMaxExclusive, (j) -&amp;gt; {&lt;br /&gt;
		array[i][j] = doSomething();&lt;br /&gt;
	});&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Is the exact same thing as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forall2d(1stMinInclusive, 1stMaxExclusive, 2ndMinInclusive, 2ndMaxExclusive, (i, j) -&amp;gt; {&lt;br /&gt;
	array[i][j] = doSomething();&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Chunking can also be done on forall2d loops.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forall2d(chunked(), minA, maxExclusiveA, minB, maxExclusiveB, (i, j) -&amp;gt; {&lt;br /&gt;
	array[i][j] = doSomething();&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Passing through &amp;lt;code&amp;gt;boolean isParallelDesired&amp;lt;/code&amp;gt; as the first parameter can cause the loop to run in parallel or sequential.&lt;br /&gt;
&lt;br /&gt;
====Forasync====&lt;br /&gt;
&lt;br /&gt;
Functions similarly to a forall loop, but does not have the finish block surrounding the for loop. So in order to do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;for(int i = minInclusive; i &amp;lt; maxExclusive; i++){&lt;br /&gt;
	final int _i = i;&lt;br /&gt;
	async( () -&amp;gt; {&lt;br /&gt;
		doSomething(_i);&lt;br /&gt;
	});&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We use the forasync:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forasync(minInclusive, maxExclusive, (i) -&amp;gt; {&lt;br /&gt;
	doSomething(i);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T[] array = new T[n];&lt;br /&gt;
forasync(array, (item) -&amp;gt; {&lt;br /&gt;
	doSomething(item);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This style of loop does have a 2d option (&amp;lt;code&amp;gt;forasync2d&amp;lt;/code&amp;gt;), passing through &amp;lt;code&amp;gt;boolean isParallelDesired&amp;lt;/code&amp;gt; as the first parameter can cause the loop to run in parallel or sequential, and it is possible to use chunking options with it.&lt;br /&gt;
&lt;br /&gt;
====Forseq====&lt;br /&gt;
&lt;br /&gt;
Forseq functions just like a regular for loop (nothing is done in parallel), but it looks similar in structure to a forall loop. Use this when you want to easily switch back and forth between the parallel and sequential versions for testing purposes.&lt;br /&gt;
&lt;br /&gt;
'''With the recent introduction of the &amp;lt;code&amp;gt;boolean isParallelDesired&amp;lt;/code&amp;gt; option, Forseq has become a less useful tool since its purpose can be achieved elsewhere.'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;forseq(minInclusive, maxExclusive, (i) -&amp;gt; {&lt;br /&gt;
	doSomething(i);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;T[] array = new T[n];&lt;br /&gt;
forseq(array, (item) -&amp;gt; {&lt;br /&gt;
	doSomething(item);&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There also is a &amp;lt;code&amp;gt;forseq2d&amp;lt;/code&amp;gt; loop that works just like its parallel counterpart. It is also possible to pass in a chunking option in a forseq, but it does not affect the performance of the code.&lt;br /&gt;
&lt;br /&gt;
===Future===&lt;br /&gt;
&lt;br /&gt;
A future is a parallel task that has a return value. This means that once the work is completed (in another thread), we can call the future and get its returned value. The basic structure of a future is &amp;lt;code&amp;gt;future(Callable&amp;lt;R&amp;gt; body)&amp;lt;/code&amp;gt;. Note that this is similar to an async, but uses a [[#Callable|Callable]] rather than a Runnable. In practice, making a future and storing it so the value can be gotten later looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Future&amp;lt;R&amp;gt; futureWithTypeR = future(() -&amp;gt; {&lt;br /&gt;
	R item = doSomething();&lt;br /&gt;
	return item;&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As shown, &amp;lt;code&amp;gt;future(Callable&amp;lt;R&amp;gt; body)&amp;lt;/code&amp;gt; returns something of type [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html Future&amp;lt;R&amp;gt;], which is a Java X10 construct.&lt;br /&gt;
&lt;br /&gt;
Then, get the value later on by calling &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; on the future.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;R result = futureWithTypeR.get();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Finish Accumulators===&lt;br /&gt;
&lt;br /&gt;
A finish accumulator allows you to do operations (such as counting) within a finish block and make them thread-safe. Then the results can be accessed outside of the finish block. First you have to make the finish accumulator, and then you have to register it with your finish.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;FinishAccumulator&amp;lt;Integer&amp;gt; acc = newIntegerFinishAccumulator(NumberReductionOperator.SUM);&lt;br /&gt;
finish(register(acc), () -&amp;gt; {&lt;br /&gt;
	int value = doSomething();&lt;br /&gt;
	acc.put(value);&lt;br /&gt;
});&lt;br /&gt;
Integer result = acc.get();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some important notes:&lt;br /&gt;
*&amp;lt;code&amp;gt;register()&amp;lt;/code&amp;gt; is a V5 method that was created for this course to make setting up accumulators a bit easier.&lt;br /&gt;
*Some other NumberReductionOperators you can use are &amp;lt;code&amp;gt;PRODUCT&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MIN&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;MAX&amp;lt;/code&amp;gt;.&lt;br /&gt;
*Some other types of accumulators are &amp;lt;code&amp;gt;newDoubleFinishAccumulator()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;newReducerFinishAccumulator&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Java Util Concurrent=&lt;br /&gt;
X10 is Java's built in parallel tools. These are some things you will always be able to use outside of this course. If you have questions on how certain things work, searching for the javadocs for any object or class will normally provide the answers.&lt;br /&gt;
&lt;br /&gt;
Once again, you can find more in-depth explanations of how some of these tools work by checking out these pages:&lt;br /&gt;
*[[Rundown on Everything Java]]&lt;br /&gt;
*[[Generics]]&lt;br /&gt;
*[[Interfaces]]&lt;br /&gt;
&lt;br /&gt;
===Threads===&lt;br /&gt;
&lt;br /&gt;
The most simple way to do something in parallel in Java is to create and run a new thread. The constructor needs to have a &amp;lt;code&amp;gt;run()&amp;lt;/code&amp;gt; method, so we can just use a lambda.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Thread threadToRun = () -&amp;gt; {&lt;br /&gt;
	doSomething();&lt;br /&gt;
}&lt;br /&gt;
threadToRun.start();&lt;br /&gt;
doSomethingElse();&lt;br /&gt;
threadToRun.join();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Executors===&lt;br /&gt;
&lt;br /&gt;
The ExecutorService class has a &amp;lt;code&amp;gt;submit()&amp;lt;/code&amp;gt; function that returns a Future. From there, it isn't to different from our version of Future. In the following example, &amp;lt;code&amp;gt;ExecutorService executor&amp;lt;/code&amp;gt; is passed to us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Future&amp;lt;Integer&amp;gt; future = executor.submit(() -&amp;gt; {&lt;br /&gt;
	int value = doSomething();&lt;br /&gt;
	return value;&lt;br /&gt;
};&lt;br /&gt;
doSomethingElse();&lt;br /&gt;
future.get()&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Phasers===&lt;br /&gt;
&lt;br /&gt;
There are a lot of things you can do with Phasers. Check out the [https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html official Javadocs] for a breakdown of all the options. Here is an example that uses some of the methods we'll call when utilizing phasers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Phaser phaserA = new Phaser();&lt;br /&gt;
Phaser phaserB = new Phaser();&lt;br /&gt;
phaserA.register()&lt;br /&gt;
phaserB.register()&lt;br /&gt;
finish(() -&amp;gt; {&lt;br /&gt;
	async(() -&amp;gt; {&lt;br /&gt;
		for(int i = 0; i &amp;lt; 10; i++){&lt;br /&gt;
			doSomething();&lt;br /&gt;
			phaserA.arrive();&lt;br /&gt;
			PhaserUtils.awaitAdvancedForPhase(phaserB, i);&lt;br /&gt;
		}&lt;br /&gt;
	});&lt;br /&gt;
	&lt;br /&gt;
	for(int j = 0; j &amp;lt; 10; j++){&lt;br /&gt;
		doSomethingElse();&lt;br /&gt;
		phaserB.arrive();&lt;br /&gt;
		PhaserUtils.awaitAdvancedForPhase(phaserA, j);&lt;br /&gt;
	}&lt;br /&gt;
});&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;PhaserUtils.awaitAdvancedForPhase(Phaser phaser, int phaseNumber)&amp;lt;/code&amp;gt; is a tool we have created to make it safer to wait for other phasers. You have have multiple things wait on one phaser by using a bulk register.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Phaser phaser = new Phaser();&lt;br /&gt;
phaser.bulkRegister(3);&lt;br /&gt;
forall(0, 3, (i) -&amp;gt; {&lt;br /&gt;
	doSomething(i);&lt;br /&gt;
	phaser.arriveAndAwaitAdvance();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Synchronized===&lt;br /&gt;
&lt;br /&gt;
Synchronizing on something prevents on any other thread using (and thus modifying) the synchronized object until it has finished executing the code in the block. You can synchronize on as big or small an object you want. The example below, both uses of synchronized will make the class thread safe, but they have different performance implications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;public class Foo{&lt;br /&gt;
	public List&amp;lt;T&amp;gt; list;&lt;br /&gt;
&lt;br /&gt;
	public Foo(){&lt;br /&gt;
		this.list = new LinkedList&amp;lt;&amp;gt;();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void addToList(){&lt;br /&gt;
		synchronized(this){&lt;br /&gt;
			this.list.add(new T());&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public void removeFromList(){&lt;br /&gt;
		synchronized(this.list){&lt;br /&gt;
			this.list.poll();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Locks===&lt;br /&gt;
&lt;br /&gt;
Locks are an alternative to synchronized. With proper use of locks, we can make our methods thread safe. Here's a couple examples that showcase the syntax for locks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Reentrant lock = ...&lt;br /&gt;
lock.lock();&lt;br /&gt;
doSomething();&lt;br /&gt;
lock.unlock();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;Lock lock = ...&lt;br /&gt;
if (lock.tryLock()) {&lt;br /&gt;
	try {&lt;br /&gt;
		doSomething();&lt;br /&gt;
	} finally {&lt;br /&gt;
		lock.unlock();&lt;br /&gt;
	}&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Pack&amp;diff=2060</id>
		<title>Pack</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Pack&amp;diff=2060"/>
		<updated>2018-06-07T17:35:40Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
One of the applications for scan is pack operation. Given an input array, the operation produces an output array containing only the elements that satisfy some specified predicate.&lt;br /&gt;
&lt;br /&gt;
The problem with parallelizing pack is that although it is easy to determine whether an element should be filtered out into the output, we can't know where to put the element in the output array. It seems that placing an element into the output requires knowledge of the placement of the previous elements. This is where prefix sum becomes very useful.&lt;br /&gt;
&lt;br /&gt;
Think about quick sort. In the partition step, we are given a pivot and need to separate the array by the predicate of whether an element is larger than the pivot. This is the perfect place to use the pack operation. You are going to build a more general pack filter in this studio, but you can still attempt the parallel partitioner challenge here: [[Quicksort Parallel Partitioner]].&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&lt;br /&gt;
For example, if you have an integer array input:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|2||6||1||3||7||9||4&lt;br /&gt;
|}&lt;br /&gt;
You want to filter out all elements that are less than five. You can first create a flag array in which all the positions i where input[i] is less than 5 is flagged as &amp;quot;1&amp;quot; and all other positions are marked as &amp;quot;0&amp;quot;.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||0||1||1||0||0||1&lt;br /&gt;
|}&lt;br /&gt;
The prefix sum of this flag array is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||1||2||3||3||3||4&lt;br /&gt;
|}&lt;br /&gt;
Notice how each position that that was flagged now has a distinct number assigned to it in the prefix sum array. We can use this to help us index the output array.&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==Parallel Pack==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelPack|pack|pack.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static &amp;lt;T&amp;gt; T[] pack(Class&amp;lt;T[]&amp;gt; arrayType, T[] arr, Predicate&amp;lt;T&amp;gt; predicate)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|PackTestSuite|pack.studio}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=2018</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=2018"/>
		<updated>2018-05-25T16:08:07Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
==Hillis-Steele Prefix Sum==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==(Optional) Work-efficient Blelloch Scan==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Prefix sum 16.svg]]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html ArraysHolder]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getSrc-- getSrc()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getDst-- getDst()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#nextSrcAndDst-- nextSrcAndDst()]&lt;br /&gt;
&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/PowersOfTwoLessThan.html PowersOfTwoLessThan] implements [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable&amp;lt;Integer&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Scan==&lt;br /&gt;
{{CodeToImplement|SequentialScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==Default Arrays Holder==&lt;br /&gt;
One of the downsides to parallel scan requires memory.  For our scans we add the additional requirement that we will not mutate the incoming array parameter.  We could create log(n) arrays, one for each level but that would be wasteful.  If we create two array buffers and switch which is the source to read from and which is the destination to write from at each power of two level of the scan, we should be good to go.&lt;br /&gt;
&lt;br /&gt;
The table below shows which array will be passed back for each offset if &amp;lt;code&amp;gt;nextSrcAndDst&amp;lt;/code&amp;gt; used appropriately from a parallel scan.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! ||1|||2||4||8||16&lt;br /&gt;
|-&lt;br /&gt;
|'''src:'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst:'''||a||b||a||b||a&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultArraysHolder|getSrc&amp;lt;br&amp;gt;getDst&amp;lt;br&amp;gt;nextSrcAndDst&amp;lt;br&amp;gt;size|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Scan==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Optional) Blelloch Work Efficient Scan==&lt;br /&gt;
{{CodeToImplement|WorkEfficientScan|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|ScanTestSuite|scan.studio}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Pack&amp;diff=2017</id>
		<title>Pack</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Pack&amp;diff=2017"/>
		<updated>2018-05-25T16:07:45Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: Created page with &amp;quot;=Motivation= One of the applications for scan is pack operation. Given an input array, the operation produces an output array containing only the elements that satisfy some sp...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
One of the applications for scan is pack operation. Given an input array, the operation produces an output array containing only the elements that satisfy some specified predicate.&lt;br /&gt;
&lt;br /&gt;
The problem with parallelizing pack is that although it is easy to determine whether an element should be filtered out into the output, we can't know where to put the element in the output array. It seems that placing an element into the output requires knowledge of the placement of the previous elements.  &lt;br /&gt;
&lt;br /&gt;
Think about quick sort. In the partition step, we are given a pivot and need to separate the array by the predicate of whether an element is larger than the pivot. This is the perfect place to use the pack operation. While you will have a chance to build this partitioner at a future point in the class, You are going to build a more general pack filter in this studio&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
&lt;br /&gt;
This is where prefix sum becomes very useful. For example, if you have an integer array input:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|2||6||1||3||7||9||4&lt;br /&gt;
|}&lt;br /&gt;
You want to filter out all elements that are less than five. You can first create a flag array in which all the positions i where input[i] is less than 5 is flagged as &amp;quot;1&amp;quot; and all other positions are marked as &amp;quot;0&amp;quot;.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||0||1||1||0||0||1&lt;br /&gt;
|}&lt;br /&gt;
The prefix sum of this flag array is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||1||2||3||3||3||4&lt;br /&gt;
|}&lt;br /&gt;
Notice how each position that that was flagged now has a distinct number assigned to it in the prefix sum array. We can use this to help us index the output array.&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
&lt;br /&gt;
==Parallel Pack==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelPack|pack|pack.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static &amp;lt;T&amp;gt; T[] pack(Class&amp;lt;T[]&amp;gt; arrayType, T[] arr, Predicate&amp;lt;T&amp;gt; predicate)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
{{TestSuite|PackTestSuite|pack.studio}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=2016</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=2016"/>
		<updated>2018-05-23T21:46:19Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: Added pack&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
==Hillis-Steele Prefix Sum==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
==Pack==&lt;br /&gt;
One of the applications for scan is pack operation. Given an input array, the operation produces an output array containing only the elements that satisfy some specified predicate.&lt;br /&gt;
&lt;br /&gt;
The problem with parallelizing pack is that although it is easy to determine whether an element should be filtered out into the output, we can't know where to put the element in the output array. It seems that placing an element into the output requires knowledge of the placement of the previous elements.  &lt;br /&gt;
&lt;br /&gt;
This is where prefix sum becomes very useful. For example, if you have an integer array input:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|2||6||1||3||7||9||4&lt;br /&gt;
|}&lt;br /&gt;
You want to filter out all elements that are less than five. You can first create a flag array in which all the positions i where input[i] is less than 5 is flagged as &amp;quot;1&amp;quot; and all other positions are marked as &amp;quot;0&amp;quot;.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||0||1||1||0||0||1&lt;br /&gt;
|}&lt;br /&gt;
The prefix sum of this flag array is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|1||1||2||3||3||3||4&lt;br /&gt;
|}&lt;br /&gt;
Notice how each position that that was flagged now has a distinct number assigned to it in the prefix sum array. We can use this to help us index the output array.&lt;br /&gt;
&lt;br /&gt;
==(Optional) Work-efficient Blelloch Scan==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Prefix sum 16.svg]]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html ArraysHolder]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getSrc-- getSrc()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getDst-- getDst()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#nextSrcAndDst-- nextSrcAndDst()]&lt;br /&gt;
&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/PowersOfTwoLessThan.html PowersOfTwoLessThan] implements [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable&amp;lt;Integer&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Scan==&lt;br /&gt;
{{CodeToImplement|SequentialScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==Default Arrays Holder==&lt;br /&gt;
One of the downsides to parallel scan requires memory.  For our scans we add the additional requirement that we will not mutate the incoming array parameter.  We could create log(n) arrays, one for each level but that would be wasteful.  If we create two array buffers and switch which is the source to read from and which is the destination to write from at each power of two level of the scan, we should be good to go.&lt;br /&gt;
&lt;br /&gt;
The table below shows which array will be passed back for each offset if &amp;lt;code&amp;gt;nextSrcAndDst&amp;lt;/code&amp;gt; used appropriately from a parallel scan.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! ||1|||2||4||8||16&lt;br /&gt;
|-&lt;br /&gt;
|'''src:'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst:'''||a||b||a||b||a&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultArraysHolder|getSrc&amp;lt;br&amp;gt;getDst&amp;lt;br&amp;gt;nextSrcAndDst&amp;lt;br&amp;gt;size|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Scan==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==Parallel Pack==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelPack|pack|pack.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public static &amp;lt;T&amp;gt; T[] pack(Class&amp;lt;T[]&amp;gt; arrayType, T[] arr, Predicate&amp;lt;T&amp;gt; predicate)}}&lt;br /&gt;
&lt;br /&gt;
==(Optional) Blelloch Work Efficient Scan==&lt;br /&gt;
{{CodeToImplement|WorkEfficientScan|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|ScanTestSuite|scan.studio}}&lt;br /&gt;
{{TestSuite|PackTestSuite|pack.studio}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
	<entry>
		<id>http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=1995</id>
		<title>Scan</title>
		<link rel="alternate" type="text/html" href="http://classes.engineering.wustl.edu/cse231/core/index.php?title=Scan&amp;diff=1995"/>
		<updated>2018-05-16T21:26:45Z</updated>

		<summary type="html">&lt;p&gt;WillZhao: Added default arrays holder, still need to change code to investigate&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Motivation=&lt;br /&gt;
Scan, also known as parallel prefix, is a fundamental and useful operation in parallel programming.  We will gain experience in building Hillis &amp;amp; Steele scan with an optional work efficient Blellock scan.  &lt;br /&gt;
&lt;br /&gt;
Further, the dependencies in scan:&lt;br /&gt;
&lt;br /&gt;
: &amp;lt;math&amp;gt;y_i = y_{i-1} + x_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make it seem to have little hope for parallelism.  However, simple yet clever approaches can achieve &amp;lt;math&amp;gt;\log n&amp;lt;/math&amp;gt; critical path lengths.&lt;br /&gt;
&lt;br /&gt;
While we will simply implement prefix sum, scan can be used for other associative operations.&lt;br /&gt;
&lt;br /&gt;
=Background=&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum Prefix Sum]&lt;br /&gt;
&lt;br /&gt;
==Required==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_1:_Shorter_span,_more_parallel Hillis and Steele Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;RdfmxfZBHpo&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Hillis-Steele_Prefix_Sum.svg]]&lt;br /&gt;
&lt;br /&gt;
==Optional==&lt;br /&gt;
[https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient Blelloch Algorithm]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;youtube&amp;gt;mmYv3Haj6uc&amp;lt;/youtube&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Prefix sum 16.svg]]&lt;br /&gt;
&lt;br /&gt;
=Code To Investigate=&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html ArraysHolder]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getSrc-- getSrc()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#getDst-- getDst()]&lt;br /&gt;
:[https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/ArraysHolder.html#nextSrcAndDst-- nextSrcAndDst()]&lt;br /&gt;
&lt;br /&gt;
class [https://www.cse.wustl.edu/~cosgroved/courses/cse231/s18/apidocs/scan/core/PowersOfTwoLessThan.html PowersOfTwoLessThan] implements [https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html Iterable&amp;lt;Integer&amp;gt;]&lt;br /&gt;
&lt;br /&gt;
=Code To Implement=&lt;br /&gt;
==Sequential Scan==&lt;br /&gt;
{{CodeToImplement|SequentialScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Sequential|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==Default Arrays Holder==&lt;br /&gt;
One of the downsides to parallel scan requires memory.  For our scans we add the additional requirement that we will not mutate the incoming array parameter.  We could create log(n) arrays, one for each level but that would be wasteful.  If we create two array buffers and switch which is the source to read from and which is the destination to write from at each power of two level of the scan, we should be good to go.&lt;br /&gt;
&lt;br /&gt;
The table below shows which array will be passed back for each offset if &amp;lt;code&amp;gt;nextSrcAndDst&amp;lt;/code&amp;gt; used appropriately from a parallel scan.&lt;br /&gt;
{|class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! ||1|||2||4||8||16&lt;br /&gt;
|-&lt;br /&gt;
|'''src:'''||data||a||b||a||b&lt;br /&gt;
|-&lt;br /&gt;
|'''dst:'''||a||b||a||b||a&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
'''NOTE:''' think about which array is the correct one to return from your scan method given how you write your code.&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|DefaultArraysHolder|getSrc&amp;lt;br&amp;gt;getDst&amp;lt;br&amp;gt;nextSrcAndDst&amp;lt;br&amp;gt;size|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
==Hillis and Steele Parallel Scan==&lt;br /&gt;
&lt;br /&gt;
{{CodeToImplement|ParallelScan|sumScan|scan.studio}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
==(Optional) Blelloch Work Efficient Scan==&lt;br /&gt;
{{CodeToImplement|WorkEfficientScan|sumScan|scan.challenge}}&lt;br /&gt;
&lt;br /&gt;
{{Parallel|public int[] sumScan(int[] data)}}&lt;br /&gt;
&lt;br /&gt;
=Testing Your Solution=&lt;br /&gt;
==Correctness==&lt;br /&gt;
===Required===&lt;br /&gt;
{{TestSuite|ScanTestSuite|scan.studio}}&lt;br /&gt;
===Optional Work Efficient===&lt;br /&gt;
{{TestSuite|WorkEfficientScanTestSuite|scan.challenge}}&lt;/div&gt;</summary>
		<author><name>WillZhao</name></author>
		
	</entry>
</feed>