All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.internal.collection.Stack Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-3.1.0).
 * Copyright (c) 2007-2015 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics.internal.collection;

import java.util.function.Consumer;

/**
 * Minimal implementation of stack data-structure. {@code Null} values are not
 * permitted, but not checked.
 *
 * 
{@code
 * final Stack stack = new Stack<>();
 * for (int i = 0; i < 10; ++i) {
 *     stack.push(i);
 * }
 *
 * for (Integer i = stack.pop(); i != null; i = stack.pop()) {
 *     System.out.println(i);
 * }
 * }
* * @author Franz Wilhelmstötter * @version 2.0 — $Date$ * @since 2.0 */ public final class Stack { private Node _tail = null; public int length = 0; public void push(final T value) { _tail = new Node<>(value, _tail); ++length; } public T pop() { T value = null; if (_tail != null) { value = _tail._value; _tail = _tail._previous; --length; } return value; } public void popAll(final Consumer consumer) { for (T element = pop(); element != null; element = pop()) { consumer.accept(element); } } public void clear() { _tail = null; length = 0; } private static final class Node { final T _value; final Node _previous; Node(final T value, final Node previous) { _value = value; _previous = previous; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy