/** * Adds the specified element to this set. Returns {@code true} if this set changed as a result * of this operation. Changes in element order are not notified by the returned value. * * <p>The default implementation delegates to <code>{@linkplain #add(Object, int) add}(element, 1)</code>.</p> * * @param element the element to add (may be {@code null}). * @return {@code true} if this set changed as a result of this operation. */ @Override public boolean add(final E element) { return add(element, 1); }
/** * Returns the frequency of the specified element in this set. * * @param element the element whose frequency is to be obtained. * @return the frequency of the given element, or {@code 0} if it does not occur in this set. */ public int frequency(final E element) { return signedFrequency(element) ^ order; }
/** * Simple test with 2 elements. */ @Test @DependsOnMethod("testSimple") public void testTwoElements() { final FrequencySortedSet<Integer> set = new FrequencySortedSet<>(true); for (int i=0; i<10; i++) { if ((i % 3) == 0) { set.add(11); } set.add(9); } assertEquals(2, set.size()); assertEquals(Integer.valueOf(9), set.first()); assertEquals(Integer.valueOf(11), set.last()); assertArrayEquals(new int[] {10, 4}, set.frequencies()); }
/** * Tests creation of various subsets. */ @Test @DependsOnMethod("testTwoElements") public void testSubSet() { final FrequencySortedSet<Integer> set = new FrequencySortedSet<>(); set.addAll(Arrays.asList(2, 5, 3, 2, 4, 2, 3, 6, 2)); assertArrayEquals(new Integer[] {5, 4, 6, 3, 2}, set.toArray()); assertArrayEquals(new int[] {1, 1, 1, 2, 4}, set.frequencies()); assertArrayEquals("Expected all elements occurring less often than 2.", new Integer[] {5, 4, 6, 3}, set.headSet(2).toArray()); assertArrayEquals("Expected all elements occurring less often than 3.", new Integer[] {5, 4, 6}, set.headSet(3).toArray()); assertArrayEquals("Expected all elements occurring at least as often than 3.", new Integer[] {3, 2}, set.tailSet(3).toArray()); assertArrayEquals("Expected all elements occurring at least as often than 3 but less than 2.", new Integer[] {3}, set.subSet(3, 2).toArray()); assertTrue(set.subSet(2, 3).isEmpty()); } }
/** * A simple case with only two elements, the first one being omitted. */ @Test public void testSimple() { boolean reverse = false; do { final FrequencySortedSet<Integer> set = new FrequencySortedSet<>(reverse); assertFalse(set.add(12, 0)); assertTrue (set.add(18, 11)); assertEquals(Collections.singleton(18), set); assertArrayEquals(new int[] {11}, set.frequencies()); } while ((reverse = !reverse) == true); }
/** * Returns the first element in this set. * * <ul> * <li>For sets created with the default order, this is the less frequently added element. * If more than one element were added with the same frequency, this is the first one * that has been {@linkplain #add added} to this set at this frequency.</li> * <li>For sets created with the reverse order, this is the most frequently added element. * If more than one element were added with the same frequency, this is the first one * that has been {@linkplain #add added} to this set at this frequency.</li> * </ul> * * @throws NoSuchElementException if this set is empty. */ @Override public E first() throws NoSuchElementException { ensureSorted(); if (sorted.length != 0) { return sorted[0]; } else { throw new NoSuchElementException(); } }
/** * Returns the last element in this set. * * <ul> * <li>For sets created with the default order, this is the most frequently added element. * If more than one element were added with the same frequency, this is the last one * that has been {@linkplain #add added} to this set at this frequency.</li> * <li>For sets created with the reverse order, this is the less frequently added element. * If more than one element were added with the same frequency, this is the last one * that has been {@linkplain #add added} to this set at this frequency.</li> * </ul> * * @throws NoSuchElementException if this set is empty. */ @Override public E last() throws NoSuchElementException { ensureSorted(); final int length = sorted.length; if (length != 0) { return sorted[length - 1]; } else { throw new NoSuchElementException(); } }
/** * Returns the first element in this set. * * <ul> * <li>For sets created with the default order, this is the less frequently added element. * If more than one element were added with the same frequency, this is the first one * that has been {@linkplain #add added} to this set at this frequency.</li> * <li>For sets created with the reverse order, this is the most frequently added element. * If more than one element were added with the same frequency, this is the first one * that has been {@linkplain #add added} to this set at this frequency.</li> * </ul> * * @throws NoSuchElementException if this set is empty. */ @Override public E first() throws NoSuchElementException { ensureSorted(); if (sorted.length != 0) { return sorted[0]; } else { throw new NoSuchElementException(); } }
/** * Adds the specified element to this set. Returns {@code true} if this set changed as a result * of this operation. Changes in element order are not notified by the returned value. * * <p>The default implementation delegates to <code>{@linkplain #add(Object, int) add}(element, 1)</code>.</p> * * @param element the element to add (may be {@code null}). * @return {@code true} if this set changed as a result of this operation. */ @Override public boolean add(final E element) { return add(element, 1); }
/** * Returns the frequency of the specified element in this set. * * @param element the element whose frequency is to be obtained. * @return the frequency of the given element, or {@code 0} if it does not occur in this set. */ public int frequency(final E element) { return signedFrequency(element) ^ order; }
/** * Returns the last element in this set. * * <ul> * <li>For sets created with the default order, this is the most frequently added element. * If more than one element were added with the same frequency, this is the last one * that has been {@linkplain #add added} to this set at this frequency.</li> * <li>For sets created with the reverse order, this is the less frequently added element. * If more than one element were added with the same frequency, this is the last one * that has been {@linkplain #add added} to this set at this frequency.</li> * </ul> * * @throws NoSuchElementException if this set is empty. */ @Override public E last() throws NoSuchElementException { ensureSorted(); final int length = sorted.length; if (length != 0) { return sorted[length - 1]; } else { throw new NoSuchElementException(); } }
/** * Compares the specified elements for {@linkplain #frequency frequency}. For {@code FrequencySortedSet} * with default ordering, this method returns a positive number if {@code o1} has been added more frequently * to this set than {@code o2}, a negative number if {@code o1} has been added less frequently than {@code o2}, * and 0 otherwise. For {@code FrequencySortedSet} with reverse ordering, this is the converse. * * <p>This method is final because the {@code FrequencySortedSet} implementation makes * assumptions on the comparator that would not hold if this method were overridden.</p> */ @Override public final int compare(final E o1, final E o2) { return signedFrequency(o1) - signedFrequency(o2); }
/** * Returns an iterator over the elements in this set in frequency order. */ @Override public Iterator<E> iterator() { ensureSorted(); return new Iter(sorted, 0, sorted.length); }
/** * Compares the specified elements for {@linkplain #frequency frequency}. For {@code FrequencySortedSet} * with default ordering, this method returns a positive number if {@code o1} has been added more frequently * to this set than {@code o2}, a negative number if {@code o1} has been added less frequently than {@code o2}, * and 0 otherwise. For {@code FrequencySortedSet} with reverse ordering, this is the converse. * * <p>This method is final because the {@code FrequencySortedSet} implementation makes * assumptions on the comparator that would not hold if this method were overridden.</p> */ @Override public final int compare(final E o1, final E o2) { return signedFrequency(o1) - signedFrequency(o2); }
/** * Returns the frequency of all elements in this set, in iteration order. * * @return the frequency of all elements in this set. */ public int[] frequencies() { ensureSorted(); return frequencies.clone(); }
/** * Returns the content of this set as an array. */ @Override public Object[] toArray() { ensureSorted(); return sorted.clone(); }
/** * Returns an iterator over the elements in this set in frequency order. */ @Override public Iterator<E> iterator() { ensureSorted(); return new Iter(sorted, 0, sorted.length); }
/** * Returns the content of this set as an array. */ @Override public Object[] toArray() { ensureSorted(); return sorted.clone(); }
/** * Returns the content of this set as an array. * * @param <T> the type of the array elements. * @param array the array where to copy the elements. * @return the elements in the given array, or in a new array * if the given array does not have a sufficient capacity. */ @Override @SuppressWarnings("unchecked") public <T> T[] toArray(T[] array) { ensureSorted(); if (array.length < sorted.length) { array = (T[]) Array.newInstance(array.getClass().getComponentType(), sorted.length); } System.arraycopy(sorted, 0, array, 0, sorted.length); return array; } }
/** * Returns the frequency of all elements in this set, in iteration order. * * @return the frequency of all elements in this set. */ public int[] frequencies() { ensureSorted(); return frequencies.clone(); }