
com.github.tommyettinger.gand.VertexSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gand Show documentation
Show all versions of gand Show documentation
Pathfinding and other graph algorithms. Based on simple-graphs.
/*
MIT License
Copyright (c) 2020 earlygrey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.github.tommyettinger.gand;
import java.util.*;
import com.github.tommyettinger.gand.NodeMap.NodeIterator;
import com.github.tommyettinger.gand.utils.Errors;
class VertexSet extends AbstractSet {
final NodeMap nodeMap;
VertexSet(NodeMap nodeMap) {
this.nodeMap = nodeMap;
}
@Override
public int size() {
return nodeMap.size;
}
@Override
public boolean isEmpty() {
return nodeMap.size == 0;
}
@Override
public boolean contains(Object o) {
return o != null && nodeMap.contains(o);
}
@Override
public Iterator iterator() {
return new VertexIterator<>(nodeMap);
}
@Override
public Object[] toArray() {
Object[] array = new Object[nodeMap.size];
int index = 0;
Node node = nodeMap.head;
while (node != null) {
array[index++] = node.getObject();
node = node.nextInOrder;
}
return array;
}
@Override
@SuppressWarnings("unchecked")
public T[] toArray(T[] array) {
if (array.length < nodeMap.size) {
array = Arrays.copyOf(array, nodeMap.size);
}
int index = 0;
Node node = nodeMap.head;
while (node != null) {
array[index++] = (T) node.getObject();
node = node.nextInOrder;
}
return array;
}
@Override
public boolean add(V v) {
Errors.throwModificationException();
return false;
}
@Override
public boolean remove(Object o) {
Errors.throwModificationException();
return false;
}
@Override
public boolean addAll(Collection extends V> collection) {
Errors.throwModificationException();
return false;
}
@Override
public boolean removeAll(Collection> collection) {
Errors.throwModificationException();
return false;
}
@Override
public boolean retainAll(Collection> collection) {
Errors.throwModificationException();
return false;
}
@Override
public void clear() {
Errors.throwModificationException();
}
static class VertexIterator implements Iterator {
private final NodeIterator nodeIterator;
VertexIterator(NodeMap nodeMap) {
nodeIterator = new NodeIterator<>(nodeMap);
}
@Override
public boolean hasNext() {
return nodeIterator.hasNext();
}
@Override
public V next() {
return nodeIterator.next().getObject();
}
@Override
public void remove() {
Errors.throwModificationException();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy