我有一个在Java中使用的匿名内部类在其他几个函数中实现,我尝试将其更改为普通类,因此可以更容易在c ++中进行转换 这是我的代码,
interface CombineFunction {
void combine(int[] target, int[] source);
}
class CombineLocals {
public int[] Vector;
public int[] SrcVector;
}
void Combine(final BinaryMap source, final RectangleC area, final Point at,
final CombineFunction function) {
final int shift = (area.X & WordMask) - (at.X & WordMask);
int vectorSize = (area.Width >> WordShift) + 2;
CombineLocals combineLocals = new CombineLocals();
combineLocals.Vector = new int[vectorSize];
combineLocals.SrcVector = new int[vectorSize];
ParallelForDelegate<CombineLocals> delegate = new ParallelForDelegate<CombineLocals>() {
@Override
public CombineLocals delegate(int y, CombineLocals locals) {
LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
source.LoadLine(locals.SrcVector,
new Point(area.X, area.Y + y), area.Width);
if (shift >= 0)
ShiftLeft(locals.SrcVector, shift);
else
ShiftRight(locals.SrcVector, -shift);
function.combine(locals.Vector, locals.SrcVector);
SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
return locals;
}
@Override
public CombineLocals combineResults(CombineLocals result1,
CombineLocals result2) {
return null;
}
};
Parallel.For(0, area.Height, delegate, combineLocals);
}
由于上面给出的代码ParallelForDelegate也是另一个像匿名内部类一样使用的接口。 我尝试更改代码的方法如下,
class Combine{
BinaryMap source;
RectangleC area;
Point at;
Combine(BinaryMap bm, RectangleC rc, Point p){
source = bm;
area = rc;
at = p;
}
void combine(int[] target, int[] source){
final int shift =(area.X & WordMask) - (at.X & WordMask);
int vectorSize = (area.Width >> WordShift) + 2;
CombineLocals combineLocals = new CombineLocals();
combineLocals.Vector = new int[vectorSize];
combineLocals.SrcVector = new int[vectorSize];
Parallel.For(0, area.Height, delegate(), combineLocals);
}
public CombineLocals delegate(int y, CombineLocals locals, int shift){
LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
source.LoadLine(locals.SrcVector,
new Point(area.X, area.Y + y), area.Width);
if (shift >= 0)
ShiftLeft(locals.SrcVector, shift);
else
ShiftRight(locals.SrcVector, -shift);
combine(locals.Vector, locals.SrcVector);
SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
return locals;
}
}
但是,当我尝试并行调用委托函数时。对于函数我不知道要把什么作为参数。
原始类取自此SDK
https://www.programcreek.com/java-api-examples/index.php?source_dir=SecugenPlugin-master/src/sourceafis/simple/Fingerprint.java#
答案 0 :(得分:0)
函数Parallel.For
需要ParallelForDelegate<CombineLocals>
类型的术语。这意味着你别无选择:你必须创建ParallelForDelegate<CombineLocals>
类型的东西。
如果您不想使用匿名内部类,则需要创建一个实现该接口的命名类。由于此类与原始内部类的范围不同,因此您需要找到一种传递适当值的方法。
public class CombineDelegate // Think of a better name.
implements ParallelForDelegate<CombineLocals>
{
private final BinaryMap source;
private final RectangleC area;
private final Point at;
private final CombineFunction function;
@Override
public CombineLocals delegate(int y, CombineLocals locals) {
// as in your anonymous inner class
}
@Override
public CombineLocals combineResults(CombineLocals result1,
CombineLocals result2) {
// as in your anonymous inner class
}
}
您还需要包含mutator方法和/或构造函数来设置私有字段的值。然后,您可以按如下方式实施Combine
方法。
void Combine(final BinaryMap source,
final RectangleC area,
final Point at,
final CombineFunction function) {
final int shift = (area.X & WordMask) - (at.X & WordMask);
// etc...
combineLocals.SrcVector = new int[vectorSize];
ParallelForDelegate<CombineLocals> delegate =
new CombineDelegate(source, area, at, function);
Parallel.For(0, area.Height, delegate, combineLocals);
}
如果在C ++中实现起来太难了,那么您需要找到另一种编写代码的方法。
注意:您不应该调用Java方法Combine
;改为称呼combine
。