Commit 850b651e authored by Louis Maliyam's avatar Louis Maliyam
Browse files

Add project files

parent 2547d8d5
# IntelliJ project files
.idea/
# macOS folder attributes
.DS_Store/
## https://raw.githubusercontent.com/github/gitignore/master/Java.gitignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.washington.cs.cse332</groupId>
<artifactId>para-practice</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
* countStrs returns the number of elements in arr that equal str.
* For example, if arr is ["h", "ee", "llll", "llll", "oo", "llll"],
* then countStrs(arr, "llll") == 3 and countStrs(arr, "h") == 1.
*
* Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
*/
public class CountStrs {
private static final ForkJoinPool POOL = new ForkJoinPool();
private static final int CUTOFF = 1;
public static int sequential(String[] arr, String str, int lo, int hi) {
// TODO: your code here
return 0; // TODO: you may want to change this
}
public static int parallel(String[] arr, String str) {
return 0; // TODO: you may want to change this
}
private static class CountStrsTask extends RecursiveTask<Integer> {
String[] arr;
String str;
int lo, hi;
public CountStrsTask(String[] arr, int lo, int hi, String str) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
this.str = str;
}
@Override
protected Integer compute() {
// TODO: your code here
return 0; // TODO: you may want to change this
}
}
public static void main(String[] args) {
String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
System.out.println(parallel(arr, "llll") + " should be 3.");
System.out.println(parallel(arr, "h") + " should be 1.");
}
}
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
* lessThan7 returns the number of elements in arr that are less than 7.
* For example, if arr is [21, 7, 6, 8, 17, 1], then lessThan7(arr) == 2.
*
* Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
*/
public class LessThan7 {
private static final ForkJoinPool POOL = new ForkJoinPool();
private static final int CUTOFF = 1;
public static int sequential(int[] arr, int lo, int hi) {
// TODO: your code here
return 0; // TODO: you may want to change this
}
public static int parallel(int[] arr) {
return POOL.invoke(new LessThan7Task(arr, 0, arr.length));
}
private static class LessThan7Task extends RecursiveTask<Integer> {
int[] arr;
int lo, hi;
public LessThan7Task(int[] arr, int lo, int hi) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
}
@Override
protected Integer compute() {
// TODO: your code here
return 0; // TODO: you may want to change this
}
}
public static void main(String[] args) {
System.out.println(parallel(new int[]{21, 7, 6, 8, 17, 1, 7, 7, 1, 1, 7}) + " should be 4.");
}
}
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
* parity returns true if there are even number of even numbers and false otherwise.
* For example, if arr is [1, 7, 4, 3, 6], then parity(arr) == true.
* But, if arr is [6, 5, 4, 3, 2, 1], parity(arr) == false.
*
* Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
*/
public class Parity {
private static final ForkJoinPool POOL = new ForkJoinPool();
private static final int CUTOFF = 1;
private static boolean sequential(int[] arr, int lo, int hi) {
// TODO: your code here
return false; // TODO: you may want to change this
}
public static boolean parallel(int[] arr) {
return false; // TODO: you may want to change this
}
private static class ParityTask extends RecursiveTask<Boolean> {
int[] arr;
int lo, hi;
public ParityTask(int[] arr, int lo, int hi) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
}
@Override
protected Boolean compute() {
// TODO: your code here
return false; // TODO: you may want to change this
}
}
public static void main(String[] args) {
System.out.println(parallel(new int[]{1, 7, 4, 3, 6}) + " should be true.");
System.out.println(parallel(new int[]{6, 5, 4, 3, 2, 1}) + " should be false.");
}
}
import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* powMod replaces every element of arr with arr[i]^p mod m.
* For example, if arr is [1, 7, 4, 3, 6], then powmod(arr, 2, 5)
* would result in arr = [1, 4, 1, 4, 1].
*
* Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
*/
public class PowMod {
private static final ForkJoinPool POOL = new ForkJoinPool();
private static final int CUTOFF = 1;
public static void sequential(int[] arr, int pow, int mod, int lo, int hi) {
// TODO: your code here
}
public static void parallel(int[] arr, int pow, int mod) {
// TODO: your code here
}
private static class PowModTask extends RecursiveAction {
int[] arr;
int lo, hi;
int pow, mod;
public PowModTask(int[] arr, int pow, int mod, int lo, int hi) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
this.pow = pow;
this.mod = mod;
}
@Override
protected void compute() {
// TODO: your code here
}
}
public static void main(String[] args) {
int[] arr = {1, 7, 4, 3, 6};
parallel(arr, 6, 5000);
System.out.println(Arrays.toString(arr) + " should be [1, 2649, 4096, 729, 1656]");
}
}
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.Arrays;
/**
* secondSmallest returns the second smallest unique element of arr.
* Assume arr contains at least two unique elements.
* For example, if arr is [1, 7, 4, 3, 6], then secondSmallest(arr) == 3.
* But, if arr is [6, 1, 4, 3, 5, 2, 1], secondSmallest(arr) == 2.
*
* Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
*/
public class SecondSmallest {
private static final ForkJoinPool POOL = new ForkJoinPool();
private static final int CUTOFF = 1;
private static class TwoSmallest {
int smallest, secondSmallest;
public TwoSmallest() {
smallest = secondSmallest = Integer.MAX_VALUE;
}
}
public static TwoSmallest combine(TwoSmallest a, TwoSmallest b) {
// TODO: your code here
return new TwoSmallest(); // TODO: you may want to change this
}
public static TwoSmallest sequential(int[] arr, int lo, int hi) {
// TODO: your code here
return new TwoSmallest(); // TODO: you may want to change this
}
public static int parallel(int[] arr) {
TwoSmallest result = POOL.invoke(new SecondSmallestTask(arr, 0, arr.length));
return result.secondSmallest;
}
private static class SecondSmallestTask extends RecursiveTask<TwoSmallest> {
int[] arr;
int lo, hi;
public SecondSmallestTask(int[] arr, int lo, int hi) {
this.arr = arr;
this.lo = lo;
this.hi = hi;
}
@Override
protected TwoSmallest compute() {
// TODO: your code here
return new TwoSmallest(); // TODO: you may want to change this
}
}
public static void main(String[] args) {
System.out.println(parallel(new int[]{1, 7, 4, 3, 6}) + " should be 3.");
System.out.println(parallel(new int[]{6, 1, 4, 3, 5, 2, 1}) + " should be 2.");
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class TestBasic {
@Test
public void testCountStrs() {
String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
assertEquals(3, CountStrs.parallel(arr, "llll"));
assertEquals(1, CountStrs.parallel(arr, "h"));
}
@Test
public void testLessThan7() {
assertEquals(4, LessThan7.parallel(new int[]{21, 7, 6, 8, 17, 1, 7, 7, 1, 1, 7}));
}
@Test
public void testParity() {
assertTrue(Parity.parallel(new int[]{1, 7, 4, 3, 6}));
assertFalse(Parity.parallel(new int[]{6, 5, 4, 3, 2, 1}));
}
@Test
public void testPowMod() {
int[] arr = {1, 7, 4, 3, 6};
int[] expected = {1, 2649, 4096, 729, 1656};
PowMod.parallel(arr, 6, 5000);
assertArrayEquals(expected, arr);
}
@Test
public void testSecondSmallest() {
assertEquals(3, SecondSmallest.parallel(new int[]{1, 7, 4, 3, 6}));
assertEquals(2, SecondSmallest.parallel(new int[]{6, 1, 4, 3, 5, 2, 1}));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment