Skip to content
Permalink
Browse files

* Add new `indexer` package containing a set of `Indexer` for easy a…

…nd efficient multidimensional access of arrays and buffers ([issue javacv:317](http://code.google.com/p/javacv/issues/detail?id=317))
  • Loading branch information...
saudet committed Oct 19, 2014
1 parent 6dac3bb commit 4d8dc9d8b244b79ca3c9d9355e476f028f3f36f9
Showing with 2,072 additions and 0 deletions.
  1. +1 −0 CHANGELOG.md
  2. +5 −0 pom.xml
  3. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/ByteArrayIndexer.java
  4. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/ByteBufferIndexer.java
  5. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/ByteIndexer.java
  6. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/CharArrayIndexer.java
  7. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/CharBufferIndexer.java
  8. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/CharIndexer.java
  9. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/DoubleArrayIndexer.java
  10. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/DoubleBufferIndexer.java
  11. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/DoubleIndexer.java
  12. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/FloatArrayIndexer.java
  13. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/FloatBufferIndexer.java
  14. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/FloatIndexer.java
  15. +44 −0 src/main/java/org/bytedeco/javacpp/indexer/Indexable.java
  16. +86 −0 src/main/java/org/bytedeco/javacpp/indexer/Indexer.java
  17. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/IntArrayIndexer.java
  18. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/IntBufferIndexer.java
  19. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/IntIndexer.java
  20. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/LongArrayIndexer.java
  21. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/LongBufferIndexer.java
  22. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/LongIndexer.java
  23. +71 −0 src/main/java/org/bytedeco/javacpp/indexer/ShortArrayIndexer.java
  24. +74 −0 src/main/java/org/bytedeco/javacpp/indexer/ShortBufferIndexer.java
  25. +61 −0 src/main/java/org/bytedeco/javacpp/indexer/ShortIndexer.java
  26. +4 −0 src/main/java/org/bytedeco/javacpp/indexer/package-info.java
  27. +490 −0 src/test/java/org/bytedeco/javacpp/IndexerTest.java
@@ -1,4 +1,5 @@

* Add new `indexer` package containing a set of `Indexer` for easy and efficient multidimensional access of arrays and buffers ([issue javacv:317](http://code.google.com/p/javacv/issues/detail?id=317))
* Use `Long.decode()` inside the `Tokenizer` to test more precisely when integer values are larger than 32 bits
* Have the `Parser` produce `@Name("operator=") ... put(... )` methods for standard C++ containers, avoiding mistaken calls to `Pointer.put(Pointer)` ([issue javacv:34](https://github.com/bytedeco/javacv/issues/34))
* Let the `Parser` apply `Info.skip` in the case of macros as well
@@ -63,6 +63,11 @@
<version>2.0</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>

<build>
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2014 Samuel Audet
*
* This file is part of JavaCPP.
*
* JavaCPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCPP. If not, see <http://www.gnu.org/licenses/>.
*/

package org.bytedeco.javacpp.indexer;

/**
* An indexer for a {@code byte[]} array.
*
* @author Samuel Audet
*/
public class ByteArrayIndexer extends ByteIndexer {
/** The backing array. */
protected byte[] array;

/** Constructor to set the {@link #array}, {@link #sizes} and {@link #strides}. */
public ByteArrayIndexer(byte[] array, int[] sizes, int[] strides) {
super(sizes, strides);
this.array = array;
}

@Override public byte[] array() {
return array;
}

@Override public byte get(int i) {
return array[i];
}
@Override public byte get(int i, int j) {
return array[i * strides[0] + j];
}
@Override public byte get(int i, int j, int k) {
return array[i * strides[0] + j * strides[1] + k];
}
@Override public byte get(int ... indices) {
return array[index(indices)];
}

@Override public ByteIndexer put(int i, byte b) {
array[i] = b;
return this;
}
@Override public ByteIndexer put(int i, int j, byte b) {
array[i * strides[0] + j] = b;
return this;
}
@Override public ByteIndexer put(int i, int j, int k, byte b) {
array[i * strides[0] + j * strides[1] + k] = b;
return this;
}
@Override public ByteIndexer put(int[] indices, byte b) {
array[index(indices)] = b;
return this;
}
}
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2014 Samuel Audet
*
* This file is part of JavaCPP.
*
* JavaCPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCPP. If not, see <http://www.gnu.org/licenses/>.
*/

package org.bytedeco.javacpp.indexer;

import java.nio.Buffer;
import java.nio.ByteBuffer;

/**
* An indexer for a {@link ByteBuffer}.
*
* @author Samuel Audet
*/
public class ByteBufferIndexer extends ByteIndexer {
/** The backing buffer. */
protected ByteBuffer buffer;

/** Constructor to set the {@link #buffer}, {@link #sizes} and {@link #strides}. */
public ByteBufferIndexer(ByteBuffer buffer, int[] sizes, int[] strides) {
super(sizes, strides);
this.buffer = buffer;
}

@Override public Buffer buffer() {
return buffer;
}

@Override public byte get(int i) {
return buffer.get(i);
}
@Override public byte get(int i, int j) {
return buffer.get(i * strides[0] + j);
}
@Override public byte get(int i, int j, int k) {
return buffer.get(i * strides[0] + j * strides[1] + k);
}
@Override public byte get(int ... indices) {
return buffer.get(index(indices));
}

@Override public ByteIndexer put(int i, byte b) {
buffer.put(i, b);
return this;
}
@Override public ByteIndexer put(int i, int j, byte b) {
buffer.put(i * strides[0] + j, b);
return this;
}
@Override public ByteIndexer put(int i, int j, int k, byte b) {
buffer.put(i * strides[0] + j * strides[1] + k, b);
return this;
}
@Override public ByteIndexer put(int[] indices, byte b) {
buffer.put(index(indices), b);
return this;
}
}
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2014 Samuel Audet
*
* This file is part of JavaCPP.
*
* JavaCPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCPP. If not, see <http://www.gnu.org/licenses/>.
*/

package org.bytedeco.javacpp.indexer;

import java.nio.ByteBuffer;

/**
* Abstract indexer for the {@code byte} primitive type.
*
* @author Samuel Audet
*/
public abstract class ByteIndexer extends Indexer {
protected ByteIndexer(int[] sizes, int[] strides) {
super(sizes, strides);
}

/** @return {@code new ByteArrayIndexer(array, sizes, strides)} */
public static ByteIndexer create(byte[] array, int[] sizes, int[] strides) {
return new ByteArrayIndexer(array, sizes, strides);
}
/** @return {@code new ByteBufferIndexer(buffer, sizes, strides)} */
public static ByteIndexer create(ByteBuffer buffer, int[] sizes, int[] strides) {
return new ByteBufferIndexer(buffer, sizes, strides);
}

/** @return {@code array/buffer[i]} */
public abstract byte get(int i);
/** @return {@code array/buffer[i * strides[0] + j]} */
public abstract byte get(int i, int j);
/** @return {@code array/buffer[i * strides[0] + j * strides[1] + k]} */
public abstract byte get(int i, int j, int k);
/** @return {@code array/buffer[index(indices)]} */
public abstract byte get(int ... indices);

/** @return {@code this} where {@code array/buffer[i] = b} */
public abstract ByteIndexer put(int i, byte b);
/** @return {@code this} where {@code array/buffer[i * strides[0] + j] = b} */
public abstract ByteIndexer put(int i, int j, byte b);
/** @return {@code this} where {@code array/buffer[i * strides[0] + j * strides[1] + k] = b} */
public abstract ByteIndexer put(int i, int j, int k, byte b);
/** @return {@code this} where {@code array/buffer[index(indices)] = b} */
public abstract ByteIndexer put(int[] indices, byte b);
}
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2014 Samuel Audet
*
* This file is part of JavaCPP.
*
* JavaCPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCPP. If not, see <http://www.gnu.org/licenses/>.
*/

package org.bytedeco.javacpp.indexer;

/**
* An indexer for a {@code char[]} array.
*
* @author Samuel Audet
*/
public class CharArrayIndexer extends CharIndexer {
/** The backing array. */
protected char[] array;

/** Constructor to set the {@link #array}, {@link #sizes} and {@link #strides}. */
public CharArrayIndexer(char[] array, int[] sizes, int[] strides) {
super(sizes, strides);
this.array = array;
}

@Override public char[] array() {
return array;
}

@Override public char get(int i) {
return array[i];
}
@Override public char get(int i, int j) {
return array[i * strides[0] + j];
}
@Override public char get(int i, int j, int k) {
return array[i * strides[0] + j * strides[1] + k];
}
@Override public char get(int ... indices) {
return array[index(indices)];
}

@Override public CharIndexer put(int i, char c) {
array[i] = c;
return this;
}
@Override public CharIndexer put(int i, int j, char c) {
array[i * strides[0] + j] = c;
return this;
}
@Override public CharIndexer put(int i, int j, int k, char c) {
array[i * strides[0] + j * strides[1] + k] = c;
return this;
}
@Override public CharIndexer put(int[] indices, char c) {
array[index(indices)] = c;
return this;
}
}
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2014 Samuel Audet
*
* This file is part of JavaCPP.
*
* JavaCPP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version (subject to the "Classpath" exception
* as provided in the LICENSE.txt file that accompanied this code).
*
* JavaCPP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaCPP. If not, see <http://www.gnu.org/licenses/>.
*/

package org.bytedeco.javacpp.indexer;

import java.nio.Buffer;
import java.nio.CharBuffer;

/**
* An indexer for a {@link CharBuffer}.
*
* @author Samuel Audet
*/
public class CharBufferIndexer extends CharIndexer {
/** The backing buffer. */
protected CharBuffer buffer;

/** Constructor to set the {@link #buffer}, {@link #sizes} and {@link #strides}. */
public CharBufferIndexer(CharBuffer buffer, int[] sizes, int[] strides) {
super(sizes, strides);
this.buffer = buffer;
}

@Override public Buffer buffer() {
return buffer;
}

@Override public char get(int i) {
return buffer.get(i);
}
@Override public char get(int i, int j) {
return buffer.get(i * strides[0] + j);
}
@Override public char get(int i, int j, int k) {
return buffer.get(i * strides[0] + j * strides[1] + k);
}
@Override public char get(int ... indices) {
return buffer.get(index(indices));
}

@Override public CharIndexer put(int i, char c) {
buffer.put(i, c);
return this;
}
@Override public CharIndexer put(int i, int j, char c) {
buffer.put(i * strides[0] + j, c);
return this;
}
@Override public CharIndexer put(int i, int j, int k, char c) {
buffer.put(i * strides[0] + j * strides[1] + k, c);
return this;
}
@Override public CharIndexer put(int[] indices, char c) {
buffer.put(index(indices), c);
return this;
}
}

0 comments on commit 4d8dc9d

Please sign in to comment.
You can’t perform that action at this time.