forked from bytedeco/javacpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bytedeco#391 Move out
Index
as an interface and create a separate c…
…lass `CustomStridesIndex` to keep the previous logic. Add also `DefaultIndex`
- Loading branch information
Showing
5 changed files
with
250 additions
and
52 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/org/bytedeco/javacpp/indexer/CustomStridesIndex.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,87 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Samuel Audet | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE.txt file that accompanied this code. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.bytedeco.javacpp.indexer; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @author Matteo Di Giovinazzo | ||
*/ | ||
public class CustomStridesIndex implements Index { | ||
|
||
protected final long[] sizes; | ||
|
||
protected final long[] strides; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param sizes | ||
* @param strides The number of elements to skip to reach the next element in a given dimension. | ||
* {@code strides[i] > strides[i + 1] && strides[strides.length - 1] == 1} preferred. | ||
*/ | ||
protected CustomStridesIndex(long[] sizes, long[] strides) { | ||
this.sizes = sizes; | ||
this.strides = strides; | ||
} | ||
|
||
public static Index customStrides(long[] sizes, long[] strides) { | ||
return new CustomStridesIndex(sizes, strides); | ||
} | ||
|
||
@Override | ||
public long[] sizes() { return sizes; } | ||
|
||
@Deprecated | ||
@Override | ||
public long[] strides() { return strides; } | ||
|
||
@Override | ||
public long index(long i) { | ||
return i * strides[0]; | ||
} | ||
|
||
@Override | ||
public long index(long i, long j) { | ||
return i * strides[0] + j * strides[1]; | ||
} | ||
|
||
@Override | ||
public long index(long i, long j, long k) { | ||
return i * strides[0] + j * strides[1] + k * strides[2]; | ||
} | ||
|
||
/** | ||
* Computes the linear index as the dot product of indices and strides. | ||
* | ||
* @param indices of each dimension | ||
* @return index to access array or buffer | ||
*/ | ||
@Override | ||
public long index(long... indices) { | ||
long index = 0; | ||
for (int i = 0; i < indices.length && i < strides.length; i++) { | ||
index += indices[i] * strides[i]; | ||
} | ||
return index; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/bytedeco/javacpp/indexer/DefaultIndex.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,51 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Samuel Audet | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE.txt file that accompanied this code. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.bytedeco.javacpp.indexer; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @author Matteo Di Giovinazzo | ||
*/ | ||
public class DefaultIndex extends CustomStridesIndex { | ||
|
||
protected DefaultIndex(long... sizes) { | ||
super(sizes, strides(sizes)); | ||
} | ||
|
||
public static Index defaultIndex(long... sizes) { | ||
return new DefaultIndex(sizes); | ||
} | ||
|
||
/** | ||
* Returns default (row-major contiguous) strides for the given sizes. | ||
*/ | ||
public static long[] strides(long... sizes) { | ||
long[] strides = new long[sizes.length]; | ||
strides[sizes.length - 1] = 1; | ||
for (int i = sizes.length - 2; i >= 0; i--) { | ||
strides[i] = strides[i + 1] * sizes[i + 1]; | ||
} | ||
return strides; | ||
} | ||
|
||
} |
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,70 @@ | ||
/* | ||
* Copyright (C) 2016-2019 Samuel Audet | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE.txt file that accompanied this code. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.bytedeco.javacpp.indexer; | ||
|
||
/** | ||
* TODO | ||
* | ||
* @author Matteo Di Giovinazzo | ||
*/ | ||
public interface Index { | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param i | ||
* @return | ||
*/ | ||
long index(long i); | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param i | ||
* @param j | ||
* @return | ||
*/ | ||
long index(long i, long j); | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param i | ||
* @param j | ||
* @param k | ||
* @return | ||
*/ | ||
long index(long i, long j, long k); | ||
|
||
/** | ||
* TODO | ||
* | ||
* @param indices | ||
* @return | ||
*/ | ||
long index(long... indices); | ||
|
||
long[] sizes(); | ||
|
||
@Deprecated | ||
long[] strides(); | ||
} |
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