Class PreferentialAttachmentCache<T>

  • All Implemented Interfaces:
    Cache<T>

    public class PreferentialAttachmentCache<T>
    extends Object
    implements Cache<T>
    A Cache that has a maximum size. Each item is associated with a count. As an item is added that count is incremented. If more than the maximum number of items are added then the one with the smallest count is removed. When the get() method is called, one of the items is returned at random. The probability of each item being returned is proportional to its count - items with large counts are more likely to be returned. When the get() method is called the count of that item is incremented. The result is that the rich grow richer --- items that are returned become more likely to be returned, and over time some items in the cache will be returned much more often than others.

    This can be used to generate graphs which are power-law in the number of times elements are added, i.e. by adding elements to both a graph and this cache, and repeatedly also adding the result of calling get() on this cache, then some elements will be added many more times that others. This can be used to test the performance when the same element is added multiple times causing many aggregations.

    • Constructor Detail

      • PreferentialAttachmentCache

        public PreferentialAttachmentCache​(int maxSize)
    • Method Detail

      • add

        public void add​(T t)
        Specified by:
        add in interface Cache<T>
      • get

        public T get()
        Returns a random element in proportion to the frequencies. The frequency of the returned item is increased by 1.
        Specified by:
        get in interface Cache<T>
        Returns:
        A random element from the cache with probability proportional to the frequency with which it has been returned.
      • getNumberOfElements

        public long getNumberOfElements()
      • getItemsAndFrequencies

        public Map<T,​Long> getItemsAndFrequencies()