Java Code Examples for java.util.Deque#offerLast()

The following examples show how to use java.util.Deque#offerLast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NullAwayNativeModels.java    From NullAway with MIT License 6 votes vote down vote up
static void dequeStuff() {
  Deque<Object> d = new ArrayDeque<>();
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.add(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.addFirst(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.addLast(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offerFirst(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offerLast(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.offer(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  d.push(null);
  Object[] o = null;
  // BUG: Diagnostic contains: passing @Nullable parameter 'o' where @NonNull is required
  d.toArray(o);
}
 
Example 2
Source File: MemoryManager.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void free(ByteBuffer buf) {
    ThreadData local = getThreadData();
    if (_isDebugEnabled) {
        local.allocated -= buf.capacity();
    }
    if (_isTraceEnabled) {
        if (local.traces == null) {
            local.traces = new HashMap<>();
        }
        local.traces.remove(UberUtil.getAddress(buf));
    }
    if (buf.capacity() <= MAX_CACHE_BLOCK_SIZE) {
        if ((buf.capacity() + local.pooled) <= MAX_CACHE_SIZE_PER_THREAD) {
            int index = 32 - Integer.numberOfLeadingZeros(buf.capacity()-1);
            Deque<ByteBuffer> q = local.buffers[index];
            buf.clear();
            q.offerLast(buf);
            local.pooled += buf.capacity();
            buf = null;
        }
    }
    if (buf != null) {
        _log.trace("ByteBuffer free {}", buf.capacity());
        Unsafe.free(buf);
    }
}
 
Example 3
Source File: SerializeDeserializeBinaryTree.java    From interview with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize Tree using level order traversal.
 */
public Node deserializeLevelOrder(String data) {
    if (data == null || data.length() == 0) {
        return null;
    }
    String[] input = data.split(",");
    Deque<Node> queue = new LinkedList<>();
    int index = 0;
    queue.offerFirst(Node.newNode(Integer.parseInt(input[index])));
    Node root = queue.peekFirst();
    index++;
    while (!queue.isEmpty()) {
        Node current = queue.pollFirst();
        if (index < input.length && !input[index].equals("%")) {
            current.left = Node.newNode(Integer.parseInt(input[index]));
            queue.offerLast(current.left);
        }
        index++;
        if (index < input.length && !input[index].equals("%")) {
            current.right = Node.newNode(Integer.parseInt(input[index]));
            queue.offerLast(current.right);
        }
        index++;
    }
    return root;
}
 
Example 4
Source File: Solution116.java    From LeetCodeSolution with Apache License 2.0 5 votes vote down vote up
/**
 * 1.About Complexity
 *     1.1 Time Complexity is O(n)
 *     1.2 Space Complexity is O(1)
 * 2.how I solve
 *     2.1 this solution is base on level order
 *     2.2 define a queue(cache all node),a list(cache node from each floor),a integer(cache current queue size),a node(cache last circulation node)
 *     2.3 get all node from queue and put them to list
 *     2.4 circulate to get list's element
 *          2.4.1 add children(right children first,left children last) to queue
 *          2.4.2 judge whether i is 0
 *                  2.4.2.1 i=0,set lastNode to current node
 *                  2.4.2.2 i!=0,set current node.next to lastNode and lastNode to temp
 *     2.5 clear list and make lastNode to null
 * 3.About submit record
 *     3.1 1ms and 35.4MB memory in LeetCode China
 *     3.2 1ms and 36.1MB memory in LeetCode
 * 4.Q&A
 * @param root
 * @return
 */
public Node connectByLevelOrder(Node root) {
    if(root==null){
        return root;
    }
    Deque<Node> queue=new LinkedList<>();
    List<Node> list=new ArrayList<>();
    Node lastNode=null;
    int size;
    queue.offerLast(root);
    while(queue.size()!=0){
        size=queue.size();
        for(int i=0;i<size;i++){
            list.add(queue.pollFirst());
        }
        for(int i=0;i<list.size();i++){
            Node temp=list.get(i);
            if(temp.right!=null){
                queue.offerLast(temp.right);
            }
            if(temp.left!=null){
                queue.offerLast(temp.left);
            }
            if(i==0){
                lastNode=temp;
            }
            else{
                temp.next=lastNode;
                lastNode=temp;
            }
        }
        lastNode=null;
        list.clear();
    }
    return root;
}
 
Example 5
Source File: _059_Max_Value_In_Slide_Window.java    From algorithm-primer with MIT License 5 votes vote down vote up
public ArrayList<Integer> maxInWindows(int[] num, int size) {
    ArrayList<Integer> result = new ArrayList<>();
    if (num == null || num.length == 0 || size < 1) return result;

    Deque<Integer> deque = new LinkedList<>();
    for (int i = 0; i < num.length; i ++){

        // 保证添加新的元素之前,窗口中首尾元素下标之差最大是size
        if (i > 0 && !deque.isEmpty()){
            int firstIdx = deque.peekFirst();
            int diff = i - firstIdx;
            if (diff == size) {
                deque.pollFirst();
            }
        }

        // 同一个窗口中的元素如果小于新元素,则被删除
        // 由于前面的元素总是大于后面的元素,所以从后面开始删除
        while(!deque.isEmpty() && num[i] >= num[deque.peekLast()]) {
            deque.pollLast();
        }

        // 新元素总是会被添加到双端队列的末尾
        deque.offerLast(i);

        // 双端队列的队头存放的是一个窗口的最大值
        if (i >= size-1) {
            result.add(num[deque.peekFirst()]);
        }
    }
    return result;
}
 
Example 6
Source File: Test65.java    From algorithm-primer with MIT License 5 votes vote down vote up
public ArrayList<Integer> maxInWindows(int[] num, int size) {
    ArrayList<Integer> result = new ArrayList<>();
    if (num == null || num.length == 0 || size < 1) return result;

    Deque<Integer> deque = new LinkedList<>();
    for (int i = 0; i < num.length; i ++){
        // 保证添加新的元素之前,窗口中首尾元素下标之差最大是size
        if (i > 0 && !deque.isEmpty()){
            int firstIdx = deque.peekFirst();
            int diff = i - firstIdx;
            if (diff == size)
                deque.pollFirst();
        }
        /*
        if (!deque.isEmpty() && num[i] > deque.peekFirst()){
            deque.clear();
        }else{
            while(!deque.isEmpty() && num[i] >= deque.peekLast())
                deque.pollLast();
        }
        */

        // 同一个窗口中的元素如果小于新元素,则被删除
        // 由于前面的元素总是大于后面的元素,所以从后面开始删除
        while(!deque.isEmpty() && num[i] >= num[deque.peekLast()])
            deque.pollLast();

        // 新元素总是会被添加到双端队列的末尾
        deque.offerLast(i);

        // 双端队列的队头存放的是一个窗口的最大值
        if (i >= size-1)
            result.add(num[deque.peekFirst()]);
    }
    return result;
}
 
Example 7
Source File: RedissonDequeTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testOfferLastOrigin() {
    Deque<Integer> queue = new ArrayDeque<Integer>();
    queue.offerLast(1);
    queue.offerLast(2);
    queue.offerLast(3);

    assertThat(queue).containsExactly(1, 2, 3);

    Assert.assertEquals((Integer)1, queue.poll());
}
 
Example 8
Source File: MaximumOfSubarrayOfSizeK.java    From interview with Apache License 2.0 5 votes vote down vote up
public int[] maxSubArray(int input[], int k) {
    Deque<Integer> queue = new LinkedList<Integer>();
    int max[] = new int[input.length - k + 1];
    int maxVal = Integer.MIN_VALUE;
    //first find max of first k values and make it 0th element of max array
    for (int i = 0; i < k; i++) {
        if(maxVal < input[i]){
            maxVal = input[i];
        }
        if (queue.isEmpty()) {
            queue.offerLast(i);
        } else {
            while (!queue.isEmpty() && input[queue.peekLast()] <= input[i]) {
                queue.pollLast();
            }
            queue.offerLast(i);
        }
        
    }
    max[0] = maxVal;
    int index=1;
    //continue from k till end of the input array
    for (int i = k; i < input.length; i++) {
        //if index of peek is k distance from i then its no value to us.
        //throw it away
        if (i - k + 1 > queue.peekFirst()) {
            queue.pollFirst();
        }
        while (!queue.isEmpty() && input[queue.peekLast()] <= input[i]) {
            queue.pollLast();
        }
        queue.offerLast(i);
        //Only reason first element survived was because it was biggest element.
        //make it the max value for this k
        max[index] = input[queue.peekFirst()];
        index++;
    }
    return max;
}
 
Example 9
Source File: TreeTraversalInSpiralOrder.java    From interview with Apache License 2.0 5 votes vote down vote up
/**
 * One deque with delimiter to print tree in spiral order
 */
public void spiralWithOneDequeDelimiter(Node root)
{
    if(root == null){
        return;
    }
    Deque<Node> q = new LinkedList<>();
    q.offer(null);
    q.offerFirst(root);
    //if only delimiter(in this case null) is left in queue then break
    while(q.size() > 1){
        root = q.peekFirst();
        while(root != null){
            root = q.pollFirst();
            System.out.print(root.data + " ");
            if(root.left != null){
                q.offerLast(root.left);
            }
            if(root.right != null){
                q.offerLast(root.right);
            }
            root = q.peekFirst();
        }
        root = q.peekLast();
        while(root != null){
            System.out.print(root.data + " ");
            root = q.pollLast();
            if(root.right != null){
                q.offerFirst(root.right);
            }
            if(root.left != null){
                q.offerFirst(root.left);
            }
            root = q.peekLast();
        }
    }
}
 
Example 10
Source File: ConstantTreeAnalyzer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void filteredPush(Deque<AbstractBlockBase<?>> worklist, AbstractBlockBase<?> block) {
    if (isMarked(block)) {
        Debug.log(Debug.VERBOSE_LOG_LEVEL, "adding %s to the worklist", block);
        worklist.offerLast(block);
    }
}
 
Example 11
Source File: TreeTraversalInSpiralOrder.java    From interview with Apache License 2.0 4 votes vote down vote up
/**
 * One deque with count method to print tree in spiral order
 */
public void spiralWithOneDeque(Node root) {
    if (root == null) {
        return;
    }
    Deque<Node> deque = new LinkedList<Node>();
    deque.offerFirst(root);
    int count = 1;
    boolean flip = true;
    while (!deque.isEmpty()) {
        int currentCount = 0;
        while (count > 0) {
            if (flip) {
                root = deque.pollFirst();
                System.out.print(root.data + " ");
                if (root.left != null) {
                    deque.offerLast(root.left);
                    currentCount++;
                }
                if (root.right != null) {
                    deque.offerLast(root.right);
                    currentCount++;
                }
            } else {
                root = deque.pollLast();
                System.out.print(root.data + " ");
                if (root.right != null) {
                    deque.offerFirst(root.right);
                    currentCount++;
                }
                if (root.left != null) {
                    deque.offerFirst(root.left);
                    currentCount++;
                }
            }
            count--;
        }
        flip = !flip;
        count = currentCount;
    }
}