Skip to content

Commit

Permalink
* Add new indexer package containing a set of Indexer for easy a…
Browse files Browse the repository at this point in the history
…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 4d8dc9d
Show file tree
Hide file tree
Showing 27 changed files with 2,072 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteArrayIndexer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteBufferIndexer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
61 changes: 61 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/ByteIndexer.java
Original file line number Diff line number Diff line change
@@ -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);
}
71 changes: 71 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/CharArrayIndexer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/bytedeco/javacpp/indexer/CharBufferIndexer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 4d8dc9d

Please sign in to comment.