001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.graphs.base.compact; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.List; 023 024import net.automatalib.commons.util.array.ResizingObjectArray; 025import net.automatalib.graphs.BidirectionalGraph; 026 027public class CompactSimpleBidiGraph<EP> extends 028 AbstractCompactSimpleGraph<CompactBidiEdge<EP>, EP> implements BidirectionalGraph<Integer, CompactBidiEdge<EP>> { 029 030 private final ResizingObjectArray inEdges; 031 032 public CompactSimpleBidiGraph() { 033 this.inEdges = new ResizingObjectArray(); 034 } 035 036 public CompactSimpleBidiGraph(int initialCapacity) { 037 super(initialCapacity); 038 this.inEdges = new ResizingObjectArray(initialCapacity); 039 } 040 041 @SuppressWarnings("unchecked") 042 protected List<CompactBidiEdge<EP>> getInEdgeList(int node) { 043 return (List<CompactBidiEdge<EP>>)inEdges.array[node]; 044 } 045 046 @Override 047 protected CompactBidiEdge<EP> createEdge(int source, int target, EP property) { 048 return new CompactBidiEdge<>(source, target, property); 049 } 050 051 @Override 052 public Collection<CompactBidiEdge<EP>> getIncomingEdges(Integer node) { 053 return getIncomingEdges(node.intValue()); 054 } 055 056 public Collection<CompactBidiEdge<EP>> getIncomingEdges(int node) { 057 List<CompactBidiEdge<EP>> inEdges = getInEdgeList(node); 058 return Collections.unmodifiableCollection(inEdges); 059 } 060 061 @Override 062 public Integer getSource(CompactBidiEdge<EP> edge) { 063 return Integer.valueOf(getIntSource(edge)); 064 } 065 066 public int getIntSource(CompactBidiEdge<EP> edge) { 067 return edge.getSource(); 068 } 069 070 /* (non-Javadoc) 071 * @see net.automatalib.graphs.base.compact.AbstractCompactGraph#addIntNode(java.lang.Object) 072 */ 073 @Override 074 public int addIntNode(Void properties) { 075 inEdges.ensureCapacity(size + 1); 076 int node = super.addIntNode(properties); 077 inEdges.array[node] = new ArrayList<CompactBidiEdge<EP>>(); 078 return node; 079 } 080 081 /* (non-Javadoc) 082 * @see net.automatalib.graphs.base.compact.AbstractCompactGraph#connect(int, int, java.lang.Object) 083 */ 084 @Override 085 public CompactBidiEdge<EP> connect(int source, int target, EP property) { 086 CompactBidiEdge<EP> edge = super.connect(source, target, property); 087 List<CompactBidiEdge<EP>> inEdges = getInEdgeList(source); 088 edge.inIndex = inEdges.size(); 089 inEdges.add(edge); 090 return edge; 091 } 092 093 094 095}