yunfengzhou-hub commented on a change in pull request #37: URL: https://github.com/apache/flink-ml/pull/37#discussion_r764718961
########## File path: flink-ml-core/src/main/java/org/apache/flink/ml/linalg/typeinfo/SparseVectorSerializer.java ########## @@ -0,0 +1,146 @@ +/* + * 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 org.apache.flink.ml.linalg.typeinfo; + +import org.apache.flink.api.common.typeutils.SimpleTypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; +import org.apache.flink.api.common.typeutils.base.TypeSerializerSingleton; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataOutputView; +import org.apache.flink.ml.linalg.SparseVector; + +import java.io.IOException; +import java.util.Arrays; + +/** Specialized serializer for {@code Sparse}. */ +public final class SparseVectorSerializer extends TypeSerializerSingleton<SparseVector> { + + private static final long serialVersionUID = 1L; + + private static final SparseVectorSerializer INSTANCE = new SparseVectorSerializer(); + + @Override + public boolean isImmutableType() { + return false; + } + + @Override + public SparseVector createInstance() { + return new SparseVector(); + } + + @Override + public SparseVector copy(SparseVector from) { + return new SparseVector( + from.n, + Arrays.copyOf(from.indices, from.indices.length), + Arrays.copyOf(from.values, from.values.length)); + } + + @Override + public SparseVector copy(SparseVector from, SparseVector reuse) { + if (from.values.length == reuse.values.length && from.n == reuse.n) { + System.arraycopy(from.values, 0, reuse.values, 0, from.values.length); + System.arraycopy(from.indices, 0, reuse.indices, 0, from.indices.length); + return reuse; + } + return copy(from); + } + + @Override + public int getLength() { + return -1; + } + + @Override + public void serialize(SparseVector vector, DataOutputView target) throws IOException { + if (vector == null) { + throw new IllegalArgumentException("The vector must not be null."); + } + + target.writeInt(vector.n); + final int len = vector.values.length; + target.writeInt(len); + for (int i = 0; i < len; i++) { + target.writeInt(vector.indices[i]); + target.writeDouble(vector.values[i]); + } + } + + @Override + public SparseVector deserialize(DataInputView source) throws IOException { + int n = source.readInt(); + int len = source.readInt(); + int[] indices = new int[len]; + double[] values = new double[len]; + for (int i = 0; i < len; i++) { + indices[i] = source.readInt(); + values[i] = source.readDouble(); + } + return new SparseVector(n, indices, values); + } + + @Override + public SparseVector deserialize(SparseVector reuse, DataInputView source) throws IOException { + int n = source.readInt(); + int len = source.readInt(); + if (reuse.n == n && reuse.values.length == len) { + for (int i = 0; i < len; i++) { + reuse.indices[i] = source.readInt(); + reuse.values[i] = source.readDouble(); + } + return reuse; + } + + int[] indices = new int[len]; + double[] values = new double[len]; + for (int i = 0; i < len; i++) { + indices[i] = source.readInt(); Review comment: OK. I'll add one. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
