Search in sources :

Example 26 with FasterList

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;
}
Also used : FasterList(jcog.list.FasterList)

Example 27 with FasterList

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);
}
Also used : PLink(jcog.pri.PLink) DefaultHijackBag(jcog.bag.impl.hijack.DefaultHijackBag) HijackBag(jcog.bag.impl.HijackBag) ArrayBag(jcog.bag.impl.ArrayBag) FasterList(jcog.list.FasterList)

Example 28 with FasterList

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;
}
Also used : FasterList(jcog.list.FasterList) FasterList(jcog.list.FasterList)

Example 29 with FasterList

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;
}
Also used : FasterList(jcog.list.FasterList) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 30 with FasterList

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();
}
Also used : FasterList(jcog.list.FasterList) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException)

Aggregations

FasterList (jcog.list.FasterList)48 Term (nars.term.Term)17 List (java.util.List)7 Truth (nars.truth.Truth)5 Pair (org.eclipse.collections.api.tuple.Pair)5 Nullable (org.jetbrains.annotations.Nullable)5 Map (java.util.Map)4 Predicate (java.util.function.Predicate)4 NAR (nars.NAR)4 Task (nars.Task)4 LongObjectPair (org.eclipse.collections.api.tuple.primitive.LongObjectPair)4 java.util (java.util)3 Supplier (java.util.function.Supplier)3 Util (jcog.Util)3 MapNodeGraph (jcog.data.graph.MapNodeGraph)3 NALTask (nars.task.NALTask)3 Bool (nars.term.atom.Bool)3 Test (org.junit.jupiter.api.Test)3 RoaringBitmap (org.roaringbitmap.RoaringBitmap)3 MultimapBuilder (com.google.common.collect.MultimapBuilder)2