-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
27 changed files
with
2,072 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/org/bytedeco/javacpp/indexer/ByteArrayIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/java/org/bytedeco/javacpp/indexer/ByteBufferIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/main/java/org/bytedeco/javacpp/indexer/ByteIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
src/main/java/org/bytedeco/javacpp/indexer/CharArrayIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/java/org/bytedeco/javacpp/indexer/CharBufferIndexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.