Last active
March 9, 2017 10:28
-
-
Save myui/2ac2bee6571934912a55dd1223b61011 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (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 | |
* | |
* 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 hivemall.matrix; | |
import hivemall.utils.lang.Primitives; | |
import javax.annotation.Nonnegative; | |
public final class SortedListMatrix extends AbstractMatrix { | |
private final long[] indicies; | |
private final double[] values; | |
private final | |
public SortedListMatrix() { | |
super(); | |
} | |
@Override | |
public boolean isSparse() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean isRowMajorMatrix() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean isColumnMajorMatrix() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean readOnly() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean swappable() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public int numRows() { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public int numColumns() { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public int numColumns(int row) { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public double[] getRow(int index) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public double[] getRow(int index, double[] dst) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public double get(int row, int col, double defaultValue) { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public void set(int row, int col, double value) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public double getAndSet(int row, int col, double value) { | |
// TODO Auto-generated method stub | |
return 0; | |
} | |
@Override | |
public void swap(int row1, int row2) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInRow(int row, VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachNonZeroInRow(int row, VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInColumn(int col, VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void eachInNonZeroColumn(int col, VectorProcedure procedure) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public MatrixBuilder builder() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Nonnegative | |
public static long index(@Nonnegative final int high, @Nonnegative final int low) { | |
return Primitives.toLong(high, low); | |
} | |
} |
This file contains hidden or 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
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (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 | |
* | |
* 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 hivemall.matrix; | |
import hivemall.utils.collections.lists.DoubleArrayList; | |
import hivemall.utils.collections.lists.LongArrayList; | |
import hivemall.utils.lang.ArrayUtils; | |
import hivemall.utils.lang.Preconditions; | |
import hivemall.utils.lang.Primitives; | |
import javax.annotation.Nonnegative; | |
import javax.annotation.Nonnull; | |
public final class SortedListMatrixBuilder extends MatrixBuilder { | |
@Nonnull | |
private/* final */LongArrayList indicies; | |
@Nonnull | |
private/* final */DoubleArrayList values; | |
private final boolean rowMajor; | |
private int row; | |
private int maxNumColumns; | |
public SortedListMatrixBuilder(boolean rowMajor) { | |
super(); | |
this.indicies = new LongArrayList(1024); | |
this.values = new DoubleArrayList(1024); | |
this.rowMajor = rowMajor; | |
this.row = 0; | |
this.maxNumColumns = 0; | |
} | |
@Override | |
public MatrixBuilder nextRow() { | |
row++; | |
return this; | |
} | |
@Override | |
public MatrixBuilder nextColumn(@Nonnegative final int col, final double value) { | |
long idx = index(row, col); | |
indicies.add(idx); | |
values.add(value); | |
this.maxNumColumns = Math.max(col + 1, maxNumColumns); | |
return this; | |
} | |
private long index(final int row, final int col) { | |
if (rowMajor) { | |
return SortedListMatrix.index(row, col); | |
} else { | |
return SortedListMatrix.index(col, row); | |
} | |
} | |
@Override | |
public SortedListMatrix buildMatrix() { | |
if (indicies.isEmpty()) { | |
throw new IllegalStateException("No element in the matrix"); | |
} | |
long[] indiciesArray = indicies.toArray(); | |
this.indicies = null; | |
double[] valuesArray = values.toArray(); | |
this.values = null; | |
// sort by row/column major | |
ArrayUtils.sort(indiciesArray, valuesArray); | |
int[] pointers = getPointers(indiciesArray, rowMajor ? row : maxNumColumns); | |
return null; | |
} | |
@Nonnull | |
private static int[] getPointers(@Nonnull final long[] indicies, @Nonnegative final int size) { | |
Preconditions.checkArgument(size <= indicies.length); | |
final int[] pointers = new int[size]; | |
int prevKey = Primitives.getHigh(indicies[0]); | |
pointers[0] = 0; | |
for (int i = 1, j = 1; i < indicies.length; i++) { | |
final int key = Primitives.getHigh(indicies[i]); | |
if (key != prevKey) { | |
pointers[j++] = i; | |
} | |
prevKey = key; | |
} | |
return pointers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment