2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.graph;
24 * This class is an adaptor for representing special subgraphs of any graph.
25 * It can represent the subgraphs spanned by the nodes 0,...,i where
26 * i is less than or equal to n-1, the last node of the original graph.
27 * The underlying graph is stored by reference. This means that if the
28 * graph changes, then these changes will be reflected by this class as well.
29 * Besides, the size of the prefix can be changed at will at any time
30 * using {@link #setSize}.
32 public class PrefixSubGraph implements Graph {
35 // ====================== private fileds ========================
36 // ==============================================================
39 private final Graph g;
41 /** The graph represents the subgraph defined by nodes 0,...,prefSize */
45 // ====================== public constructors ===================
46 // ==============================================================
50 * Constructs an initially max size subgraph of g. That is, the subgraph will
53 public PrefixSubGraph( Graph g ) {
60 // ======================= Graph implementations ================
61 // ==============================================================
64 public boolean isEdge(int i, int j) {
66 if( i<0 || i>=prefSize ) throw new IndexOutOfBoundsException();
67 if( j<0 || j>=prefSize ) throw new IndexOutOfBoundsException();
71 // ---------------------------------------------------------------
73 public Collection<Integer> getNeighbours(int i) {
75 if( i<0 || i>=prefSize ) throw new IndexOutOfBoundsException();
77 List<Integer> result = new LinkedList<Integer>();
78 for(Integer j:g.getNeighbours(i))
80 if( j < prefSize ) result.add(j);
83 return Collections.unmodifiableCollection(result);
86 // ---------------------------------------------------------------
88 public Object getNode(int i) {
90 if( i<0 || i>=prefSize ) throw new IndexOutOfBoundsException();
94 // ---------------------------------------------------------------
97 * Returns the edge in the original graph if both i and j are smaller than
100 public Object getEdge(int i, int j) {
102 if( isEdge(i,j) ) return g.getEdge(i,j);
106 // --------------------------------------------------------------------
108 public int size() { return prefSize; }
110 // --------------------------------------------------------------------
112 public boolean directed() { return g.directed(); }
114 // --------------------------------------------------------------------
117 public boolean setEdge( int i, int j ) {
119 throw new UnsupportedOperationException();
122 // ---------------------------------------------------------------
125 public boolean clearEdge( int i, int j ) {
127 throw new UnsupportedOperationException();
130 // ---------------------------------------------------------------
132 public int degree(int i) {
134 if( i<0 || i>=prefSize ) throw new IndexOutOfBoundsException();
139 // ================= public functions =================================
140 // ====================================================================
144 * Sets the size of the subgraph. If i is negative, it is changed to 0 and
145 * if it is larger than the underlying graph size, it is changed to the
146 * underlying graph size (set at construction time).
149 public int setSize(int i) {
153 if( i > g.size() ) i=g.size();