Search in sources :

Example 1 with HashBag

use of org.eclipse.collections.impl.bag.mutable.HashBag in project eclipse-collections by eclipse.

the class RichIterableTestCase method RichIterable_iterationOrder.

@Test
default void RichIterable_iterationOrder() {
    MutableCollection<Integer> expectedIterationOrder = this.expectedIterationOrder();
    Procedure<Object> noop = each -> {
    };
    MutableCollection<Integer> selectIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().select(selectIterationOrder::add).forEach(noop);
    assertEquals(expectedIterationOrder, selectIterationOrder);
    MutableCollection<Integer> selectTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().select(selectTargetIterationOrder::add, new HashBag<>());
    assertEquals(expectedIterationOrder, selectTargetIterationOrder);
    MutableCollection<Integer> selectWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().selectWith((each, param) -> selectWithIterationOrder.add(each), null).forEach(noop);
    assertEquals(expectedIterationOrder, selectWithIterationOrder);
    MutableCollection<Integer> selectWithTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().selectWith((each, param) -> selectWithTargetIterationOrder.add(each), null, new HashBag<>());
    assertEquals(expectedIterationOrder, selectWithTargetIterationOrder);
    MutableCollection<Integer> rejectIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().reject(rejectIterationOrder::add).forEach(noop);
    assertEquals(expectedIterationOrder, rejectIterationOrder);
    MutableCollection<Integer> rejectTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().reject(rejectTargetIterationOrder::add, new HashBag<>());
    assertEquals(expectedIterationOrder, rejectTargetIterationOrder);
    MutableCollection<Integer> rejectWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().rejectWith((each, param) -> rejectWithIterationOrder.add(each), null).forEach(noop);
    assertEquals(expectedIterationOrder, rejectWithIterationOrder);
    MutableCollection<Integer> rejectWithTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().rejectWith((each, param) -> rejectWithTargetIterationOrder.add(each), null, new HashBag<>());
    assertEquals(expectedIterationOrder, rejectWithTargetIterationOrder);
    MutableCollection<Integer> partitionIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().partition(partitionIterationOrder::add);
    assertEquals(expectedIterationOrder, partitionIterationOrder);
    MutableCollection<Integer> partitionWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().partitionWith((each, param) -> partitionWithIterationOrder.add(each), null);
    assertEquals(expectedIterationOrder, partitionWithIterationOrder);
    MutableCollection<Integer> collectIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collect(collectIterationOrder::add).forEach(noop);
    assertEquals(expectedIterationOrder, collectIterationOrder);
    MutableCollection<Integer> collectTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collect(collectTargetIterationOrder::add, new HashBag<>());
    assertEquals(expectedIterationOrder, collectTargetIterationOrder);
    MutableCollection<Integer> collectWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectWith((each, param) -> collectWithIterationOrder.add(each), null).forEach(noop);
    assertEquals(expectedIterationOrder, collectWithIterationOrder);
    MutableCollection<Integer> collectWithTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectWith((each, param) -> collectWithTargetIterationOrder.add(each), null, new HashBag<>());
    assertEquals(expectedIterationOrder, collectWithTargetIterationOrder);
    MutableCollection<Integer> collectIfPredicateIterationOrder = this.newMutableForFilter();
    MutableCollection<Integer> collectIfFunctionIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectIf(collectIfPredicateIterationOrder::add, collectIfFunctionIterationOrder::add).forEach(noop);
    assertEquals(expectedIterationOrder, collectIfPredicateIterationOrder);
    assertEquals(expectedIterationOrder, collectIfFunctionIterationOrder);
    MutableCollection<Integer> collectIfPredicateTargetIterationOrder = this.newMutableForFilter();
    MutableCollection<Integer> collectIfFunctionTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectIf(collectIfPredicateTargetIterationOrder::add, collectIfFunctionTargetIterationOrder::add, new HashBag<>());
    assertEquals(expectedIterationOrder, collectIfPredicateTargetIterationOrder);
    assertEquals(expectedIterationOrder, collectIfFunctionTargetIterationOrder);
    MutableCollection<Integer> collectBooleanIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectBoolean(collectBooleanIterationOrder::add).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectBooleanIterationOrder);
    MutableCollection<Integer> collectBooleanTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectBoolean(collectBooleanTargetIterationOrder::add, new BooleanHashBag());
    assertEquals(expectedIterationOrder, collectBooleanTargetIterationOrder);
    MutableCollection<Integer> collectByteIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectByte((Integer each) -> {
        collectByteIterationOrder.add(each);
        return (byte) 0;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectByteIterationOrder);
    MutableCollection<Integer> collectByteTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectByte((Integer each) -> {
        collectByteTargetIterationOrder.add(each);
        return (byte) 0;
    }, new ByteHashBag());
    assertEquals(expectedIterationOrder, collectByteTargetIterationOrder);
    MutableCollection<Integer> collectCharIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectChar((Integer each) -> {
        collectCharIterationOrder.add(each);
        return ' ';
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectCharIterationOrder);
    MutableCollection<Integer> collectCharTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectChar((Integer each) -> {
        collectCharTargetIterationOrder.add(each);
        return ' ';
    }, new CharHashBag());
    assertEquals(expectedIterationOrder, collectCharTargetIterationOrder);
    MutableCollection<Integer> collectDoubleIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectDouble((Integer each) -> {
        collectDoubleIterationOrder.add(each);
        return 0.0;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectDoubleIterationOrder);
    MutableCollection<Integer> collectDoubleTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectDouble((Integer each) -> {
        collectDoubleTargetIterationOrder.add(each);
        return 0.0;
    }, new DoubleHashBag());
    assertEquals(expectedIterationOrder, collectDoubleTargetIterationOrder);
    MutableCollection<Integer> collectFloatIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectFloat((Integer each) -> {
        collectFloatIterationOrder.add(each);
        return 0.0f;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectFloatIterationOrder);
    MutableCollection<Integer> collectFloatTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectFloat((Integer each) -> {
        collectFloatTargetIterationOrder.add(each);
        return 0.0f;
    }, new FloatHashBag());
    assertEquals(expectedIterationOrder, collectFloatTargetIterationOrder);
    MutableCollection<Integer> collectIntIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectInt((Integer each) -> {
        collectIntIterationOrder.add(each);
        return 0;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectIntIterationOrder);
    MutableCollection<Integer> collectIntTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectInt((Integer each) -> {
        collectIntTargetIterationOrder.add(each);
        return 0;
    }, new IntHashBag());
    assertEquals(expectedIterationOrder, collectIntTargetIterationOrder);
    MutableCollection<Integer> collectLongIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectLong((Integer each) -> {
        collectLongIterationOrder.add(each);
        return 0L;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectLongIterationOrder);
    MutableCollection<Integer> collectLongTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectLong((Integer each) -> {
        collectLongTargetIterationOrder.add(each);
        return 0L;
    }, new LongHashBag());
    assertEquals(expectedIterationOrder, collectLongTargetIterationOrder);
    MutableCollection<Integer> collectShortIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectShort((ShortFunction<Integer>) each -> {
        collectShortIterationOrder.add(each);
        return (short) 0;
    }).forEach(each -> {
    });
    assertEquals(expectedIterationOrder, collectShortIterationOrder);
    MutableCollection<Integer> collectShortTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().collectShort((ShortFunction<Integer>) each -> {
        collectShortTargetIterationOrder.add(each);
        return (short) 0;
    }, new ShortHashBag());
    assertEquals(expectedIterationOrder, collectShortTargetIterationOrder);
    MutableCollection<Integer> flatCollectIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().flatCollect(each -> Lists.immutable.with(flatCollectIterationOrder.add(each))).forEach(noop);
    assertEquals(expectedIterationOrder, flatCollectIterationOrder);
    MutableCollection<Integer> flatCollectTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().flatCollect(each -> Lists.immutable.with(flatCollectTargetIterationOrder.add(each)), new HashBag<>());
    assertEquals(expectedIterationOrder, flatCollectTargetIterationOrder);
    MutableCollection<Integer> countIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().count(countIterationOrder::add);
    assertEquals(expectedIterationOrder, countIterationOrder);
    MutableCollection<Integer> countWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().countWith((each, param) -> countWithIterationOrder.add(each), null);
    assertEquals(expectedIterationOrder, countWithIterationOrder);
    MutableCollection<Integer> anySatisfyIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().anySatisfy(each -> {
        anySatisfyIterationOrder.add(each);
        return false;
    });
    assertEquals(expectedIterationOrder, anySatisfyIterationOrder);
    MutableCollection<Integer> anySatisfyWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().anySatisfyWith((each, param) -> {
        anySatisfyWithIterationOrder.add(each);
        return false;
    }, null);
    assertEquals(expectedIterationOrder, anySatisfyWithIterationOrder);
    MutableCollection<Integer> allSatisfyIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().allSatisfy(each -> {
        allSatisfyIterationOrder.add(each);
        return true;
    });
    assertEquals(expectedIterationOrder, allSatisfyIterationOrder);
    MutableCollection<Integer> allSatisfyWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().allSatisfyWith((each, param) -> {
        allSatisfyWithIterationOrder.add(each);
        return true;
    }, null);
    assertEquals(expectedIterationOrder, allSatisfyWithIterationOrder);
    MutableCollection<Integer> noneSatisfyIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().noneSatisfy(each -> {
        noneSatisfyIterationOrder.add(each);
        return false;
    });
    assertEquals(expectedIterationOrder, noneSatisfyIterationOrder);
    MutableCollection<Integer> noneSatisfyWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().noneSatisfyWith((each, param) -> {
        noneSatisfyWithIterationOrder.add(each);
        return false;
    }, null);
    assertEquals(expectedIterationOrder, noneSatisfyWithIterationOrder);
    MutableCollection<Integer> detectIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().detect(each -> {
        detectIterationOrder.add(each);
        return false;
    });
    assertEquals(expectedIterationOrder, detectIterationOrder);
    MutableCollection<Integer> detectWithIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().detectWith((each, param) -> {
        detectWithIterationOrder.add(each);
        return false;
    }, null);
    assertEquals(expectedIterationOrder, detectWithIterationOrder);
    MutableCollection<Integer> detectIfNoneIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().detectIfNone(each -> {
        detectIfNoneIterationOrder.add(each);
        return false;
    }, () -> 0);
    assertEquals(expectedIterationOrder, detectIfNoneIterationOrder);
    MutableCollection<Integer> detectWithIfNoneIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().detectWithIfNone((each, param) -> {
        detectWithIfNoneIterationOrder.add(each);
        return false;
    }, null, () -> 0);
    assertEquals(expectedIterationOrder, detectWithIfNoneIterationOrder);
    MutableCollection<Integer> minComparatorIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().min((o1, o2) -> {
        if (minComparatorIterationOrder.isEmpty()) {
            minComparatorIterationOrder.add(o2);
        }
        minComparatorIterationOrder.add(o1);
        return 0;
    });
    assertEquals(expectedIterationOrder, minComparatorIterationOrder);
    MutableCollection<Integer> maxComparatorIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().max((o1, o2) -> {
        if (maxComparatorIterationOrder.isEmpty()) {
            maxComparatorIterationOrder.add(o2);
        }
        maxComparatorIterationOrder.add(o1);
        return 0;
    });
    assertEquals(expectedIterationOrder, maxComparatorIterationOrder);
    MutableCollection<Integer> minByIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().minBy(minByIterationOrder::add);
    assertEquals(expectedIterationOrder, minByIterationOrder);
    MutableCollection<Integer> maxByIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().maxBy(maxByIterationOrder::add);
    assertEquals(expectedIterationOrder, maxByIterationOrder);
    MutableCollection<Integer> groupByIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().groupBy(groupByIterationOrder::add);
    assertEquals(expectedIterationOrder, groupByIterationOrder);
    MutableCollection<Integer> groupByTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().groupBy(groupByTargetIterationOrder::add, new HashBagMultimap<>());
    assertEquals(expectedIterationOrder, groupByTargetIterationOrder);
    MutableCollection<Integer> groupByEachIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().groupByEach(each -> {
        groupByEachIterationOrder.add(each);
        return Lists.immutable.with(each);
    });
    assertEquals(expectedIterationOrder, groupByEachIterationOrder);
    MutableCollection<Integer> groupByEachTargetIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().groupByEach(each -> {
        groupByEachTargetIterationOrder.add(each);
        return Lists.immutable.with(each);
    }, new HashBagMultimap<>());
    assertEquals(expectedIterationOrder, groupByEachTargetIterationOrder);
    MutableCollection<Integer> sumOfFloatIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().sumOfFloat(each -> {
        sumOfFloatIterationOrder.add(each);
        return each.floatValue();
    });
    assertEquals(expectedIterationOrder, sumOfFloatIterationOrder);
    MutableCollection<Integer> sumOfDoubleIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().sumOfDouble(each -> {
        sumOfDoubleIterationOrder.add(each);
        return each.doubleValue();
    });
    assertEquals(expectedIterationOrder, sumOfDoubleIterationOrder);
    MutableCollection<Integer> sumOfIntIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().sumOfInt(each -> {
        sumOfIntIterationOrder.add(each);
        return each.intValue();
    });
    assertEquals(expectedIterationOrder, sumOfIntIterationOrder);
    MutableCollection<Integer> sumOfLongIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().sumOfLong(each -> {
        sumOfLongIterationOrder.add(each);
        return each.longValue();
    });
    assertEquals(expectedIterationOrder, sumOfLongIterationOrder);
    /*
         * TODO: Fix sumByPrimitive methods for bags, to only iterate once per item, not per occurrence.
        MutableCollection<Integer> sumByDoubleIterationOrder1 = this.newMutableForFilter();
        MutableCollection<Integer> sumByDoubleIterationOrder2 = this.newMutableForFilter();
        this.getInstanceUnderTest().sumByDouble(
                each -> {
                    sumByDoubleIterationOrder1.add(each);
                    return each;
                },
                each -> {
                    sumByDoubleIterationOrder2.add(each);
                    return 0.0;
                });
        assertEquals(expectedIterationOrder, sumByDoubleIterationOrder1);
        assertEquals(expectedIterationOrder, sumByDoubleIterationOrder2);

        MutableCollection<Integer> sumByFloatIterationOrder1 = this.newMutableForFilter();
        MutableCollection<Integer> sumByFloatIterationOrder2 = this.newMutableForFilter();
        this.getInstanceUnderTest().sumByFloat(
                each -> {
                    sumByFloatIterationOrder1.add(each);
                    return each;
                },
                each -> {
                    sumByFloatIterationOrder2.add(each);
                    return 0.0f;
                });
        assertEquals(expectedIterationOrder, sumByFloatIterationOrder1);
        assertEquals(expectedIterationOrder, sumByFloatIterationOrder2);

        MutableCollection<Integer> sumByIntIterationOrder1 = this.newMutableForFilter();
        MutableCollection<Integer> sumByIntIterationOrder2 = this.newMutableForFilter();
        this.getInstanceUnderTest().sumByInt(
                each -> {
                    sumByIntIterationOrder1.add(each);
                    return each;
                },
                each -> {
                    sumByIntIterationOrder2.add(each);
                    return 0;
                });
        assertEquals(expectedIterationOrder, sumByIntIterationOrder1);
        assertEquals(expectedIterationOrder, sumByIntIterationOrder2);

        MutableCollection<Integer> sumByLongIterationOrder1 = this.newMutableForFilter();
        MutableCollection<Integer> sumByLongIterationOrder2 = this.newMutableForFilter();
        this.getInstanceUnderTest().sumByLong(
                each -> {
                    sumByLongIterationOrder1.add(each);
                    return each;
                },
                each -> {
                    sumByLongIterationOrder2.add(each);
                    return 0L;
                });
        assertEquals(expectedIterationOrder, sumByLongIterationOrder1);
        assertEquals(expectedIterationOrder, sumByLongIterationOrder2);
        */
    MutableCollection<Integer> expectedInjectIntoIterationOrder = this.allowsDuplicates() ? this.newMutableForFilter(4, 4, 4, 4, 3, 3, 3, 2, 2, 1) : expectedIterationOrder;
    MutableCollection<Integer> injectIntoIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().injectInto(0, (Function2<Integer, Integer, Integer>) (argument1, argument2) -> {
        injectIntoIterationOrder.add(argument2);
        return argument1 + argument2;
    });
    assertEquals(expectedInjectIntoIterationOrder, injectIntoIterationOrder);
    MutableCollection<Integer> injectIntoIntIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().injectInto(0, (IntObjectToIntFunction<Integer>) (intParameter, objectParameter) -> {
        injectIntoIntIterationOrder.add(objectParameter);
        return intParameter + objectParameter;
    });
    assertEquals(expectedInjectIntoIterationOrder, injectIntoIntIterationOrder);
    MutableCollection<Integer> injectIntoLongIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().injectInto(0L, (LongObjectToLongFunction<Integer>) (longParameter, objectParameter) -> {
        injectIntoLongIterationOrder.add(objectParameter);
        return longParameter + objectParameter;
    });
    assertEquals(expectedInjectIntoIterationOrder, injectIntoLongIterationOrder);
    MutableCollection<Integer> injectIntoDoubleIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().injectInto(0L, (DoubleObjectToDoubleFunction<Integer>) (doubleParameter, objectParameter) -> {
        injectIntoDoubleIterationOrder.add(objectParameter);
        return doubleParameter + objectParameter;
    });
    assertEquals(expectedInjectIntoIterationOrder, injectIntoDoubleIterationOrder);
    MutableCollection<Integer> injectIntoFloatIterationOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().injectInto(0L, (FloatObjectToFloatFunction<Integer>) (floatParameter, objectParameter) -> {
        injectIntoFloatIterationOrder.add(objectParameter);
        return floatParameter + objectParameter;
    });
    assertEquals(expectedInjectIntoIterationOrder, injectIntoFloatIterationOrder);
    Counter toSortedListCount = new Counter();
    this.getInstanceUnderTest().toSortedList((o1, o2) -> {
        toSortedListCount.increment();
        return 0;
    });
    assertEquals(expectedIterationOrder.size() - 1, toSortedListCount.getCount());
    /*
        MutableCollection<Integer> toSortedListByIterationOrder = this.newMutableForFilter();
        this.getInstanceUnderTest().toSortedListBy(toSortedListByIterationOrder::add);
        assertEquals(expectedIterationOrder.size(), toSortedListByIterationOrder.size());

*/
    Counter toSortedSetCount = new Counter();
    this.getInstanceUnderTest().toSortedSet((o1, o2) -> {
        toSortedSetCount.increment();
        return 0;
    });
    assertEquals(expectedIterationOrder.size(), toSortedSetCount.getCount());
    /*
        MutableCollection<Integer> toSortedSetByIterationOrder = this.newMutableForFilter();
        this.getInstanceUnderTest().toSortedSetBy(toSortedSetByIterationOrder::add);
        assertEquals(expectedIterationOrder.size(), toSortedSetByIterationOrder.size());
*/
    Counter toSortedBagCount = new Counter();
    this.getInstanceUnderTest().toSortedBag((o1, o2) -> {
        toSortedBagCount.increment();
        return 0;
    });
    assertEquals(expectedIterationOrder.size(), toSortedBagCount.getCount());
    /*
        MutableCollection<Integer> toSortedBagByIterationOrder = this.newMutableForFilter();
        this.getInstanceUnderTest().toSortedBagBy(toSortedBagByIterationOrder::add);
        assertEquals(expectedIterationOrder.size(), toSortedBagByIterationOrder.size());
*/
    MutableCollection<Integer> summarizeIntOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().summarizeInt(each -> {
        summarizeIntOrder.add(each);
        return 0;
    });
    assertEquals(expectedIterationOrder, summarizeIntOrder);
    MutableCollection<Integer> summarizeFloatOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().summarizeFloat(each -> {
        summarizeFloatOrder.add(each);
        return 0;
    });
    assertEquals(expectedIterationOrder, summarizeFloatOrder);
    MutableCollection<Integer> summarizeLongOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().summarizeLong(each -> {
        summarizeLongOrder.add(each);
        return 0;
    });
    assertEquals(expectedIterationOrder, summarizeLongOrder);
    MutableCollection<Integer> summarizeDoubleOrder = this.newMutableForFilter();
    this.getInstanceUnderTest().summarizeDouble(each -> {
        summarizeDoubleOrder.add(each);
        return 0;
    });
    assertEquals(expectedIterationOrder, summarizeDoubleOrder);
}
Also used : CharHashBag(org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Multimap(org.eclipse.collections.api.multimap.Multimap) Predicate2(org.eclipse.collections.api.block.predicate.Predicate2) MutableBag(org.eclipse.collections.api.bag.MutableBag) MutableList(org.eclipse.collections.api.list.MutableList) LongObjectToLongFunction(org.eclipse.collections.api.block.function.primitive.LongObjectToLongFunction) Assert.assertThat(org.junit.Assert.assertThat) DoubleObjectToDoubleFunction(org.eclipse.collections.api.block.function.primitive.DoubleObjectToDoubleFunction) RichIterable(org.eclipse.collections.api.RichIterable) MutableLongCollection(org.eclipse.collections.api.collection.primitive.MutableLongCollection) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Functions(org.eclipse.collections.impl.block.factory.Functions) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LongIterable(org.eclipse.collections.api.LongIterable) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Tuples(org.eclipse.collections.impl.tuple.Tuples) Interval(org.eclipse.collections.impl.list.Interval) Assert.fail(org.junit.Assert.fail) ShortFunction(org.eclipse.collections.api.block.function.primitive.ShortFunction) ShortHashBag(org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag) Comparators(org.eclipse.collections.impl.block.factory.Comparators) MutableCharCollection(org.eclipse.collections.api.collection.primitive.MutableCharCollection) DoubleHashBag(org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) MutableIntCollection(org.eclipse.collections.api.collection.primitive.MutableIntCollection) IntObjectToIntFunction(org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction) ByteHashBag(org.eclipse.collections.impl.bag.mutable.primitive.ByteHashBag) BooleanHashBag(org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Collectors(java.util.stream.Collectors) CharIterable(org.eclipse.collections.api.CharIterable) Objects(java.util.Objects) List(java.util.List) DoubleIterable(org.eclipse.collections.api.DoubleIterable) Lists(org.eclipse.collections.impl.factory.Lists) ObjectDoubleMaps(org.eclipse.collections.impl.factory.primitive.ObjectDoubleMaps) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) MutableByteCollection(org.eclipse.collections.api.collection.primitive.MutableByteCollection) Matchers.is(org.hamcrest.Matchers.is) FloatIterable(org.eclipse.collections.api.FloatIterable) Function(org.eclipse.collections.api.block.function.Function) IntIterable(org.eclipse.collections.api.IntIterable) IterableTestCase.assertEquals(org.eclipse.collections.test.IterableTestCase.assertEquals) Bag(org.eclipse.collections.api.bag.Bag) MutableShortCollection(org.eclipse.collections.api.collection.primitive.MutableShortCollection) Procedure(org.eclipse.collections.api.block.procedure.Procedure) LongHashBag(org.eclipse.collections.impl.bag.mutable.primitive.LongHashBag) Verify.assertThrows(org.eclipse.collections.impl.test.Verify.assertThrows) Assert.assertSame(org.junit.Assert.assertSame) IterableTestCase.assertNotEquals(org.eclipse.collections.test.IterableTestCase.assertNotEquals) MutableMap(org.eclipse.collections.api.map.MutableMap) HashBagMultimap(org.eclipse.collections.impl.multimap.bag.HashBagMultimap) Function2(org.eclipse.collections.api.block.function.Function2) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) ByteIterable(org.eclipse.collections.api.ByteIterable) ImmutableBag(org.eclipse.collections.api.bag.ImmutableBag) NoSuchElementException(java.util.NoSuchElementException) FloatObjectToFloatFunction(org.eclipse.collections.api.block.function.primitive.FloatObjectToFloatFunction) Pair(org.eclipse.collections.api.tuple.Pair) MutableCollection(org.eclipse.collections.api.collection.MutableCollection) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Collectors2(org.eclipse.collections.impl.collector.Collectors2) MutableFloatCollection(org.eclipse.collections.api.collection.primitive.MutableFloatCollection) Iterator(java.util.Iterator) BooleanIterable(org.eclipse.collections.api.BooleanIterable) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) ShortIterable(org.eclipse.collections.api.ShortIterable) MutableDoubleCollection(org.eclipse.collections.api.collection.primitive.MutableDoubleCollection) SortedSets(org.eclipse.collections.impl.factory.SortedSets) Bags(org.eclipse.collections.impl.factory.Bags) ObjectLongMaps(org.eclipse.collections.impl.factory.primitive.ObjectLongMaps) TreeSortedMap(org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MutableBooleanCollection(org.eclipse.collections.api.collection.primitive.MutableBooleanCollection) Assert.assertNull(org.junit.Assert.assertNull) Sets(org.eclipse.collections.impl.factory.Sets) MapIterable(org.eclipse.collections.api.map.MapIterable) PartitionIterable(org.eclipse.collections.api.partition.PartitionIterable) AddFunction(org.eclipse.collections.impl.block.function.AddFunction) FloatHashBag(org.eclipse.collections.impl.bag.mutable.primitive.FloatHashBag) Assert(org.junit.Assert) IntHashBag(org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag) ByteHashBag(org.eclipse.collections.impl.bag.mutable.primitive.ByteHashBag) BooleanHashBag(org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag) DoubleHashBag(org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag) Counter(org.eclipse.collections.impl.Counter) FloatHashBag(org.eclipse.collections.impl.bag.mutable.primitive.FloatHashBag) LongHashBag(org.eclipse.collections.impl.bag.mutable.primitive.LongHashBag) ShortFunction(org.eclipse.collections.api.block.function.primitive.ShortFunction) IntHashBag(org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CharHashBag(org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag) ShortHashBag(org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag) Test(org.junit.Test)

Example 2 with HashBag

use of org.eclipse.collections.impl.bag.mutable.HashBag in project narchy by automenta.

the class NAR method stats.

/**
 * creates a snapshot statistics object
 * TODO extract a Method Object holding the snapshot stats with the instances created below as its fields
 */
public SortedMap<String, Object> stats() {
    LongSummaryStatistics beliefs = new LongSummaryStatistics();
    LongSummaryStatistics goals = new LongSummaryStatistics();
    LongSummaryStatistics questions = new LongSummaryStatistics();
    LongSummaryStatistics quests = new LongSummaryStatistics();
    Histogram termlinkCount = new Histogram(1);
    Histogram tasklinkCount = new Histogram(1);
    // Frequency complexity = new Frequency();
    HashBag clazz = new HashBag();
    HashBag policy = new HashBag();
    HashBag rootOp = new HashBag();
    ShortCountsHistogram volume = new ShortCountsHistogram(2);
    // AtomicInteger i = new AtomicInteger(0);
    // LongSummaryStatistics termlinksCap = new LongSummaryStatistics();
    // LongSummaryStatistics tasklinksCap = new LongSummaryStatistics();
    SortedMap<String, Object> x = new TreeMap();
    synchronized (exe) {
        concepts().filter(xx -> !(xx instanceof Functor)).forEach(c -> {
            // complexity.addValue(c.complexity());
            volume.recordValue(c.volume());
            rootOp.add(c.op());
            clazz.add(c.getClass().toString());
            ConceptState p = c.state();
            policy.add(p != null ? p.toString() : "null");
            // termlinksCap.accept(c.termlinks().capacity());
            termlinkCount.recordValue(c.termlinks().size());
            // tasklinksCap.accept(c.tasklinks().capacity());
            tasklinkCount.recordValue(c.tasklinks().size());
            beliefs.accept(c.beliefs().size());
            goals.accept(c.goals().size());
            questions.accept(c.questions().size());
            quests.accept(c.quests().size());
        });
        // x.put("time real", new Date());
        if (loop.isRunning()) {
            loop.stats("loop", x);
        }
        x.put("time", time());
        // x.put("term index", terms.summary());
        x.put("concept count", concepts.size());
    }
    x.put("belief count", ((double) beliefs.getSum()));
    x.put("goal count", ((double) goals.getSum()));
    Util.decode(tasklinkCount, "tasklink count", 4, x::put);
    // x.put("tasklink usage", ((double) tasklinkCount.getTotalCount()) / tasklinksCap.getSum());
    x.put("tasklink total", ((double) tasklinkCount.getTotalCount()));
    Util.decode(termlinkCount, "termlink count", 4, x::put);
    // x.put("termlink usage", ((double) termlinkCount.getTotalCount()) / termlinksCap.getSum());
    x.put("termlink total", ((double) termlinkCount.getTotalCount()));
    // DoubleSummaryStatistics pos = new DoubleSummaryStatistics();
    // DoubleSummaryStatistics neg = new DoubleSummaryStatistics();
    // causes.forEach(c -> pos.accept(c.pos()));
    // causes.forEach(c -> neg.accept(c.neg()));
    // x.put("value count", pos.getCount());
    // x.put("value pos mean", pos.getAverage());
    // x.put("value pos min", pos.getMin());
    // x.put("value pos max", pos.getMax());
    // x.put("value neg mean", neg.getAverage());
    // x.put("value neg min", neg.getMin());
    // x.put("value neg max", neg.getMax());
    // x.put("volume mean", volume.);
    // 
    // x.put("termLinksCapacity", termlinksCap);
    // x.put("taskLinksUsed", tasklinksUsed);
    // x.put("taskLinksCapacity", tasklinksCap);
    Util.toMap(policy, "concept state", x::put);
    Util.toMap(rootOp, "concept op", x::put);
    Util.decode(volume, "concept volume", 4, x::put);
    Util.toMap(clazz, "concept class", x::put);
    x.put("term cache (eternal)", Op.cache.summary());
    x.put("term cache (temporal)", Op.cacheTemporal.summary());
    return x;
}
Also used : Atom(nars.term.atom.Atom) Ansi(org.fusesource.jansi.Ansi) GZIPInputStream(java.util.zip.GZIPInputStream) Exec(nars.exe.Exec) ConceptBuilder(nars.concept.util.ConceptBuilder) MutableInteger(jcog.math.MutableInteger) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) ConceptState(nars.concept.util.ConceptState) Truth(nars.truth.Truth) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) Op(nars.Op) Topic(jcog.event.Topic) ListTopic(jcog.event.ListTopic) TaskConcept(nars.concept.TaskConcept) TruthFunctions.c2w(nars.truth.TruthFunctions.c2w) Term(nars.term.Term) Longs(com.google.common.primitives.Longs) TriConsumer(jcog.util.TriConsumer) ShortToObjectFunction(org.eclipse.collections.api.block.function.primitive.ShortToObjectFunction) Twin(org.eclipse.collections.api.tuple.Twin) Service(jcog.Service) PreciseTruth(nars.truth.PreciseTruth) Functor.f(nars.term.Functor.f) Util(jcog.Util) BeliefTable(nars.table.BeliefTable) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) ConceptIndex(nars.index.term.ConceptIndex) GZIPOutputStream(java.util.zip.GZIPOutputStream) java.util.function(java.util.function) NotNull(org.jetbrains.annotations.NotNull) Operator(nars.concept.Operator) nars.control(nars.control) java.util(java.util) Tense(nars.time.Tense) Prioritized(jcog.pri.Prioritized) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ShortCountsHistogram(org.HdrHistogram.ShortCountsHistogram) ETERNAL(nars.time.Tense.ETERNAL) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvalidTaskException(nars.task.util.InvalidTaskException) nars.$.$(nars.$.$) Pri(jcog.pri.Pri) On(jcog.event.On) Cycles(nars.util.Cycles) Pair(org.eclipse.collections.api.tuple.Pair) Concept(nars.concept.Concept) Time(nars.time.Time) Ansi.ansi(org.fusesource.jansi.Ansi.ansi) NarseseException(nars.Narsese.NarseseException) Logger(org.slf4j.Logger) FasterList(jcog.list.FasterList) Functor(nars.term.Functor) Cycler(jcog.exe.Cycler) ITask(nars.task.ITask) NALTask(nars.task.NALTask) Histogram(org.HdrHistogram.Histogram) ArrayIterator(jcog.util.ArrayIterator) java.io(java.io) Subterms(nars.subterm.Subterms) Atomic(nars.term.atom.Atomic) Termed(nars.term.Termed) Services(jcog.Services) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) ShortCountsHistogram(org.HdrHistogram.ShortCountsHistogram) Histogram(org.HdrHistogram.Histogram) ShortCountsHistogram(org.HdrHistogram.ShortCountsHistogram) ConceptIndex(nars.index.term.ConceptIndex) Functor(nars.term.Functor) ConceptState(nars.concept.util.ConceptState)

Example 3 with HashBag

use of org.eclipse.collections.impl.bag.mutable.HashBag in project narchy by automenta.

the class ActivateTest method testConceptFireLinkSelection.

@Test
public void testConceptFireLinkSelection() throws Narsese.NarseseException {
    int count = 8;
    NAR nar = new NARS().tmp();
    // low priority so it doesnt affect links
    nar.input("$0.01 a:b.");
    nar.run(1);
    System.out.println("inputs:\n");
    Concept c = nar.conceptualize("a:b");
    for (int n = 0; n < count; n++) {
        PLink<Term> inserted = new PLink<>($("x" + n + ":a"), ((1 + n) / ((float) count)));
        System.out.println(inserted);
        c.termlinks().put(inserted);
    }
    System.out.println();
    HashBag<String> termlinkHits = new HashBag();
    HashBag<String> taskHits = new HashBag();
    // HashBag<String> premiseHits = new HashBag();
    Activate cf = new Activate(c, 1f);
    Term A = $.the("a");
    // BatchActivation ba = new BatchActivation();
    for (int i = 0; i < 100; i++) {
        final int[] remain = { 9 };
        cf.premises(nar, (task, term) -> {
            Task ptask = task;
            Term pterm = term.get();
            System.out.println("tasklink=" + ptask + " termlink=" + pterm);
            if (pterm instanceof Atom || !A.equals(pterm.sub(0)))
                // ignore
                return true;
            String tls = pterm.toString();
            // premiseHits.addOccurrences(p.toString(), 1);
            termlinkHits.addOccurrences(/*tasklink.get() + " " +*/
            tls, 1);
            taskHits.addOccurrences(/*tasklink.get() + " " +*/
            (ptask + " " + pterm), 1);
            return --remain[0] > 0;
        }, 1, 3);
    // ba.commit(nar);
    }
    System.out.println("termlinks pri (after):\n");
    c.termlinks().print();
    System.out.println("\ntermlink hits:\n");
    termlinkHits.topOccurrences(termlinkHits.size()).forEach(System.out::println);
    // System.out.println("\ntask hits:\n");
    // taskHits.topOccurrences(taskHits.size()).forEach(System.out::println);
    // System.out.println("\npremise hits:\n");
    // premiseHits.topOccurrences(premiseHits.size()).forEach(System.out::println);
    // System.out.println();
    // c.print();
    // System.out.println();
    ObjectIntPair<String> top = termlinkHits.topOccurrences(1).get(0);
    ObjectIntPair<String> bottom = termlinkHits.bottomOccurrences(1).get(0);
    String min = bottom.getOne();
    // allow either 0 or 1
    assertTrue("(a-->x0)".equals(min) || "(a-->x1)".equals(min));
    assertEquals("(a-->x" + (count - 1) + ")", top.getOne());
}
Also used : PLink(jcog.pri.PLink) Term(nars.term.Term) Atom(nars.term.atom.Atom) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) Activate(nars.control.Activate) Test(org.junit.jupiter.api.Test)

Aggregations

Term (nars.term.Term)2 Atom (nars.term.atom.Atom)2 HashBag (org.eclipse.collections.impl.bag.mutable.HashBag)2 Longs (com.google.common.primitives.Longs)1 java.io (java.io)1 URL (java.net.URL)1 java.util (java.util)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 java.util.function (java.util.function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1