Skip to content

Commit 47e2d1a

Browse files
committed
Added bulk operations
- Added array insertion operations to lists and queues - Added basic set operations - Unit tests
1 parent 06e804c commit 47e2d1a

49 files changed

Lines changed: 1402 additions & 10 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ build
3434

3535
# Ignore Intellij files
3636
/.idea/
37+
38+
# Ignore build output
39+
/out/

lib/build.gradle.kts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,29 @@ repositories {
88
mavenCentral()
99
}
1010

11-
java {withSourcesJar()}
11+
java {
12+
withSourcesJar()
13+
withJavadocJar()
14+
}
1215

1316
tasks {
1417
test {useJUnitPlatform()}
1518
withType<Jar> {
1619
manifest {attributes(mapOf("PrimitiveContainer" to rootProject.name))}
1720
archiveBaseName.set(rootProject.name)
1821
}
22+
withType<Javadoc> {
23+
(options as StandardJavadocDocletOptions).tags(
24+
"apiNote:a:API Note:",
25+
"implSpec:a:Implementation Requirements:",
26+
"implNote:a:Implementation Note:"
27+
)
28+
}
29+
}
30+
tasks.register<AbstractCompile>("release") {
31+
dependsOn(tasks.withType<Test>(),tasks.withType<Jar>(),tasks.withType<Javadoc>())
32+
description = "Builds release artifacts"
33+
group = "build"
1934
}
2035

2136
dependencies {

lib/src/main/java/azuretriple/primitivecontainer/ByteList.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,27 @@ default void set(final int location,final byte v)
3333

3434
/** Appends the values in the argument to the end of the list. */
3535
default void add(final byte[] v) {((List)this).add(v);}
36+
/**
37+
* Appends the values in the argument to the end of the list.
38+
*
39+
* @param start Starting position in the array.
40+
* @param length Number of elements to copy.
41+
*/
42+
default void add(final byte[] v,final int start,final int length) {((List)this).add(v,start,length);}
3643
/** Appends the argument to the end of the list. */
3744
default void add(final byte v) {((List)this).addLogic(); data()[((List)this).size++] = v;}
3845
/** Inserts the specified values immediately before the specified location. */
3946
default void insert(final int location,final byte[] v) {((List)this).insert(location,v);}
47+
/**
48+
* Inserts the specified values immediately before the specified location.
49+
*
50+
* @param start Starting position in the array.
51+
* @param length Number of elements to copy.
52+
*/
53+
default void insert(final int location,final byte[] v,final int start,final int length)
54+
{
55+
((List)this).insert(location,v,start,length);
56+
}
4057
/** Inserts the specified value immediately before the specified location. */
4158
default void insert(final int location,final byte v) {((List)this).insertLogic(location); data()[location] = v;}
4259
/** Removes the last value from the list. */

lib/src/main/java/azuretriple/primitivecontainer/ByteQueue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ default boolean equals(final ByteQueue other)
1818

1919
/** Appends the values in the argument to the queue. */
2020
default void push(final byte[] v) {((Queue)this).push(v);}
21+
/**
22+
* Appends the values in the argument to the queue.
23+
*
24+
* @param start Starting position in the array.
25+
* @param length Number of elements to copy.
26+
*/
27+
default void push(final byte[] v,final int start,final int length) {((Queue)this).push(v,start,length);}
2128
/** Appends the value to the queue. */
2229
default void push(final byte v) {((Queue)this).pushLogic(); data()[((Queue)this).end++] = v;}
2330
/** Removes the value at the front of the queue. */

lib/src/main/java/azuretriple/primitivecontainer/ByteSet.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,55 @@ public int remove(final byte v)
3232
/** @return {@code true} iff the value exists in the set. */
3333
public boolean contains(final byte v) {return data.find(v) < 0;}
3434

35+
/** Adds all elements not present in this set from the argument. */
36+
public void union(final ByteSet other)
37+
{
38+
final ByteList a = data.list;
39+
final byte[] b = other.data.list.data();
40+
final int bl = other.data.list.size();
41+
int bi = 0;
42+
for(int ai = 0;ai < a.size() && bi < bl;++ai)
43+
if(a.data()[ai] >= b[bi])
44+
{
45+
if(a.data()[ai] > b[bi]) a.insert(ai,b[bi]);
46+
++bi;
47+
}
48+
a.add(b,bi,bl-bi);
49+
}
50+
/** Removes all elements in this set not present in the argument. */
51+
public void intersect(final ByteSet other)
52+
{
53+
final byte[] a = data.list.data(),b = other.data.list.data();
54+
final int al = data.list.size(),bl = other.data.list.size();
55+
int ai = 0;
56+
{
57+
final int min = Math.min(al,bl);
58+
while(ai < min && a[ai] == b[ai]) ++ai;
59+
}
60+
int bi = ai,ci = ai;
61+
while(ai < al && bi < bl)
62+
if(a[ai] < b[bi]) ++ai;
63+
else
64+
{
65+
if(a[ai] == b[bi]) a[ci++] = a[ai++];
66+
++bi;
67+
}
68+
((List)data.list).size = ci;
69+
}
70+
/** Removes all elements in this set present in the other set. */
71+
public void subtract(final ByteSet other)
72+
{
73+
final byte[] a = data.list.data(),b = other.data.list.data();
74+
final int al = data.list.size(),bl = other.data.list.size();
75+
int ai = 0,bi = 0,ci = 0;
76+
while(ai < al && bi < bl)
77+
if(a[ai] == b[bi]) {++ai; ++bi;}
78+
else if(a[ai] < b[bi]) a[ci++] = a[ai++];
79+
else ++bi;
80+
System.arraycopy(a,ai,a,ci,al-ai);
81+
((List)data.list).size = ci;
82+
}
83+
3584
@SuppressWarnings("MethodDoesntCallSuperMethod")
3685
@Override public ByteSet clone() {return new ByteSet(this);}
3786
}

lib/src/main/java/azuretriple/primitivecontainer/Container.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public abstract class Container implements PrimitiveContainer
77
{
88
static Object arrInst(final Object arr,final int size) {return newInstance(arr.getClass().componentType(),size);}
99

10-
Object arr;
10+
/** The backing array for this container. Exercise caution when modifying this variable. */
11+
public Object arr;
1112

1213
Container(final Object arr) {this.arr = arr; assert arr != null;}
1314

lib/src/main/java/azuretriple/primitivecontainer/DoubleList.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,27 @@ default void set(final int location,final double v)
3333

3434
/** Appends the values in the argument to the end of the list. */
3535
default void add(final double[] v) {((List)this).add(v);}
36+
/**
37+
* Appends the values in the argument to the end of the list.
38+
*
39+
* @param start Starting position in the array.
40+
* @param length Number of elements to copy.
41+
*/
42+
default void add(final double[] v,final int start,final int length) {((List)this).add(v,start,length);}
3643
/** Appends the argument to the end of the list. */
3744
default void add(final double v) {((List)this).addLogic(); data()[((List)this).size++] = v;}
3845
/** Inserts the specified values immediately before the specified location. */
3946
default void insert(final int location,final double[] v) {((List)this).insert(location,v);}
47+
/**
48+
* Inserts the specified values immediately before the specified location.
49+
*
50+
* @param start Starting position in the array.
51+
* @param length Number of elements to copy.
52+
*/
53+
default void insert(final int location,final double[] v,final int start,final int length)
54+
{
55+
((List)this).insert(location,v,start,length);
56+
}
4057
/** Inserts the specified value immediately before the specified location. */
4158
default void insert(final int location,final double v) {((List)this).insertLogic(location); data()[location] = v;}
4259
/** Removes the last value from the list. */

lib/src/main/java/azuretriple/primitivecontainer/DoubleQueue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ default boolean equals(final DoubleQueue other)
1818

1919
/** Appends the values in the argument to the queue. */
2020
default void push(final double[] v) {((Queue)this).push(v);}
21+
/**
22+
* Appends the values in the argument to the queue.
23+
*
24+
* @param start Starting position in the array.
25+
* @param length Number of elements to copy.
26+
*/
27+
default void push(final double[] v,final int start,final int length) {((Queue)this).push(v,start,length);}
2128
/** Appends the value to the queue. */
2229
default void push(final double v) {((Queue)this).pushLogic(); data()[((Queue)this).end++] = v;}
2330
/** Removes the value at the front of the queue. */

lib/src/main/java/azuretriple/primitivecontainer/DoubleSet.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,55 @@ public int remove(final double v)
3232
/** @return {@code true} iff the value exists in the set. */
3333
public boolean contains(final double v) {return data.find(v) < 0;}
3434

35+
/** Adds all elements not present in this set from the argument. */
36+
public void union(final DoubleSet other)
37+
{
38+
final DoubleList a = data.list;
39+
final double[] b = other.data.list.data();
40+
final int bl = other.data.list.size();
41+
int bi = 0;
42+
for(int ai = 0;ai < a.size() && bi < bl;++ai)
43+
if(a.data()[ai] >= b[bi])
44+
{
45+
if(a.data()[ai] > b[bi]) a.insert(ai,b[bi]);
46+
++bi;
47+
}
48+
a.add(b,bi,bl-bi);
49+
}
50+
/** Removes all elements in this set not present in the argument. */
51+
public void intersect(final DoubleSet other)
52+
{
53+
final double[] a = data.list.data(),b = other.data.list.data();
54+
final int al = data.list.size(),bl = other.data.list.size();
55+
int ai = 0;
56+
{
57+
final int min = Math.min(al,bl);
58+
while(ai < min && a[ai] == b[ai]) ++ai;
59+
}
60+
int bi = ai,ci = ai;
61+
while(ai < al && bi < bl)
62+
if(a[ai] < b[bi]) ++ai;
63+
else
64+
{
65+
if(a[ai] == b[bi]) a[ci++] = a[ai++];
66+
++bi;
67+
}
68+
((List)data.list).size = ci;
69+
}
70+
/** Removes all elements in this set present in the other set. */
71+
public void subtract(final DoubleSet other)
72+
{
73+
final double[] a = data.list.data(),b = other.data.list.data();
74+
final int al = data.list.size(),bl = other.data.list.size();
75+
int ai = 0,bi = 0,ci = 0;
76+
while(ai < al && bi < bl)
77+
if(a[ai] == b[bi]) {++ai; ++bi;}
78+
else if(a[ai] < b[bi]) a[ci++] = a[ai++];
79+
else ++bi;
80+
System.arraycopy(a,ai,a,ci,al-ai);
81+
((List)data.list).size = ci;
82+
}
83+
3584
@SuppressWarnings("MethodDoesntCallSuperMethod")
3685
@Override public DoubleSet clone() {return new DoubleSet(this);}
3786
}

lib/src/main/java/azuretriple/primitivecontainer/FixedList.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ void add(final Object v)
1717
arraycopy(v,0,arr,size,vl);
1818
size += vl;
1919
}
20+
@Override
21+
void add(final Object v,final int start,final int length)
22+
{
23+
arraycopy(v,start,arr,size,length);
24+
size += length;
25+
}
2026
@Override void addLogic() {}
2127
@Override
2228
void insert(final int location,final Object v)
@@ -26,6 +32,13 @@ void insert(final int location,final Object v)
2632
arraycopy(v,0,arr,location,vl);
2733
size += vl;
2834
}
35+
@Override
36+
void insert(final int location,final Object v,final int start,final int length)
37+
{
38+
arraycopy(arr,location,arr,location+length,size-location);
39+
arraycopy(v,start,arr,location,length);
40+
size += length;
41+
}
2942
@Override void insertLogic(final int location) {arraycopy(arr,location,arr,location+1,(size++)-location);}
3043
@Override public void remove(final int location) {arraycopy(arr,location+1,arr,location,(--size)-location);}
3144

0 commit comments

Comments
 (0)