com.github.scr.hashmap.collections.FloatBufferCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of memory-mapped-hashmap Show documentation
Show all versions of memory-mapped-hashmap Show documentation
Readonly Set and Map that use a MappedByteBuffer to load quickly and use minimal Java Heap.
/*
The MIT License (MIT)
Copyright (c) 2015 Sheridan C Rawlins
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.github.scr.hashmap.collections;
import com.google.common.collect.Iterators;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import static com.github.scr.hashmap.Constants.MAGIC;
import static com.github.scr.hashmap.Constants.VERSION;
/**
* Created by scr on 7/2/15.
*/
public class FloatBufferCollection implements IndexedCollection {
@NotNull
private final FloatBuffer BUFFER;
@Nullable
@Override
public Float get(int index) {
return BUFFER.get(index);
}
@Override
public void writeOutput(@NotNull DataOutput dataOutput) throws IOException {
dataOutput.writeInt(MAGIC);
dataOutput.writeInt(VERSION);
int size = BUFFER.remaining();
dataOutput.writeInt(size);
for (int i = 0; i < size; ++i) {
dataOutput.writeFloat(BUFFER.get(i));
}
}
public static class BufferIterator implements Iterator {
@NotNull
private final FloatBuffer BUFFER;
public BufferIterator(@NotNull FloatBuffer buffer) {
BUFFER = buffer.duplicate();
}
@Override
public boolean hasNext() {
return BUFFER.hasRemaining();
}
@NotNull
@Override
public Float next() {
return BUFFER.get();
}
}
public FloatBufferCollection(@NotNull FloatBuffer ints) {
BUFFER = ints;
}
@Override
public int size() {
return BUFFER.capacity();
}
@Override
public boolean isEmpty() {
return BUFFER.capacity() > 0;
}
@Override
public boolean contains(Object o) {
if (!(o instanceof Float)) {
return false;
}
Float oFloat = (Float) o;
return Iterators.contains(iterator(), oFloat);
}
@NotNull
@Override
public Iterator iterator() {
return new BufferIterator(BUFFER);
}
@NotNull
@Override
public Object[] toArray() {
int size = size();
Object[] ret = new Object[size];
for (int i = 0; i < size; ++i) {
ret[i] = BUFFER.get(i);
}
return ret;
}
@NotNull
@Override
public T[] toArray(@NotNull T[] a) {
int size = size();
if (a.length < size) {
a = Arrays.copyOf(a, size);
}
for (int i = 0; i < size; ++i) {
@SuppressWarnings("unchecked")
T t = (T) Float.valueOf(BUFFER.get(i));
a[i] = t;
}
return a;
}
@Override
public boolean add(@NotNull Float aFloat) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(@NotNull Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(@NotNull Collection> c) {
for (Object o : c) {
if (!contains(o)) {
return false;
}
}
return true;
}
@Override
public boolean addAll(@NotNull Collection extends Float> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(@NotNull Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(@NotNull Collection> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public float getFloat(int index) {
return BUFFER.get(index);
}
}