Search in sources :

Example 1 with ArrayBag

use of jcog.bag.impl.ArrayBag 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 2 with ArrayBag

use of jcog.bag.impl.ArrayBag in project narchy by automenta.

the class BagTest method fillLinear.

/**
 * fill it exactly to capacity
 */
public static void fillLinear(Bag<PLink<String>, PLink<String>> bag, int c) {
    assertTrue(bag.isEmpty());
    // insert biggest items first
    for (int i = c - 1; i >= 0; i--) {
        // midpoint of (i,i+1)
        PLink inserted = bag.put(new PLink(i + "x", (i + 0.5f) / c));
        if (inserted == null) {
            fail("");
        }
    }
    bag.commit(null);
    assertEquals(c, bag.size());
    assertEquals(0.5f / c, bag.priMin(), 0.03f);
    // no pressure should have been applied because capacity was only reached after the last put
    assertEquals(1 - 1f / (c * 2f), bag.priMax(), 0.03f);
    if (bag instanceof ArrayBag)
        assertSorted((ArrayBag) bag);
}
Also used : PLink(jcog.pri.PLink) ArrayBag(jcog.bag.impl.ArrayBag)

Example 3 with ArrayBag

use of jcog.bag.impl.ArrayBag in project narchy by automenta.

the class BagTest method testRemoveByKey.

public static void testRemoveByKey(Bag<String, PriReference<String>> a) {
    a.put(new PLink("x", 0.1f));
    a.commit();
    assertEquals(1, a.size());
    a.remove("x");
    a.commit();
    assertEquals(0, a.size());
    assertTrue(a.isEmpty());
    if (a instanceof ArrayBag) {
        assertTrue(((ArrayBag) a).listCopy().isEmpty());
        assertTrue(((ArrayBag) a).keySet().isEmpty());
    }
}
Also used : PLink(jcog.pri.PLink) ArrayBag(jcog.bag.impl.ArrayBag)

Example 4 with ArrayBag

use of jcog.bag.impl.ArrayBag in project narchy by automenta.

the class BagTest method populate.

public static void populate(Bag<String, PriReference<String>> a, Random rng, int count, int dimensionality, float minPri, float maxPri, float minQua, float maxQua) {
    float dPri = maxPri - minPri;
    for (int i = 0; i < count; i++) {
        a.put(new PLink("x" + rng.nextInt(dimensionality), rng.nextFloat() * dPri + minPri));
    }
    a.commit(null);
    if (a instanceof ArrayBag)
        assertSorted((ArrayBag) a);
}
Also used : PLink(jcog.pri.PLink) ArrayBag(jcog.bag.impl.ArrayBag)

Aggregations

ArrayBag (jcog.bag.impl.ArrayBag)4 PLink (jcog.pri.PLink)4 HijackBag (jcog.bag.impl.HijackBag)1 DefaultHijackBag (jcog.bag.impl.hijack.DefaultHijackBag)1 FasterList (jcog.list.FasterList)1