use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class MixedDotExecutor method apply.
private static double apply(CompIntFloatVector v1, IntIntVector v2) {
double dotValue = 0.0;
if (v2.isDense()) {
int base = 0;
int[] v2Values = v2.getStorage().getValues();
for (IntFloatVector part : v1.getPartitions()) {
if (part.isDense()) {
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2Values[idx];
}
} else if (part.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getFloatValue() * v2Values[idx];
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2Values[idx];
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
dotValue += v1.get(idx) * entry.getIntValue();
}
} else if (v2.isSorted() && v1.size() > v2.size()) {
// v2 is sorted
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
dotValue += v1.get(idx) * v2Values[i];
}
} else {
int base = 0;
for (IntFloatVector part : v1.getPartitions()) {
if (part.isDense()) {
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < partValues.length; i++) {
int idx = base + i;
dotValue += partValues[i] * v2.get(idx);
}
} else if (part.isSparse()) {
ObjectIterator<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getFloatValue() * v2.get(idx);
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < partIndices.length; i++) {
int idx = base + partIndices[i];
dotValue += partValues[i] * v2.get(idx);
}
}
base += part.getDim();
}
}
return dotValue;
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class MixedBinaryOutAllExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntIntVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
if (part.isDense()) {
double[] partValue = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i;
resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
}
} else if (part.isSparse()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPart.size(); i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
double[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class MixedBinaryOutAllExecutor method apply.
private static Vector apply(CompIntFloatVector v1, IntLongVector v2, Binary op) {
IntFloatVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
long[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntFloatVector part : parts) {
IntFloatVectorStorage resPart = (IntFloatVectorStorage) resParts[k];
if (part.isDense()) {
float[] partValue = part.getStorage().getValues();
float[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i;
resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
}
} else if (part.isSparse()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2FloatMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getFloatValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPart.size(); i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
float[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntFloatVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
float[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
float[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntFloatVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntFloatSortedVectorStorage) {
resParts[i] = new IntFloatSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntFloatVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntFloatVector[] res = new IntFloatVector[parts.length];
int i = 0;
for (IntFloatVector part : parts) {
res[i] = new IntFloatVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntFloatVectorStorage) resParts[i]);
i++;
}
return new CompIntFloatVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class MixedBinaryOutAllExecutor method apply.
private static Vector apply(CompIntLongVector v1, IntLongVector v2, Binary op) {
IntLongVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
long[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntLongVector part : parts) {
IntLongVectorStorage resPart = (IntLongVectorStorage) resParts[k];
if (part.isDense()) {
long[] partValue = part.getStorage().getValues();
long[] resPartValues = resPart.getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i;
resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
}
} else if (part.isSparse()) {
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
for (int i = 0; i < part.getDim(); i++) {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
ObjectIterator<Int2LongMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getLongValue(), v2Values[idx + base]));
}
} else {
for (int i = 0; i < resPart.size(); i++) {
if (part.getStorage().hasKey(i)) {
resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
} else {
resPart.set(i, op.apply(0, v2Values[i + base]));
}
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
long[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntLongVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartIndices[i] = i;
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartIndices[i] = i;
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
} else {
long[] resPartValues = resPart.getValues();
if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int i = 0; i < part.getDim(); i++) {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntLongVectorStorage partStorage = part.getStorage();
for (int i = 0; i < resPartValues.length; i++) {
if (partStorage.hasKey(i)) {
resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
resPartValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (!op.isKeepStorage()) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].getStorage() instanceof IntLongSortedVectorStorage) {
resParts[i] = new IntLongSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
}
}
}
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
for (int i = 0; i < v1.getDim(); i++) {
int pidx = (int) (i / subDim);
int subidx = i % subDim;
if (v2.getStorage().hasKey(i)) {
((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
} else {
((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
}
}
}
IntLongVector[] res = new IntLongVector[parts.length];
int i = 0;
for (IntLongVector part : parts) {
res[i] = new IntLongVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntLongVectorStorage) resParts[i]);
i++;
}
return new CompIntLongVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class SimpleBinaryInAllExecutor method apply.
public static Vector apply(IntFloatVector v1, IntLongVector v2, Binary op) {
if (v1.isDense() && v2.isDense()) {
float[] resValues = v1.getStorage().getValues();
long[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < resValues.length; idx++) {
resValues[idx] = op.apply(resValues[idx], v2Values[idx]);
}
} else if (v1.isDense() && v2.isSparse()) {
float[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(v1.get(idx), entry.getLongValue());
}
} else {
IntLongVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isDense() && v2.isSorted()) {
float[] resValues = v1.getStorage().getValues();
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
long[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
} else {
IntLongVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
long[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
ObjectIterator<Int2FloatMap.Entry> iter = v1.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resValues[idx] = op.apply(entry.getFloatValue(), v2Values[idx]);
}
} else {
for (int i = 0; i < resValues.length; i++) {
if (v1.getStorage().hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isDense()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
long[] v2Values = v2.getStorage().getValues();
if (v1.size() < Constant.denseLoopThreshold * v1.getDim()) {
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(0, v2Values[i]);
}
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = op.apply(v1Values[i], v2Values[idx]);
}
} else {
IntFloatVectorStorage v1Storage = v1.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v1Storage.hasKey(i)) {
resValues[i] = op.apply(v1.get(i), v2Values[i]);
} else {
resValues[i] = op.apply(0, v2Values[i]);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2FloatMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getFloatValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getLongValue());
}
}
} else {
IntLongVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSparse() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
ObjectIterator<Int2FloatMap.Entry> iter1 = v1.getStorage().entryIterator();
while (iter1.hasNext()) {
Int2FloatMap.Entry entry = iter1.next();
int idx = entry.getIntKey();
resValues[idx] = entry.getFloatValue();
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
int[] v2Indices = v2.getStorage().getIndices();
long[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), v2Values[i]);
}
}
} else {
IntLongVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSparse()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
resValues[idx] = v1Values[i];
}
if (v2.size() < Constant.denseLoopThreshold * v2.getDim()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = op.apply(resValues[i], 0);
}
ObjectIterator<Int2LongMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1.getStorage().hasKey(idx)) {
resValues[idx] = op.apply(v1.get(idx), entry.getLongValue());
}
}
} else {
IntLongVectorStorage v2Storage = v2.getStorage();
for (int i = 0; i < resValues.length; i++) {
if (v2Storage.hasKey(i)) {
resValues[i] = op.apply(resValues[i], v2.get(i));
} else {
resValues[i] = op.apply(resValues[i], 0);
}
}
}
v1.setStorage(newStorage);
}
} else if (v1.isSorted() && v2.isSorted()) {
if (op.isKeepStorage()) {
throw new AngelException("operation is not support!");
} else {
IntFloatVectorStorage newStorage = v1.getStorage().emptyDense();
float[] resValues = newStorage.getValues();
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
int[] v1Indices = v1.getStorage().getIndices();
float[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
long[] v2Values = v2.getStorage().getValues();
if (!op.isCompare()) {
for (int i = 0; i < resValues.length; i++) {
resValues[i] = Float.NaN;
}
}
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
resValues[v1Indices[v1Pointor]] = op.apply(v1Values[v1Pointor], 0);
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
resValues[v2Indices[v2Pointor]] = op.apply(0, v2Values[v2Pointor]);
v2Pointor++;
}
}
v1.setStorage(newStorage);
}
} else {
throw new AngelException("The operation is not support!");
}
return v1;
}
Aggregations