Package edu.rit.pj.reduction
Class SharedCharacterArray
java.lang.Object
edu.rit.pj.reduction.SharedCharacterArray
Class SharedCharacterArray provides an array reduction variable with elements
of type
char
.
Class SharedCharacterArray is multiple thread safe. The methods use lock-free atomic compare-and-set.
Note: Class SharedCharacterArray is implemented using class
java.util.concurrent.atomic.AtomicIntegerArray. Each character array element
is stored as an int
whose values are restricted to the range of type
char
.
- Version:
- 24-Aug-2007
- Author:
- Alan Kaminsky
-
Constructor Summary
ConstructorDescriptionSharedCharacterArray
(char[] array) Construct a new character array reduction variable whose elements are copied from the given array.SharedCharacterArray
(int len) Construct a new character array reduction variable with the given length. -
Method Summary
Modifier and TypeMethodDescriptionchar
addAndGet
(int i, char value) Add the given value to this array reduction variable at the given index and return the new value.boolean
compareAndSet
(int i, char expect, char update) Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.char
decrementAndGet
(int i) Subtract one from this array reduction variable at the given index and return the new value.char
get
(int i) Returns this array reduction variable's current value at the given index.char
getAndAdd
(int i, char value) Add the given value to this array reduction variable at the given index and return the previous value.char
getAndDecrement
(int i) Subtract one from this array reduction variable at the given index and return the previous value.char
getAndIncrement
(int i) Add one to this array reduction variable at the given index and return the previous value.char
getAndSet
(int i, char value) Set this array reduction variable at the given index to the given value and return the previous value.char
incrementAndGet
(int i) Add one to this array reduction variable at the given index and return the new value.int
length()
Returns this array reduction variable's length.void
reduce
(char[] src, CharacterOp op) Combine this array reduction variable with the given array using the given operation.void
reduce
(int dstoff, char[] src, int srcoff, int len, CharacterOp op) Combine a portion of this array reduction variable with a portion of the given array using the given operation.char
reduce
(int i, char value, CharacterOp op) Combine this array reduction variable at the given index with the given value using the given operation.void
set
(int i, char value) Set this array reduction variable at the given index to the given value.toString()
Returns a string version of this array reduction variable.boolean
weakCompareAndSet
(int i, char expect, char update) Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.
-
Constructor Details
-
SharedCharacterArray
public SharedCharacterArray(int len) Construct a new character array reduction variable with the given length. Each array element is initially 0.- Parameters:
len
- Length.- Throws:
NegativeArraySizeException
- (unchecked exception) Thrown iflen
< 0.
-
SharedCharacterArray
public SharedCharacterArray(char[] array) Construct a new character array reduction variable whose elements are copied from the given array.- Parameters:
array
- Array to copy.- Throws:
NullPointerException
- (unchecked exception) Thrown ifarray
is null.
-
-
Method Details
-
length
public int length()Returns this array reduction variable's length.- Returns:
- Length.
-
get
public char get(int i) Returns this array reduction variable's current value at the given index.- Parameters:
i
- Index.- Returns:
- Current value.
-
set
public void set(int i, char value) Set this array reduction variable at the given index to the given value.- Parameters:
i
- Index.value
- New value.
-
getAndSet
public char getAndSet(int i, char value) Set this array reduction variable at the given index to the given value and return the previous value.- Parameters:
i
- Index.value
- New value.- Returns:
- Previous value.
-
compareAndSet
public boolean compareAndSet(int i, char expect, char update) Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.- Parameters:
i
- Index.expect
- Expected value.update
- Updated value.- Returns:
- True if the update happened, false otherwise.
-
weakCompareAndSet
public boolean weakCompareAndSet(int i, char expect, char update) Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value. May fail spuriously.- Parameters:
i
- Index.expect
- Expected value.update
- Updated value.- Returns:
- True if the update happened, false otherwise.
-
getAndIncrement
public char getAndIncrement(int i) Add one to this array reduction variable at the given index and return the previous value.- Parameters:
i
- Index.- Returns:
- Previous value.
-
getAndDecrement
public char getAndDecrement(int i) Subtract one from this array reduction variable at the given index and return the previous value.- Parameters:
i
- Index.- Returns:
- Previous value.
-
getAndAdd
public char getAndAdd(int i, char value) Add the given value to this array reduction variable at the given index and return the previous value.- Parameters:
i
- Index.value
- Value to add.- Returns:
- Previous value.
-
incrementAndGet
public char incrementAndGet(int i) Add one to this array reduction variable at the given index and return the new value.- Parameters:
i
- Index.- Returns:
- New value.
-
decrementAndGet
public char decrementAndGet(int i) Subtract one from this array reduction variable at the given index and return the new value.- Parameters:
i
- Index.- Returns:
- New value.
-
addAndGet
public char addAndGet(int i, char value) Add the given value to this array reduction variable at the given index and return the new value.- Parameters:
i
- Index.value
- Value to add.- Returns:
- New value.
-
reduce
Combine this array reduction variable at the given index with the given value using the given operation. (This array[i]
) is set to (this array[i]
) op (value
), then (this array[i]
) is returned.- Parameters:
i
- Index.value
- Value.op
- Binary operation.- Returns:
- (This array
[i]
) op (value
).
-
reduce
Combine this array reduction variable with the given array using the given operation. For each indexi
from 0 to this array's length-1, (this array[i]
) is set to (this array[i]
) op (src[i]
).The
reduce()
method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.- Parameters:
src
- Source array.op
- Binary operation.- Throws:
NullPointerException
- (unchecked exception) Thrown ifsrc
is null. Thrown ifop
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown if any array index would be out of bounds.
-
reduce
Combine a portion of this array reduction variable with a portion of the given array using the given operation. For each indexi
from 0 tolen
-1, (this array[dstoff+i]
) is set to (this array[dstoff+i]
) op (src[srcoff+i]
).The
reduce()
method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.- Parameters:
dstoff
- Index of first element to update in this array.src
- Source array.srcoff
- Index of first element to update from in the source array.len
- Number of array elements to update.op
- Binary operation.- Throws:
NullPointerException
- (unchecked exception) Thrown ifsrc
is null. Thrown ifop
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown iflen
< 0. Thrown if any array index would be out of bounds.
-
toString
Returns a string version of this array reduction variable.
-