org.ranksys.javafm.data.SimpleFMData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JavaFM Show documentation
Show all versions of JavaFM Show documentation
Java 8 Factorization Machines Library.
The newest version!
/*
* Copyright (C) 2016 RankSys http://ranksys.org
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.ranksys.javafm.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
import org.ranksys.javafm.FMInstance;
/**
* Subclass of ArrayList implementing the FMData interface.
*
* @author Saúl Vargas ([email protected])
*/
public class SimpleFMData implements FMData {
private final List list;
private final int numFeatures;
private final Random rnd;
/**
* Constructor.
*
* @param numFeatures number of features
* @param rnd random number generator
* @param instances list of instances that compose the data
*/
public SimpleFMData(int numFeatures, Random rnd, List instances) {
this.numFeatures = numFeatures;
this.rnd = rnd;
this.list = new ArrayList<>(instances);
}
/**
* Constructor.
*
* @param numFeatures number of features
*/
public SimpleFMData(int numFeatures) {
this(numFeatures, new Random(), new ArrayList<>());
}
public void add(FMInstance x) {
list.add(x);
}
@Override
public int numInstances() {
return list.size();
}
@Override
public int numFeatures() {
return numFeatures;
}
@Override
public void shuffle() {
Collections.shuffle(list, rnd);
}
@Override
public Stream extends FMInstance> stream() {
return list.stream();
}
}