Java Code Examples for java.util.ArrayDeque#getFirst()

The following examples show how to use java.util.ArrayDeque#getFirst() . 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: FormattingXmlStreamWriter.java    From galleon with Apache License 2.0 6 votes vote down vote up
public void writeStartElement(final String localName) throws XMLStreamException {
    ArrayDeque<String> namespaces = unspecifiedNamespaces;
    String namespace = namespaces.getFirst();
    if (namespace != NO_NAMESPACE) {
        writeStartElement(namespace, localName);
        return;
    }

    unspecifiedNamespaces.push(namespace);

    // If this is a nested element flush the outer
    nl();
    indent();
    delegate.writeStartElement(localName);

    level++;
    state = START_ELEMENT;
    indentEndElement = false;
}
 
Example 2
Source File: Solution5.java    From LeetCode-Solution-in-Good-Style with Apache License 2.0 6 votes vote down vote up
public int[] maxSlidingWindow(int[] nums, int k) {
    int len = nums.length;
    if (len == 0) {
        return new int[0];
    }
    int[] res = new int[len - k + 1];
    ArrayDeque<Integer> queue = new ArrayDeque<>(len);
    for (int i = 0; i < len; i++) {
        // 左边界滑出
        if (i >= k && queue.getFirst() == i - k) {
            queue.removeFirst();
        }

        // 在 nums[i] 加入之前考虑把不可能的值弹出
        while (!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
            queue.removeLast();
        }

        queue.add(i);
        // 记录结果
        if (i >= k - 1) {
            res[i - k + 1] = nums[queue.getFirst()];
        }
    }
    return res;
}
 
Example 3
Source File: Solution8.java    From LeetCode-Solution-in-Good-Style with Apache License 2.0 5 votes vote down vote up
public int[] maxSlidingWindow(int[] nums, int k) {
    int len = nums.length;
    // 特判
    if (len == 0) {
        return new int[]{};
    }
    // 结果集
    List<Integer> res = new ArrayList<>();
    // 滑动窗口,注意:保存的是索引值
    ArrayDeque<Integer> deque = new ArrayDeque<>(k);

    for (int i = 0; i < len; i++) {
        // 当元素从左边界滑出的时候,如果它恰恰好是滑动窗口的最大值
        // 那么将它弹出
        if (i >= k && i - k == deque.getFirst()) {
            deque.pollFirst();
        }

        // 如果滑动窗口非空,新进来的数比队列里已经存在的数还要大
        // 则说明已经存在数一定不会是滑动窗口的最大值(它们毫无出头之日)
        // 将它们弹出
        while (!deque.isEmpty() && nums[deque.peekLast()] <= nums[i]) {
            deque.pollLast();
        }
        deque.add(i);
        // 队首一定是滑动窗口的最大值的索引
        if (i >= k - 1) {
            res.add(nums[deque.peekFirst()]);
        }
    }

    int size = res.size();
    int[] result = new int[size];

    for (int i = 0; i < size; i++) {
        result[i] = res.get(i);
    }
    return result;
}
 
Example 4
Source File: ArrayDequeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getFirst() returns first element, or throws NSEE if empty
 */
public void testFirstElement() {
    ArrayDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.getFirst());
        assertEquals(i, q.pollFirst());
    }
    try {
        q.getFirst();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 5
Source File: ArrayDequeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getFirst() returns first element, or throws NSEE if empty
 */
public void testFirstElement() {
    ArrayDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.getFirst());
        assertEquals(i, q.pollFirst());
    }
    try {
        q.getFirst();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 6
Source File: FormattingXMLStreamWriter.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void writeStartElement(final String localName) throws XMLStreamException {
    ArrayDeque<String> namespaces = unspecifiedNamespaces;
    String namespace = namespaces.getFirst();
    if (namespace == null ? NO_NAMESPACE != null : ! namespace.equals(NO_NAMESPACE)) {
        writeStartElement(namespace, localName);
        return;
    }

    unspecifiedNamespaces.push(namespace);

    // If this is a nested element flush the outer
    runAttrQueue();
    nl();
    indent();
    attrQueue.add(new ArgRunnable() {
        public void run(int arg) throws XMLStreamException {
            if (arg == 0) {
                delegate.writeStartElement(localName);
            } else {
                delegate.writeEmptyElement(localName);
            }
        }
    });

    level++;
    state = START_ELEMENT;
    indentEndElement = false;
}
 
Example 7
Source File: FormattingXmlStreamWriter.java    From galleon with Apache License 2.0 4 votes vote down vote up
private String nestUnspecifiedNamespace() {
    ArrayDeque<String> namespaces = unspecifiedNamespaces;
    String clone = namespaces.getFirst();
    namespaces.push(clone);
    return clone;
}
 
Example 8
Source File: DefaultExplorerFactory.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
@Override
public ExplorerElement create(PresentationEnvironment env) {
	MetaResource rootResource = (MetaResource) env.getElement();
	MetaResource nestedResource = rootResource;

	ArrayDeque<MetaAttribute> attributePath = env.getAttributePath();
	PreconditionUtil.verify(attributePath == null || attributePath.size() < 2, "only relationships supported");
	MetaAttribute relationshipAttribute = attributePath != null ? attributePath.getFirst() : null;
	if (relationshipAttribute != null) {
		nestedResource = (MetaResource) relationshipAttribute.getType().getElementType();
	}

	PresentationService service = env.getService();

	DataTableElement table = createTable(env, nestedResource);

	ExplorerElement explorerElement = new ExplorerElement();
	explorerElement.setId(toId(service, nestedResource));
	explorerElement.setComponentId("explorer");
	explorerElement.setTable(table);
	explorerElement.addAction(createRefreshAction());
	if (rootResource != nestedResource) {
		explorerElement.setPath(StringUtils.nullToEmpty(service.getPath()) + rootResource.getResourcePath() + "/" + relationshipAttribute.getName());

		// no need to show parent again when nested
		MetaAttribute oppositeAttribute = relationshipAttribute.getOppositeAttribute();
		if (oppositeAttribute != null) {
			TableColumnElement oppositeColumn = table.getColumns().getElements().get(oppositeAttribute.getName());
			if (oppositeColumn != null) {
				table.getColumns().remove(oppositeColumn);
			}
		}
	}
	else {
		explorerElement.setPath(service.getPath() + rootResource.getResourcePath());
	}
	explorerElement.setServiceName(service.getServiceName());
	explorerElement.setServicePath(service.getPath());
	explorerElement.setFullTextSearchPaths(getSearchPaths(nestedResource));

	if (nestedResource.isInsertable()) {
		explorerElement.addAction(createPostAction());
	}

	QueryElement query = explorerElement.getBaseQuery();
	query.setResourceType(nestedResource.getResourceType());
	query.setInclusions(PresentationBuilderUtils.computeIncludes(explorerElement.getTable().getChildren()));
	return explorerElement;
}
 
Example 9
Source File: FormattingXMLStreamWriter.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private String nestUnspecifiedNamespace() {
    ArrayDeque<String> namespaces = unspecifiedNamespaces;
    String clone = namespaces.getFirst();
    namespaces.push(clone);
    return clone;
}