Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / graph / BitMatrixGraph.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18                 
19 package peersim.graph;
20
21 import java.util.*;
22
23 /**
24 * This class implements a graph which uses a bitmatrix as inner representation
25 * of edges.
26 */
27 public class BitMatrixGraph implements Graph {
28
29
30 // ====================== private fileds ========================
31 // ==============================================================
32
33 private final List<BitSet> sets;
34
35 private final boolean directed;
36
37 // ====================== public constructors ===================
38 // ==============================================================
39
40
41 /**
42 * Constructs a directed graph with the given number of nodes.
43 * The graph has no edges initially. The graph is directed.
44 * @param n size of graph
45 */
46 public BitMatrixGraph( int n ) {
47
48         this(n,true);
49 }
50
51 // ---------------------------------------------------------------
52
53 /**
54 * Constructs an graph with the given number of nodes.
55 * The graph has no edges initially.
56 * @param n size of graph
57 * @param directed if true graph is directed
58 */
59 public BitMatrixGraph( int n, boolean directed ) {
60
61         sets = new ArrayList<BitSet>(n);
62         for(int i=0; i<n; ++i) sets.add(new BitSet());
63         this.directed = directed;
64 }
65
66
67 // ======================= Graph implementations ================
68 // ==============================================================
69
70
71 public boolean isEdge(int i, int j) {
72         
73         return sets.get(i).get(j);
74 }
75
76 // ---------------------------------------------------------------
77
78 public Collection<Integer> getNeighbours(int i) {
79         
80         Set<Integer> result = new HashSet<Integer>();
81         BitSet neighb = sets.get(i);
82         final int max = size();
83         for(int j=0; j<max; ++j)
84         {
85                 if( neighb.get(j) ) result.add(j);
86         }
87
88         return Collections.unmodifiableCollection(result);
89 }
90
91 // ---------------------------------------------------------------
92
93 /** Returns null always */
94 public Object getNode(int i) { return null; }
95         
96 // ---------------------------------------------------------------
97
98 /**
99 * Returns null always. 
100 */
101 public Object getEdge(int i, int j) { return null; }
102
103 // ---------------------------------------------------------------
104
105 public int size() { return sets.size(); }
106
107 // --------------------------------------------------------------------
108         
109 public boolean directed() { return directed; }
110
111 // --------------------------------------------------------------------
112
113 public boolean setEdge(int i, int j) {
114
115         if( i > size() || j > size() || i<0 || j<0 ) throw new
116                 IndexOutOfBoundsException();
117
118         BitSet neighb = sets.get(i);
119         boolean old = neighb.get(j);
120         neighb.set(j);
121         
122         if( !old && !directed )
123         {
124                 neighb = sets.get(j);
125                 neighb.set(i);
126         }
127         
128         return !old;
129 }
130
131 // --------------------------------------------------------------------
132
133 public boolean clearEdge(int i, int j) {
134
135         if( i > size() || j > size() || i<0 || j<0 ) throw new
136                 IndexOutOfBoundsException();
137
138         BitSet neighb = sets.get(i);
139         boolean old = neighb.get(j);
140         neighb.clear(j);
141         
142         if( old && !directed )
143         {
144                 neighb = sets.get(i);
145                 neighb.clear(j);
146         }
147         
148         return old;
149 }
150
151 // --------------------------------------------------------------------
152
153 public int degree(int i) {
154
155         BitSet neighb = sets.get(i);
156         return neighb.cardinality(); // only from jdk 1.4
157 }
158
159 }
160