lindong28 commented on a change in pull request #27: URL: https://github.com/apache/flink-ml/pull/27#discussion_r749956004
########## File path: flink-ml-api/src/main/java/org/apache/flink/ml/linalg/typeinfo/DenseVectorSerializer.java ########## @@ -0,0 +1,133 @@ +/* + * 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.DenseVector; + +import java.io.IOException; +import java.util.Arrays; + +/** Specialized serializer for {@code DenseVector}. */ +public final class DenseVectorSerializer extends TypeSerializerSingleton<DenseVector> { + + private static final long serialVersionUID = 1L; + + private static final double[] EMPTY = new double[0]; + + private static final DenseVectorSerializer INSTANCE = new DenseVectorSerializer(); + + @Override + public boolean isImmutableType() { + return false; + } + + @Override + public DenseVector createInstance() { + return new DenseVector(EMPTY); + } + + @Override + public DenseVector copy(DenseVector from) { + return new DenseVector(Arrays.copyOf(from.values, from.values.length)); + } + + @Override + public DenseVector copy(DenseVector from, DenseVector reuse) { + if (from.values.length == reuse.values.length) { + System.arraycopy(from.values, 0, reuse.values, 0, from.values.length); + return reuse; + } + return copy(from); + } + + @Override + public int getLength() { + return -1; + } + + @Override + public void serialize(DenseVector vector, DataOutputView target) throws IOException { + if (vector == null) { + throw new IllegalArgumentException("The vector must not be null."); + } + + final int len = vector.values.length; + target.writeInt(len); + for (int i = 0; i < len; i++) { + target.writeDouble(vector.get(i)); + } + } + + @Override + public DenseVector deserialize(DataInputView source) throws IOException { + int len = source.readInt(); + double[] values = new double[len]; + for (int i = 0; i < len; i++) { + values[i] = source.readDouble(); + } + return new DenseVector(values); + } + + @Override + public DenseVector deserialize(DenseVector reuse, DataInputView source) throws IOException { + int len = source.readInt(); + if (len == reuse.values.length) { + for (int i = 0; i < len; i++) { + reuse.values[i] = source.readDouble(); + } + return reuse; + } + + double[] values = new double[len]; Review comment: I agree. It is updated as suggested. -- 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]
