grendel.storage
Class NewsSet

java.lang.Object
  |
  +--grendel.storage.NewsSet

public class NewsSet
extends java.lang.Object

This class represents a set of integers. It uses a highly compressed encoding, taking advantage of the assumption that many of the integers are consecutive. This is intended for representing lines from the .newsrc file, which have lists of message-numbers lists like

1-29627,29635,29658,32861-32863

so the data has these properties:


Constructor Summary
NewsSet()
           
NewsSet(byte[] chars, int start, int end)
           
NewsSet(ByteBuf chars)
           
NewsSet(java.lang.String chars)
           
 
Method Summary
 long countMissingInRange(long range_start, long range_end)
          Returns the number of elements in the range [start, end) which are not members of the set.
 boolean delete(long number)
          Cause the number to not be a member of the set.
 boolean delete(long start, long end)
          Cause the numbers in the range [start, end) to not be members of the set.
 long firstNonMember()
          Returns the lowest non-member of the set greater than 0.
 long firstNonMember(long min, long max)
          Returns the first number which is not a member of the set, which falls in the range [min, max).
 boolean insert(long number)
          Cause the number to be a member of the set.
 boolean insert(long start, long end)
          Cause the numbers in the range [start, end) to be members of the set.
protected  boolean isEmpty()
          True if there are no elements in the set.
 long lastNonMember(long min, long max)
          Returns the last number which is not a member of the set, which falls in the range [min, max).
static void main(java.lang.String[] args)
           
 void markDirty()
          Called when a change is made to the set.
 long max()
          Returns the largest element of the set.
 boolean member(long number)
          Returns whether the number is a member of the set.
 long min()
          Returns the smallest element of the set.
 long nextMember(long after)
          Returns the first number which is in the set, and which is greater than the given value.
 long previousMember(long after)
          Returns the last number which is in the set, and which is less than the given value.
protected static void self_test_adder()
           
protected static void self_test_adder(NewsSet set, boolean add_p, long value, java.lang.String target)
           
protected static void self_test_decoder()
           
protected static void self_test_decoder(java.lang.String s, java.lang.String target)
          Self tests
protected static void self_test_first_nonmember(boolean cache)
           
protected static void self_test_first_nonmember(NewsSet set, boolean cache, long start, long end, long target)
           
protected static void self_test_last_nonmember(boolean cache)
           
protected static void self_test_last_nonmember(NewsSet set, boolean cache, long start, long end, long target)
           
protected static void self_test_member(boolean cache)
           
protected static void self_test_member(NewsSet set, boolean cache, long elt, boolean target)
           
protected static void self_test_next_member(boolean cache)
           
protected static void self_test_next_member(NewsSet set, boolean cache, long elt, long target)
           
protected static void self_test_prev_member(boolean cache)
           
protected static void self_test_prev_member(NewsSet set, boolean cache, long elt, long target)
           
protected static void self_test_ranges()
           
protected static void self_test_ranges(NewsSet set, long start, long end, java.lang.String target)
           
protected static void self_test()
           
 java.lang.String toString()
           
 void write(ByteBuf out)
          Converts a printed representation of the numbers in the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NewsSet

public NewsSet()

NewsSet

public NewsSet(java.lang.String chars)

NewsSet

public NewsSet(ByteBuf chars)

NewsSet

public NewsSet(byte[] chars,
               int start,
               int end)
Method Detail

firstNonMember

public long firstNonMember()
Returns the lowest non-member of the set greater than 0. Note that this never returns -1, since a NewsSet can't hold the set of positive integers.

min

public long min()
Returns the smallest element of the set. Returns -1 if the set is empty.

max

public long max()
Returns the largest element of the set. Returns -1 if the set is empty.

member

public boolean member(long number)
Returns whether the number is a member of the set.

insert

public boolean insert(long number)
Cause the number to be a member of the set. Returns false if the number was already a member of the set, true otherwise.

delete

public boolean delete(long number)
Cause the number to not be a member of the set. Returns true if the number had been a member of the set, false otherwise.

insert

public boolean insert(long start,
                      long end)
Cause the numbers in the range [start, end) to be members of the set. Returns false if all of the numbers were already members of the set, true otherwise.

delete

public boolean delete(long start,
                      long end)
Cause the numbers in the range [start, end) to not be members of the set. Returns true if any of the numbers had been members of the set, false otherwise.

countMissingInRange

public long countMissingInRange(long range_start,
                                long range_end)
Returns the number of elements in the range [start, end) which are not members of the set.

firstNonMember

public long firstNonMember(long min,
                           long max)
Returns the first number which is not a member of the set, which falls in the range [min, max). Returns -1 if all numbers in the range are members.

lastNonMember

public long lastNonMember(long min,
                          long max)
Returns the last number which is not a member of the set, which falls in the range [min, max). If all numbers in that range are members of the set, returns -1.

nextMember

public long nextMember(long after)
Returns the first number which is in the set, and which is greater than the given value. Returns -1 if no number greater than the given value is in the set.

previousMember

public long previousMember(long after)
Returns the last number which is in the set, and which is less than the given value. Returns -1 if the smallest member of the set is greater than or equal to the given value.

isEmpty

protected boolean isEmpty()
True if there are no elements in the set.

markDirty

public void markDirty()
Called when a change is made to the set. This method does nothing, but is provided for the benefit of subclasses.

write

public void write(ByteBuf out)
Converts a printed representation of the numbers in the set. This will be something like "1-29627,29635,29658,32861-32863", which is the same representation that new NewsSet() expects.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

self_test_decoder

protected static void self_test_decoder(java.lang.String s,
                                        java.lang.String target)
Self tests

self_test_decoder

protected static void self_test_decoder()

self_test_adder

protected static void self_test_adder(NewsSet set,
                                      boolean add_p,
                                      long value,
                                      java.lang.String target)

self_test_adder

protected static void self_test_adder()

self_test_ranges

protected static void self_test_ranges(NewsSet set,
                                       long start,
                                       long end,
                                       java.lang.String target)

self_test_ranges

protected static void self_test_ranges()

self_test_member

protected static void self_test_member(NewsSet set,
                                       boolean cache,
                                       long elt,
                                       boolean target)

self_test_first_nonmember

protected static void self_test_first_nonmember(NewsSet set,
                                                boolean cache,
                                                long start,
                                                long end,
                                                long target)

self_test_last_nonmember

protected static void self_test_last_nonmember(NewsSet set,
                                               boolean cache,
                                               long start,
                                               long end,
                                               long target)

self_test_next_member

protected static void self_test_next_member(NewsSet set,
                                            boolean cache,
                                            long elt,
                                            long target)

self_test_prev_member

protected static void self_test_prev_member(NewsSet set,
                                            boolean cache,
                                            long elt,
                                            long target)

self_test_member

protected static void self_test_member(boolean cache)

self_test_first_nonmember

protected static void self_test_first_nonmember(boolean cache)

self_test_last_nonmember

protected static void self_test_last_nonmember(boolean cache)

self_test_next_member

protected static void self_test_next_member(boolean cache)

self_test_prev_member

protected static void self_test_prev_member(boolean cache)

self_test

protected static void self_test()

main

public static void main(java.lang.String[] args)