use of jcog.list.FasterList in project narchy by automenta.
the class Node method toList.
default FasterList<V> toList() {
int s = size();
FasterList f = new FasterList(s);
Object[] ff = f.array();
for (int i = 0; i < s; i++) {
ff[i] = get(i);
}
return f;
}
use of jcog.list.FasterList in project narchy by automenta.
the class BagTest method testPutMinMaxAndUniqueness.
public static void testPutMinMaxAndUniqueness(Bag<Integer, PriReference<Integer>> a) {
float pri = 0.5f;
// insert enough to fully cover all slots. strings have bad hashcode when input iteratively so this may need to be a high multiple
int n = a.capacity() * 16;
for (int i = 0; i < n; i++) {
a.put(new PLink((i), pri));
}
// commit but dont forget
a.commit(null);
assertEquals(a.capacity(), a.size());
if (a instanceof ArrayBag)
assertSorted((ArrayBag) a);
// a.print();
// System.out.println(n + " " + a.size());
List<Integer> keys = new FasterList(a.capacity());
a.forEachKey(keys::add);
assertEquals(a.size(), keys.size());
assertEquals(new HashSet(keys).size(), keys.size());
assertEquals(pri, a.priMin(), 0.01f);
assertEquals(a.priMin(), a.priMax(), 0.08f);
if (a instanceof HijackBag)
assertTrue(((HijackBag) a).density() > 0.75f);
}
use of jcog.list.FasterList in project narchy by automenta.
the class DecisionTree method explanations.
/**
* requires V to be Comparable
*/
public SortedMap<Node.LeafNode<V>, List<ObjectBooleanPair<Node<V>>>> explanations() {
SortedMap<Node.LeafNode<V>, List<ObjectBooleanPair<Node<V>>>> explanations = new TreeMap();
explain((path, result) -> {
explanations.put(result, new FasterList(path));
});
return explanations;
}
use of jcog.list.FasterList in project narchy by automenta.
the class GraphMeter method weakly.
// --------------------------------------------------------------------
/**
* Returns the weakly connected cluster indexes with size as a value.
* Cluster membership can be seen from the content of the array {@link #color};
* each node has the cluster index as color. The cluster indexes carry no
* information; we guarantee only that different clusters have different indexes.
*/
public List<IntHashSet> weakly(Graph g) {
int size = g.size();
this.g = g;
if (cluster == null)
cluster = new IntHashSet();
if (color == null || color.length < size)
color = new int[size];
for (int i = 0; i < size; ++i) color[i] = WHITE;
int actCluster = 0;
int j;
for (int i = 0; i < size; ++i) {
if (color[i] != WHITE)
continue;
cluster.clear();
// dfs is recursive, for large graphs not ok
bfs(i);
--actCluster;
for (j = 0; j < size; ++j) {
int cj = color[j];
if (cj == BLACK || cluster.contains(cj))
color[j] = actCluster;
}
}
if (actCluster == 0)
return Collections.emptyList();
actCluster = -actCluster;
List<IntHashSet> x = new FasterList(actCluster);
for (int i = 0; i < actCluster; i++) {
x.add(new IntHashSet(1));
}
for (j = 0; j < size; ++j) {
x.get(-color[j] - 1).add(j);
}
return x;
}
use of jcog.list.FasterList in project narchy by automenta.
the class FileCache method fileCache.
public static <X> Stream<X> fileCache(File f, String baseName, Supplier<Stream<X>> o, BiConsumer<Stream<X>, DataOutput> encoder, Function<DataInput, X> decoder, Logger logger) throws IOException {
long lastModified = f.lastModified();
long size = f.length();
String suffix = '_' + f.getName() + '_' + lastModified + '_' + size;
List<X> buffer = new FasterList(1024);
String tempDir = Util.tempDir();
File cached = new File(tempDir, baseName + suffix);
if (cached.exists()) {
// try read
try {
FileInputStream ff = new FileInputStream(cached);
DataInputStream din = new DataInputStream(new BufferedInputStream(ff));
while (din.available() > 0) {
buffer.add(decoder.apply(din));
}
din.close();
logger.warn("cache loaded {}: ({} bytes, from {})", cached.getAbsolutePath(), cached.length(), new Date(cached.lastModified()));
return buffer.stream();
} catch (Exception e) {
logger.warn("{}, regenerating..", e);
// continue below
}
}
// save
buffer.clear();
Stream<X> instanced = o.get();
DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(cached.getAbsolutePath())));
encoder.accept(instanced.peek(buffer::add), dout);
dout.close();
logger.warn("cache saved {}: ({} bytes)", cached.getAbsolutePath(), dout.size());
return buffer.stream();
}
Aggregations