Skip to content

Commit

Permalink
Try with CaseInsensitiveSet, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed Dec 28, 2023
1 parent 55f6c5f commit 9135340
Showing 1 changed file with 78 additions and 8 deletions.
86 changes: 78 additions & 8 deletions src/test/java/com/github/tommyettinger/ds/test/PileupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.github.tommyettinger.digital.Base;
import com.github.tommyettinger.digital.BitConversion;
import com.github.tommyettinger.digital.MathTools;
import com.github.tommyettinger.ds.CaseInsensitiveSet;
import com.github.tommyettinger.ds.CharFilter;
import com.github.tommyettinger.ds.FilteredStringOrderedSet;
import com.github.tommyettinger.ds.FilteredStringSet;
import com.github.tommyettinger.ds.IntLongMap;
import com.github.tommyettinger.ds.IntLongOrderedMap;
import com.github.tommyettinger.ds.ObjectList;
import com.github.tommyettinger.ds.ObjectOrderedSet;
import com.github.tommyettinger.ds.ObjectSet;
import com.github.tommyettinger.ds.Utilities;
Expand All @@ -19,6 +23,8 @@
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.github.tommyettinger.function.IntIntToIntBiFunction;

public class PileupTest {
Expand Down Expand Up @@ -1601,16 +1607,22 @@ public void clear () {
}

@Test
public void testFilteredStringSetWordList () throws IOException {
final List<String> words = Files.readAllLines(Paths.get("src/test/resources/word_list.txt"));
public void testVariousSetWordList () throws IOException {
CharFilter filter = CharFilter.getOrCreate("CaseInsensitive", c -> true, Character::toUpperCase);

FilteredStringOrderedSet wordSet = new FilteredStringOrderedSet(filter, Files.readAllLines(Paths.get("src/test/resources/word_list.txt")));
final ObjectList<String> words = wordSet.order();
Collections.shuffle(words, new WhiskerRandom(1234567890L));
long start = System.nanoTime();
ObjectSet set = new MeasuredFilteredStringSet();
for (int i = 0; i < words.size(); i++) {
set.add(words.get(i));

for(Set set : new Set[]{new MeasuredFilteredStringSet(filter), new MeasuredCaseInsensitiveSet()}){
long start = System.nanoTime();
for (int i = 0; i < words.size(); i++) {
set.add(words.get(i));
}
System.out.println((System.nanoTime() - start) + " ns");
set.clear();
System.out.println("\n");
}
System.out.println((System.nanoTime() - start) + " ns");
set.clear();
}

@Test
Expand Down Expand Up @@ -3544,6 +3556,9 @@ static class MeasuredFilteredStringSet extends FilteredStringSet {
public MeasuredFilteredStringSet () {
super(51, PileupTest.LOAD);
}
public MeasuredFilteredStringSet (CharFilter filter) {
super(filter, 51, PileupTest.LOAD);
}

@Override
protected long hashHelper (String s) {
Expand Down Expand Up @@ -3587,6 +3602,61 @@ protected void resize (int newSize) {
System.out.println("average pileup: " + (averagePileup / size));
}

@Override
public void clear () {
System.out.println("hash multiplier: " + Base.BASE16.unsigned(hashMultiplier) + " with final size " + size);
System.out.print("total collisions: " + collisionTotal);
System.out.println(", longest pileup: " + longestPileup);
System.out.println("total of " + pileupChecks + " longest pileups: " + (allPileups + longestPileup));
super.clear();
}
}
static class MeasuredCaseInsensitiveSet extends CaseInsensitiveSet {
long collisionTotal = 0;
int longestPileup = 0, allPileups = 0, pileupChecks = 0;
double averagePileup = 0;

public MeasuredCaseInsensitiveSet () {
super(51, PileupTest.LOAD);
}

@Override
protected int place (Object item) {
return super.place(item);
}

@Override
protected void addResize (@NonNull CharSequence key) {
Object[] keyTable = this.keyTable;
for (int i = place(key), p = 0; ; i = i + 1 & mask) {
if (keyTable[i] == null) {
keyTable[i] = key;
averagePileup += p;

return;
} else {
collisionTotal++;
longestPileup = Math.max(longestPileup, ++p);
}
}
}

@Override
protected void resize (int newSize) {
allPileups += longestPileup;
pileupChecks++;
collisionTotal = 0;
longestPileup = 0;
averagePileup = 0.0;

super.resize(newSize);

System.out.println("hash multiplier: " + Base.BASE16.unsigned(hashMultiplier) + " with new size " + newSize);
System.out.print("total collisions: " + collisionTotal);
System.out.println(", longest pileup: " + longestPileup);
System.out.println("average pileup: " + (averagePileup / size));
}

@Override
public void clear () {
System.out.println("hash multiplier: " + Base.BASE16.unsigned(hashMultiplier) + " with final size " + size);
Expand Down

0 comments on commit 9135340

Please sign in to comment.