use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class SimpleBinaryInZAExecutor method apply.
private static Vector apply(LongDoubleVector v1, LongFloatVector v2, Binary op) {
LongDoubleVectorStorage newStorage = (LongDoubleVectorStorage) StorageSwitch.apply(v1, v2, op);
if (v1.isSparse() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongFloatVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
}
}
} else {
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getFloatValue()));
}
}
}
} else {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongFloatVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else {
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongFloatVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
}
} else if (v1.isSparse() && v2.isSorted()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
LongDoubleVectorStorage storage = v1.getStorage();
long size = v2.size();
for (int i = 0; i < size; i++) {
long idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongFloatVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
LongDoubleVectorStorage storage = v1.getStorage();
long size = v2.size();
for (int i = 0; i < size; i++) {
long idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else {
// preferred dense
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongFloatVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
} else if (v1.isSorted() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v2.size
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getFloatValue()));
}
}
} else {
// sparse preferred
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getFloatValue()));
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v1.size
long[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongFloatVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongFloatVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
ObjectIterator<Long2FloatMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2FloatMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getFloatValue()));
}
}
} else {
// dense preferred
if (op.isKeepStorage()) {
// sorted preferred v1.size
long[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongFloatVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongFloatVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
}
} else if (v1.isSorted() && v2.isSorted()) {
int v1Pointor = 0;
int v2Pointor = 0;
long size1 = v1.size();
long size2 = v2.size();
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// sparse preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// sparse preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else {
if (op.isKeepStorage()) {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
}
} else {
throw new AngelException("The operation is not support!");
}
v1.setStorage(newStorage);
return v1;
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class SimpleBinaryInZAExecutor method apply.
private static Vector apply(IntDoubleVector v1, IntDoubleVector v2, Binary op) {
IntDoubleVectorStorage newStorage = (IntDoubleVectorStorage) StorageSwitch.apply(v1, v2, op);
if (v1.isDense() && v2.isDense()) {
double[] v1Values = newStorage.getValues();
double[] v2Values = v2.getStorage().getValues();
for (int idx = 0; idx < v1Values.length; idx++) {
v1Values[idx] = op.apply(v1Values[idx], v2Values[idx]);
}
return v1;
} else if (v1.isDense() && v2.isSparse()) {
double[] v1Values = newStorage.getValues();
if (v2.size() < Constant.sparseDenseStorageThreshold * v2.getDim() || v1.getDim() < Constant.denseStorageThreshold) {
// slower but memory efficient, for small vector only
IntDoubleVectorStorage v2storage = v2.getStorage();
for (int i = 0; i < v1Values.length; i++) {
if (v2storage.hasKey(i)) {
v1Values[i] = op.apply(v1Values[i], v2.get(i));
}
}
} else {
// faster but not memory efficient
double[] newValues = newStorage.getValues();
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
newValues[idx] = op.apply(v1Values[idx], entry.getDoubleValue());
}
}
return v1;
} else if (v1.isDense() && v2.isSorted()) {
if ((v2.isSparse() && v2.getSize() >= Constant.sparseDenseStorageThreshold * v2.dim()) || (v2.isSorted() && v2.getSize() >= Constant.sortedDenseStorageThreshold * v2.dim())) {
// dense preferred, KeepStorage is guaranteed
double[] newValues = newStorage.getValues();
double[] v1Values = v1.getStorage().getValues();
int[] vIndices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = vIndices[i];
newValues[idx] = op.apply(v1Values[idx], v2Values[i]);
}
} else {
if (op.isKeepStorage()) {
double[] newValues = newStorage.getValues();
double[] v1Values = v1.getStorage().getValues();
int[] vIndices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = vIndices[i];
newValues[idx] = op.apply(v1Values[idx], v2Values[i]);
}
} else {
double[] v1Values = v1.getStorage().getValues();
int[] vIndices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = vIndices[i];
newStorage.set(idx, op.apply(v1Values[idx], v2Values[i]));
}
}
}
} else if (v1.isSparse() && v2.isDense()) {
if (op.isKeepStorage() || v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
double[] v2Values = v2.getStorage().getValues();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx]));
}
} else {
// dense preferred
double[] newValues = newStorage.getValues();
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
double[] v2Values = v2.getStorage().getValues();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
newValues[idx] = op.apply(entry.getDoubleValue(), v2Values[idx]);
}
}
} else if (v1.isSparse() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getDoubleValue()));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
IntDoubleVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getDoubleValue()));
}
}
} else {
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getDoubleValue()));
}
}
}
} else {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
IntDoubleVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else {
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
IntDoubleVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
}
} else if (v1.isSparse() && v2.isSorted()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
IntDoubleVectorStorage storage = v1.getStorage();
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
IntDoubleVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
IntDoubleVectorStorage storage = v1.getStorage();
int size = v2.size();
for (int i = 0; i < size; i++) {
int idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else {
// preferred dense
ObjectIterator<Int2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
IntDoubleVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
} else if (v1.isSorted() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v2.size
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getDoubleValue()));
}
}
} else {
// sparse preferred
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getDoubleValue()));
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v1.size
int[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
IntDoubleVectorStorage storage = v2.getStorage();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
IntDoubleVectorStorage storage = v2.getStorage();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
ObjectIterator<Int2DoubleMap.Entry> iter = v2.getStorage().entryIterator();
IntDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getDoubleValue()));
}
}
} else {
// dense preferred
if (op.isKeepStorage()) {
// sorted preferred v1.size
int[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
IntDoubleVectorStorage storage = v2.getStorage();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
// dense preferred
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
IntDoubleVectorStorage storage = v2.getStorage();
int size = v1.size();
for (int i = 0; i < size; i++) {
int idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
}
} else if (v1.isSorted() && v2.isSorted()) {
int v1Pointor = 0;
int v2Pointor = 0;
int size1 = v1.size();
int size2 = v2.size();
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new IntDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// sparse preferred
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new IntDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// sparse preferred
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new IntDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// dense preferred
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else {
if (op.isKeepStorage()) {
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
int[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new IntDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// dense preferred
int[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
int[] v2Indices = v2.getStorage().getIndices();
double[] v2Values = v2.getStorage().getValues();
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
}
} else {
throw new AngelException("The operation is not support!");
}
v1.setStorage(newStorage);
return v1;
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class SimpleBinaryInZAExecutor method apply.
private static Vector apply(LongDoubleVector v1, LongIntVector v2, Binary op) {
LongDoubleVectorStorage newStorage = (LongDoubleVectorStorage) StorageSwitch.apply(v1, v2, op);
if (v1.isSparse() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongIntVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
}
}
} else {
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1.get(idx), entry.getIntValue()));
}
}
}
} else {
// preferred dense
if (op.isKeepStorage()) {
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongIntVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else {
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongIntVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
}
} else if (v1.isSparse() && v2.isSorted()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sparseDenseStorageThreshold * v2.dim()) {
// sparse preferred, keep storage guaranteed
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
LongDoubleVectorStorage storage = v1.getStorage();
long size = v2.size();
for (int i = 0; i < size; i++) {
long idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sparseDenseStorageThreshold * v1.dim()) {
// sparse preferred, keep storage guaranteed
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongIntVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sparseDenseStorageThreshold * v2.dim()) {
// preferred dense
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
LongDoubleVectorStorage storage = v1.getStorage();
long size = v2.size();
for (int i = 0; i < size; i++) {
long idx = v2Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(storage.get(idx), v2Values[i]));
}
}
} else {
// preferred dense
ObjectIterator<Long2DoubleMap.Entry> iter = v1.getStorage().entryIterator();
LongIntVectorStorage v2storage = v2.getStorage();
while (iter.hasNext()) {
Long2DoubleMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v2storage.hasKey(idx)) {
newStorage.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx)));
}
}
}
} else if (v1.isSorted() && v2.isSparse()) {
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v2.size
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
}
}
} else {
// sparse preferred
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
// sorted preferred v1.size
long[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongIntVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongIntVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
LongDoubleVectorStorage v1storage = v1.getStorage();
while (iter.hasNext()) {
Long2IntMap.Entry entry = iter.next();
long idx = entry.getLongKey();
if (v1storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1storage.get(idx), entry.getIntValue()));
}
}
} else {
// dense preferred
if (op.isKeepStorage()) {
// sorted preferred v1.size
long[] resIndices = newStorage.getIndices();
double[] resValues = newStorage.getValues();
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongIntVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
resIndices[i] = idx;
resValues[i] = op.apply(v1Values[i], storage.get(idx));
}
}
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
LongIntVectorStorage storage = v2.getStorage();
long size = v1.size();
for (int i = 0; i < size; i++) {
long idx = v1Indices[i];
if (storage.hasKey(idx)) {
newStorage.set(idx, op.apply(v1Values[i], storage.get(idx)));
}
}
}
}
} else if (v1.isSorted() && v2.isSorted()) {
int v1Pointor = 0;
int v2Pointor = 0;
long size1 = v1.size();
long size2 = v2.size();
if (v1.getSize() >= v2.getSize() && v2.getSize() <= Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// sparse preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() <= v2.getSize() && v1.getSize() <= Constant.sortedDenseStorageThreshold * v1.dim()) {
if (op.isKeepStorage()) {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// sparse preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else if (v1.getSize() > v2.getSize() && v2.getSize() > Constant.sortedDenseStorageThreshold * v2.dim()) {
if (op.isKeepStorage()) {
// sorted v2.size
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v2Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v2Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v2.size(), resIndices, resValues);
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
} else {
if (op.isKeepStorage()) {
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
long[] resIndices = ArrayCopy.copy(v1Indices);
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
resValues[v1Pointor] = op.apply(v1Values[v1Pointor], v2Values[v2Pointor]);
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
newStorage = new LongDoubleSortedVectorStorage(v1.getDim(), (int) v1.size(), resIndices, resValues);
} else {
// dense preferred
long[] v1Indices = v1.getStorage().getIndices();
double[] v1Values = v1.getStorage().getValues();
long[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
double[] resValues = newStorage.getValues();
while (v1Pointor < size1 && v2Pointor < size2) {
if (v1Indices[v1Pointor] == v2Indices[v2Pointor]) {
newStorage.set(v1Indices[v1Pointor], op.apply(v1Values[v1Pointor], v2Values[v2Pointor]));
v1Pointor++;
v2Pointor++;
} else if (v1Indices[v1Pointor] < v2Indices[v2Pointor]) {
v1Pointor++;
} else {
// v1Indices[v1Pointor] > v2Indices[v2Pointor]
v2Pointor++;
}
}
}
}
} else {
throw new AngelException("The operation is not support!");
}
v1.setStorage(newStorage);
return v1;
}
use of it.unimi.dsi.fastutil.objects.ObjectIterator in project angel by Tencent.
the class MixedBinaryOutNonZAExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntFloatVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
float[] v2Values = v2.getStorage().getValues();
int base = 0, k = 0;
for (IntDoubleVector part : parts) {
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
double[] newValues = resPart.getValues();
if (part.isDense()) {
double[] partValue = part.getStorage().getValues();
for (int i = 0; i < partValue.length; i++) {
int idx = i + base;
newValues[i] = op.apply(partValue[i], v2Values[idx]);
}
} 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 < newValues.length; 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 dim = part.getDim();
int[] resIndices = resPart.getIndices();
double[] resValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int i = 0; i < dim; i++) {
resIndices[i] = i;
resValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
resValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
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++) {
newValues[i] = op.apply(0, v2Values[i + base]);
}
int size = part.size();
for (int i = 0; i < size; i++) {
int idx = partIndices[i];
newValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
}
} else {
IntDoubleVectorStorage partStorage = part.getStorage();
for (int i = 0; i < newValues.length; i++) {
if (partStorage.hasKey(i)) {
newValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
} else {
newValues[i] = op.apply(0, v2Values[i + base]);
}
}
}
}
}
base += part.getDim();
k++;
}
} else {
if (v2.isSparse()) {
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();
ObjectIterator<Int2FloatMap.Entry> iter = v2.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int gidx = entry.getIntKey();
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getFloatValue()));
}
} else {
// sorted
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();
int[] v2Indices = v2.getStorage().getIndices();
float[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int gidx = v2Indices[i];
int pidx = (int) (gidx / subDim);
int subidx = gidx % subDim;
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
}
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 MixedDotExecutor method apply.
private static double apply(CompIntLongVector v1, IntIntVector v2) {
double dotValue = 0.0;
if (v2.isDense()) {
int base = 0;
int[] v2Values = v2.getStorage().getValues();
for (IntLongVector part : v1.getPartitions()) {
if (part.isDense()) {
long[] 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<Int2LongMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getLongValue() * v2Values[idx];
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
long[] 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 (IntLongVector part : v1.getPartitions()) {
if (part.isDense()) {
long[] 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<Int2LongMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = base + entry.getIntKey();
dotValue += entry.getLongValue() * v2.get(idx);
}
} else {
// isSorted
int[] partIndices = part.getStorage().getIndices();
long[] 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;
}
Aggregations