Skip to content

Commit

Permalink
Add FunctionalObject2IntMap with type specific computeIfAbsent
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldexun committed Jun 27, 2023
1 parent 445cdc4 commit 485a586
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/main/java/meldexun/renderlib/util/FunctionalObject2IntMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package meldexun.renderlib.util;

import static it.unimi.dsi.fastutil.HashCommon.arraySize;

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;

@SuppressWarnings("serial")
class FunctionalObject2IntMap<K> extends Object2IntOpenHashMap<K> {

public int computeIfAbsent(final K k, final java.util.function.ToIntFunction<? super K> mappingFunction) {
java.util.Objects.requireNonNull(mappingFunction);
final int pos = find(k);
if (pos >= 0) return value[pos];
final int newValue = mappingFunction.applyAsInt(k);
insert(-pos - 1, k, newValue);
return newValue;
}

private int find(final K k) {
if (((k) == null)) return containsNullKey ? n : -(n + 1);
K curr;
final K[] key = this.key;
int pos;
// The starting point.
if (((curr = key[pos = (it.unimi.dsi.fastutil.HashCommon.mix((k).hashCode())) & mask]) == null)) return -(pos + 1);
if (((k).equals(curr))) return pos;
// There's always an unused entry.
while (true) {
if (((curr = key[pos = (pos + 1) & mask]) == null)) return -(pos + 1);
if (((k).equals(curr))) return pos;
}
}

private void insert(final int pos, final K k, final int v) {
if (pos == n) containsNullKey = true;
key[pos] = k;
value[pos] = v;
if (size++ >= maxFill) rehash(arraySize(size + 1, f));
}

}
18 changes: 14 additions & 4 deletions src/main/java/meldexun/renderlib/util/GLShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
Expand All @@ -17,8 +18,8 @@ public class GLShader {

private static final IntList PROGRAM_STACK = new IntArrayList();
private int program;
private final Object2IntMap<String> uniforms = new Object2IntOpenHashMap<>();
private final Object2IntMap<String> attributes = new Object2IntOpenHashMap<>();
private final FunctionalObject2IntMap<String> uniforms = new FunctionalObject2IntMap<>();
private final FunctionalObject2IntMap<String> attributes = new FunctionalObject2IntMap<>();

public GLShader(int program) {
this.program = program;
Expand Down Expand Up @@ -48,12 +49,21 @@ public void use() {
use(program);
}

public void bind() {
push();
use();
}

public void unbind() {
pop();
}

public int getUniform(String uniform) {
return uniforms.computeIfAbsent(uniform, k -> GL20.glGetUniformLocation(program, k));
return uniforms.computeIfAbsent(uniform, (ToIntFunction<String>) k -> GL20.glGetUniformLocation(program, k));
}

public int getAttribute(String attribute) {
return attributes.computeIfAbsent(attribute, k -> GL20.glGetAttribLocation(program, k));
return attributes.computeIfAbsent(attribute, (ToIntFunction<String>) k -> GL20.glGetAttribLocation(program, k));
}

public void dispose() {
Expand Down

0 comments on commit 485a586

Please sign in to comment.