001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.geometry.partitioning;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.commons.math3.geometry.Space;
024
025/** Set of {@link BSPTree BSP tree} nodes.
026 * @see BoundaryAttribute
027 * @param <S> Type of the space.
028 * @since 3.4
029 */
030public class NodesSet<S extends Space> implements Iterable<BSPTree<S>> {
031
032    /** List of sub-hyperplanes. */
033    private List<BSPTree<S>> list;
034
035    /** Simple constructor.
036     */
037    public NodesSet() {
038        list = new ArrayList<BSPTree<S>>();
039    }
040
041    /** Add a node if not already known.
042     * @param node node to add
043     */
044    public void add(final BSPTree<S> node) {
045
046        for (final BSPTree<S> existing : list) {
047            if (node == existing) {
048                // the node is already known, don't add it
049                return;
050            }
051        }
052
053        // the node was not known, add it
054        list.add(node);
055
056    }
057
058    /** Add nodes if they are not already known.
059     * @param iterator nodes iterator
060     */
061    public void addAll(final Iterable<BSPTree<S>> iterator) {
062        for (final BSPTree<S> node : iterator) {
063            add(node);
064        }
065    }
066
067    /** {@inheritDoc} */
068    public Iterator<BSPTree<S>> iterator() {
069        return list.iterator();
070    }
071
072}