component/src/main/java/org/wso2/extension/siddhi/execution/extrema/util/AbstractTopKBottomKFinder.java
private void incrementCounter(ListNode<Counter<T>> counterNode, int incrementCount) {
        // Increase the counter count
        Counter<T> counter = counterNode.getValue();
        counter.increaseCount(incrementCount);

        // Detaching the counter from the old bucket node
        ListNode<Bucket> oldBucketNode = counter.getBucketNode();
        Bucket oldBucket = oldBucketNode.getValue();
        oldBucket.getCounterList().remove(counterNode);

        if (counter.getCount() <= 0) {
            // Removing counter if count is 0
            counterMap.remove(counter.getItem());
        } else {
            // Finding the right bucket for the counter
            // This may or may not be an adjacent bucket because incrementCount can be aby value
            ListNode<Bucket> currentNode = oldBucketNode;
            ListNode<Bucket> nextBucketNode;
            if (incrementCount > 0) {
                nextBucketNode = oldBucketNode.getNextNode();
            } else {
                nextBucketNode = oldBucketNode.getPreviousNode();
            }
            while (nextBucketNode != null) {
                Bucket nextBucket = nextBucketNode.getValue();
                if (counter.getCount() == nextBucket.getCount()) {
                    nextBucket.getCounterList().addAfterLast(counterNode);
                    break;
                } else if (incrementCount > 0 && counter.getCount() > nextBucket.getCount()) {
                    currentNode = nextBucketNode;
                    nextBucketNode = nextBucketNode.getNextNode();
                } else if (incrementCount < 0 && counter.getCount() < nextBucket.getCount()) {
                    currentNode = nextBucketNode;
                    nextBucketNode = nextBucketNode.getPreviousNode();
                } else {
                    // New bucket node needs to be created
                    nextBucketNode = null;
                }
            }

            if (nextBucketNode == null) {
                Bucket newBucket = new Bucket(counter.getCount());
                newBucket.counterList.addAfterLast(counterNode);
                nextBucketNode = bucketList.addAfterNode(currentNode, newBucket);
            }
            counter.setBucketNode(nextBucketNode);
        }

        // Cleaning up
        if (oldBucket.counterList.isEmpty()) {
            bucketList.remove(oldBucketNode);
        }
    }
/**
     * Increase the frequency for the node specified and moves the counter to relevant bucket
     *
     * @param counterNode    The counter node to increase the frequency of
     * @param incrementCount The increment size
     */
Increase the frequency for the node specified and moves the counter to relevant bucket
@param counterNode    The counter node to increase the frequency of
@param incrementCount The increment size
Increase the frequency for the node specified and moves the counter to relevant bucket @param counterNode The counter node to increase the frequency of @param incrementCount The increment size
private void incrementCounter(ListNode<Counter<T>> counterNode, int incrementCount) {
        Counter<T> counter = counterNode.getValue();
        counter.increaseCount(incrementCount);
        ListNode<Bucket> oldBucketNode = counter.getBucketNode();
        Bucket oldBucket = oldBucketNode.getValue();
        oldBucket.getCounterList().remove(counterNode);
        if (counter.getCount() <= 0) {
            counterMap.remove(counter.getItem());
        } else {
            ListNode<Bucket> currentNode = oldBucketNode;
            ListNode<Bucket> nextBucketNode;
            if (incrementCount > 0) {
                nextBucketNode = oldBucketNode.getNextNode();
            } else {
                nextBucketNode = oldBucketNode.getPreviousNode();
            }
            while (nextBucketNode != null) {
                Bucket nextBucket = nextBucketNode.getValue();
                if (counter.getCount() == nextBucket.getCount()) {
                    nextBucket.getCounterList().addAfterLast(counterNode);
                    break;
                } else if (incrementCount > 0 && counter.getCount() > nextBucket.getCount()) {
                    currentNode = nextBucketNode;
                    nextBucketNode = nextBucketNode.getNextNode();
                } else if (incrementCount < 0 && counter.getCount() < nextBucket.getCount()) {
                    currentNode = nextBucketNode;
                    nextBucketNode = nextBucketNode.getPreviousNode();
                } else {
                    nextBucketNode = null;
                }
            }
            if (nextBucketNode == null) {
                Bucket newBucket = new Bucket(counter.getCount());
                newBucket.counterList.addAfterLast(counterNode);
                nextBucketNode = bucketList.addAfterNode(currentNode, newBucket);
            }
            counter.setBucketNode(nextBucketNode);
        }
        if (oldBucket.counterList.isEmpty()) {
            bucketList.remove(oldBucketNode);
        }
    }
private void incrementCounter ( ListNode < Counter < T > > counterNode , int incrementCount ) { Counter < T > counter = counterNode . getValue ( ) ; counter . increaseCount ( incrementCount ) ; ListNode < Bucket > oldBucketNode = counter . getBucketNode ( ) ; Bucket oldBucket = oldBucketNode . getValue ( ) ; oldBucket . getCounterList ( ) . remove ( counterNode ) ; if ( counter . getCount ( ) <= 0 ) { counterMap . remove ( counter . getItem ( ) ) ; } else { ListNode < Bucket > currentNode = oldBucketNode ; ListNode < Bucket > nextBucketNode ; if ( incrementCount > 0 ) { nextBucketNode = oldBucketNode . getNextNode ( ) ; } else { nextBucketNode = oldBucketNode . getPreviousNode ( ) ; } while ( nextBucketNode != null ) { Bucket nextBucket = nextBucketNode . getValue ( ) ; if ( counter . getCount ( ) == nextBucket . getCount ( ) ) { nextBucket . getCounterList ( ) . addAfterLast ( counterNode ) ; break ; } else if ( incrementCount > 0 && counter . getCount ( ) > nextBucket . getCount ( ) ) { currentNode = nextBucketNode ; nextBucketNode = nextBucketNode . getNextNode ( ) ; } else if ( incrementCount < 0 && counter . getCount ( ) < nextBucket . getCount ( ) ) { currentNode = nextBucketNode ; nextBucketNode = nextBucketNode . getPreviousNode ( ) ; } else { nextBucketNode = null ; } } if ( nextBucketNode == null ) { Bucket newBucket = new Bucket ( counter . getCount ( ) ) ; newBucket . counterList . addAfterLast ( counterNode ) ; nextBucketNode = bucketList . addAfterNode ( currentNode , newBucket ) ; } counter . setBucketNode ( nextBucketNode ) ; } if ( oldBucket . counterList . isEmpty ( ) ) { bucketList . remove ( oldBucketNode ) ; } }
Increase the frequency for the node specified and moves the counter to relevant bucket
 @param counterNode    The counter node to increase the frequency of
 @param incrementCount The increment size
Increase the frequency for the node specified and moves the counter to relevant bucket @param counterNode The counter node to increase the frequency of @param incrementCount The increment size
// Increase the counter count // Detaching the counter from the old bucket node // Removing counter if count is 0 // Finding the right bucket for the counter // This may or may not be an adjacent bucket because incrementCount can be aby value // New bucket node needs to be created // Cleaning up