Difference between revisions of "Slices"
Line 6: | Line 6: | ||
=Code to Implement= | =Code to Implement= | ||
{{CodeToImplement|Slices|createNSlicesForArrayObject|slices.studio}} | {{CodeToImplement|Slices|createNSlicesForArrayObject|slices.studio}} | ||
+ | |||
+ | In order to support primitive arrays (e.g. byte[], int[], et cetera) and non-primitive arrays (e.g. Object[]) we provide all of the public methods, each which simply call the single method requiring implementation: | ||
+ | |||
+ | {{Sequential|createNSlicesForArrayObject}} | ||
+ | |||
+ | <nowiki> public static <T> List<Slice<T[]>> createNSlices(T[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<byte[]>> createNSlices(byte[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<char[]>> createNSlices(char[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<short[]>> createNSlices(short[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<int[]>> createNSlices(int[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<long[]>> createNSlices(long[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<float[]>> createNSlices(float[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | } | ||
+ | public static List<Slice<double[]>> createNSlices(double[] data, int numSlices) { | ||
+ | return createNSlicesForArrayObject(data, numSlices); | ||
+ | }</nowiki> | ||
Line 35: | Line 64: | ||
In order to conveniently support primitive arrays and Object arrays we will need to use the [https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html#getLength-java.lang.Object- Array.getLength(Object)] method. | In order to conveniently support primitive arrays and Object arrays we will need to use the [https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Array.html#getLength-java.lang.Object- Array.getLength(Object)] method. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=Mistakes To Avoid= | =Mistakes To Avoid= | ||
Line 66: | Line 71: | ||
=Background= | =Background= | ||
− | |||
As the name suggests, we will create a class to represent a <code>Slice</code> of data and a list of <code>Slice</code> called <code>Slices</code> which divides the data into a specified number of <code>Slice</code> given an array of data. | As the name suggests, we will create a class to represent a <code>Slice</code> of data and a list of <code>Slice</code> called <code>Slices</code> which divides the data into a specified number of <code>Slice</code> given an array of data. |
Revision as of 04:08, 22 January 2018
Contents
Motivation
Coarsening, or n-way split as we tend to call it in this course, comes up a fair amount. This studio has you implement a utility that you can use over and over throughout the semester.
In order to support future testing well, we are strict about exactly how the data is split up among the slices.
Code to Implement
class: | Slices.java | |
methods: | createNSlicesForArrayObject | |
package: | slices.studio | |
source folder: | student/src/main/java |
In order to support primitive arrays (e.g. byte[], int[], et cetera) and non-primitive arrays (e.g. Object[]) we provide all of the public methods, each which simply call the single method requiring implementation:
method: createNSlicesForArrayObject
(sequential implementation only)
public static <T> List<Slice<T[]>> createNSlices(T[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<byte[]>> createNSlices(byte[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<char[]>> createNSlices(char[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<short[]>> createNSlices(short[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<int[]>> createNSlices(int[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<long[]>> createNSlices(long[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<float[]>> createNSlices(float[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); } public static List<Slice<double[]>> createNSlices(double[] data, int numSlices) { return createNSlicesForArrayObject(data, numSlices); }
Example: array.length=7; numSlices=4
Distribute the remainder 1 each to the lower end slices.
A | A | B | B | C | C | D |
Warning:Do NOT slice up the data by giving all of the remainder to one slice |
A
|B
|C
|D
|D
|D
|D
Java Utility
In order to conveniently support primitive arrays and Object arrays we will need to use the Array.getLength(Object) method.
Mistakes To Avoid
Warning: Do NOT Parallelize |
Warning: Do NOT Copy The Data Into SubArrays |
Background
As the name suggests, we will create a class to represent a Slice
of data and a list of Slice
called Slices
which divides the data into a specified number of Slice
given an array of data.
Where to Start
Navigate to the slice directory and look under slice.studio
. You must implement Slices
using the implementation of Slice
given to you under the slice.core
directory.
createNSlices
To create this slice, you will start at the beginning of the data array and keep creating new slices until you have numSlices which more or less evenly span the range. Remember that the sliceId should start at the zero index and the length of the list should be equal to the number of slices. Also remember that if the range is not divisible by numSlices, you must account for this by either evenly distributing the extras or making a slightly larger slice at the end of the range.
Hint: you do not need to divide up the array of data fed into the method and doing so would remove the need for a minInclusive
variable for a Slice
.
Javadocs
use: class Slice
Testing Your Solution
Correctness
class: | SlicesTestSuite.java | |
package: | slice.studio | |
source folder: | testing/src/test/java |