Skip to content

Commit ea124f9

Browse files
committed
Change graph interfaces to accommodate larger graphs.
The number of links is now a long value. Instead of returning a collection of links, we now return an iterable (since collections have integer-valued sizes).
1 parent 2f58a00 commit ea124f9

19 files changed

Lines changed: 80 additions & 66 deletions

nodes/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
<dependency>
103103
<groupId>com.github.pbloem</groupId>
104104
<artifactId>kit</artifactId>
105-
<version>v0.1.8</version>
105+
<version>v0.1.9</version>
106106
</dependency>
107107
<!-- <dependency> -->
108108
<!-- <groupId>peterbloem.nl</groupId> -->

nodes/src/main/java/org/nodes/DGraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface DGraph<L> extends Graph<L>
2121
@Override
2222
public DNode<L> get(int i);
2323

24-
public Collection<? extends DLink<L>> links();
24+
public Iterable<? extends DLink<L>> links();
2525

2626
/**
2727
* Adds a new node with the given label

nodes/src/main/java/org/nodes/DTGraph.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ public interface DTGraph<L, T> extends DGraph<L>, TGraph<L, T>
3030
@Override
3131
public DTNode<L, T> get(int i);
3232

33-
public Collection<? extends DTLink<L, T>> links();
33+
public Iterable<? extends DTLink<L, T>> links();
3434

3535
/**
3636
* Adds a new node with the given label
3737
*/
3838
public DTNode<L, T> add(L label);
39-
40-
public int numLinks();
41-
39+
4240
/**
4341
* Returns the node labels
4442
* @return

nodes/src/main/java/org/nodes/DiskDGraph.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class DiskDGraph implements DGraph<String>, FastWalkable<String, DNode<St
7373
private List<List<Integer>> in;
7474
private List<List<Integer>> out;
7575

76-
private int numLinks = 0;
76+
private long numLinks = 0;
7777
private long modCount = 0;
7878

7979
// * changes for any edit which causes the node indices to change
@@ -85,7 +85,7 @@ public class DiskDGraph implements DGraph<String>, FastWalkable<String, DNode<St
8585
private Long hashMod = null;
8686

8787
private boolean sorted = false;
88-
88+
8989
public DiskDGraph(File dir)
9090
{
9191
this(dir, false);
@@ -94,7 +94,7 @@ public DiskDGraph(File dir)
9494
/**
9595
*
9696
* @param dbFile The file containing the graph structure. If the file doesn't exist,
97-
* it will be created. IF it does exist, the graph it contains will be loaded.
97+
* it will be created. If it does exist, the graph it contains will be loaded.
9898
* @param nullLabels If true, all labels will be null (saving some space).
9999
* Adding a node with a nonnull label will result in an exception.
100100
*/
@@ -126,7 +126,7 @@ public int size()
126126
}
127127

128128
@Override
129-
public int numLinks()
129+
public long numLinks()
130130
{
131131
return numLinks;
132132
}
@@ -827,14 +827,22 @@ public List<? extends DNode<String>> nodes()
827827
return new NodeList(Series.series(size()));
828828
}
829829

830+
/**
831+
* NOTE: If hasLongNumLinks is true and the graph has more links than
832+
* Integer.MAX_VALUE, the {size()} of the returned collection
833+
* will be -1.
834+
*
835+
* @return
836+
*/
830837
@Override
831-
public Collection<? extends DLink<String>> links()
838+
public Iterable<? extends DLink<String>> links()
832839
{
833840
return new LinkCollection();
834841
}
835842

836843
/**
837844
* A collection of all links in this graph.
845+
* The iterator it returns can be safely used.
838846
*
839847
* @author Peter
840848
*
@@ -850,7 +858,7 @@ public Iterator<DLink<String>> iterator()
850858
@Override
851859
public int size()
852860
{
853-
return numLinks;
861+
return (int)numLinks();
854862
}
855863

856864
private class LLIterator implements Iterator<DLink<String>>
@@ -1265,7 +1273,8 @@ public static DiskDGraph fromFile(File file, File tmpDir, File dbFile)
12651273

12661274
public void close()
12671275
{
1268-
db.atomicInteger("numLinks").createOrOpen().set(numLinks);
1276+
db.atomicLong("numLinks").createOrOpen().set(numLinks);
1277+
12691278
db.close();
12701279
}
12711280

nodes/src/main/java/org/nodes/Graph.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
* API level.</li>
2121
* </ul>
2222
*
23-
* TODO: Create a separate UGraph & UTGraph. Undirected graphs behave differently than
24-
* directed graphs. Letting Graph be the interface for undirected graphs violates
25-
* LSP.
26-
*
2723
* <h2>A note on equality and inheritance</h2>
2824
* <p>
2925
* Our choice to use inheritance in the definition of graphs presents a problem
@@ -42,7 +38,7 @@
4238
* graphs to be equal, their level must match.
4339
* </p>
4440
*
45-
*
41+
* TODO: Make numLinks() return a long, and make {links()} return an iterable.
4642
*
4743
*
4844
* @param <L>
@@ -67,7 +63,7 @@ public interface Graph<L>
6763
* behavior.
6864
* @return
6965
*/
70-
public Collection<? extends Link<L>> links();
66+
public Iterable<? extends Link<L>> links();
7167

7268
/**
7369
* @return The graph's size in nodes.
@@ -79,7 +75,7 @@ public interface Graph<L>
7975
*/
8076
public Node<L> add(L label);
8177

82-
public int numLinks();
78+
public long numLinks();
8379

8480
/**
8581
* Returns the node labels

nodes/src/main/java/org/nodes/LightDGraph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class LightDGraph<L> implements DGraph<L>, FastWalkable<L, DNode<L>>
4646
private List<List<Integer>> out;
4747
private List<List<Integer>> in;
4848

49-
private int numLinks = 0;
49+
private long numLinks = 0;
5050
private long modCount = 0;
5151

5252
// * changes for any edit which causes the node indices to change
@@ -79,7 +79,7 @@ public int size()
7979
}
8080

8181
@Override
82-
public int numLinks()
82+
public long numLinks()
8383
{
8484
return numLinks;
8585
}
@@ -712,7 +712,7 @@ public List<? extends DNode<L>> nodes()
712712
}
713713

714714
@Override
715-
public Collection<? extends DLink<L>> links()
715+
public Iterable<? extends DLink<L>> links()
716716
{
717717
return new LinkCollection();
718718
}
@@ -734,7 +734,7 @@ public Iterator<DLink<L>> iterator()
734734
@Override
735735
public int size()
736736
{
737-
return numLinks;
737+
return (int)numLinks;
738738
}
739739

740740
private class LLIterator implements Iterator<DLink<L>>

nodes/src/main/java/org/nodes/LightDTGraph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class LightDTGraph<L,T> implements DTGraph<L, T> {
3131
private List<List<T>> outTags;
3232
private List<List<T>> inTags;
3333

34-
private int numLinks = 0;
34+
private long numLinks = 0;
3535
private long modCount = 0;
3636

3737
// * changes for any edit which causes the node indices to change
@@ -121,7 +121,7 @@ public DTNode<L, T> get(int i) {
121121
}
122122

123123
@Override
124-
public Collection<? extends DTLink<L, T>> links() {
124+
public Iterable<? extends DTLink<L, T>> links() {
125125
return new LinkCollection();
126126
}
127127

@@ -140,7 +140,7 @@ public DTNode<L, T> add(L label) {
140140
}
141141

142142
@Override
143-
public int numLinks() {
143+
public long numLinks() {
144144
return numLinks;
145145
}
146146

@@ -960,7 +960,7 @@ public Iterator<DTLink<L,T>> iterator()
960960
@Override
961961
public int size()
962962
{
963-
return numLinks;
963+
return (int)numLinks;
964964
}
965965

966966
private class LLIterator implements Iterator<DTLink<L,T>>

nodes/src/main/java/org/nodes/LightUGraph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class LightUGraph<L> implements UGraph<L>, FastWalkable<L, UNode<L>>
4949

5050
List<List<Integer>> neighbors;
5151

52-
private int numLinks = 0;
52+
private long numLinks = 0;
5353
private long modCount = 0;
5454

5555
// * changes for any edit which causes the node indices to change
@@ -81,7 +81,7 @@ public int size()
8181
}
8282

8383
@Override
84-
public int numLinks()
84+
public long numLinks()
8585
{
8686
return numLinks;
8787
}
@@ -529,7 +529,7 @@ public List<? extends UNode<L>> nodes()
529529
}
530530

531531
@Override
532-
public Collection<? extends ULink<L>> links()
532+
public Iterable<? extends ULink<L>> links()
533533
{
534534
return new LinkCollection();
535535
}
@@ -551,7 +551,7 @@ public Iterator<ULink<L>> iterator()
551551
@Override
552552
public int size()
553553
{
554-
return numLinks;
554+
return (int)numLinks;
555555
}
556556

557557
private class LLIterator implements Iterator<ULink<L>>

nodes/src/main/java/org/nodes/MapDTGraph.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class MapDTGraph<L, T> implements DTGraph<L, T>, HasPersistentNodes, HasP
5454
protected List<MapDTNode> nodeList = new ArrayList<MapDTNode>();
5555
protected Map<L, Set<MapDTNode>> nodes = new LinkedHashMap<L, Set<MapDTNode>>();
5656

57-
protected int numEdges = 0;
57+
protected long numLinks = 0;
5858
protected long modCount = 0;
5959

6060
protected int hash;
@@ -242,7 +242,7 @@ public MapDTLink connect(TNode<L, T> other, T tag)
242242
neighborsTo.add(mdtOther);
243243
mdtOther.neighborsFrom.add(this);
244244

245-
numEdges++;
245+
numLinks++;
246246
modCount++;
247247

248248
return link;
@@ -790,7 +790,7 @@ public void remove()
790790

791791
dead = true;
792792

793-
numEdges--;
793+
numLinks--;
794794
modCount++;
795795
}
796796

@@ -937,9 +937,9 @@ public Set<L> labels()
937937
}
938938

939939
@Override
940-
public int numLinks()
940+
public long numLinks()
941941
{
942-
return numEdges;
942+
return numLinks;
943943
}
944944

945945
@Override
@@ -955,7 +955,7 @@ public List<? extends DTNode<L, T>> nodes()
955955
}
956956

957957
@Override
958-
public Collection<MapDTLink> links()
958+
public Iterable<MapDTLink> links()
959959
{
960960
return new LinkCollection();
961961
}
@@ -971,7 +971,7 @@ public Iterator<MapDTLink> iterator()
971971
@Override
972972
public int size()
973973
{
974-
return numLinks();
974+
return (int)numLinks();
975975
}
976976

977977
private class LCIterator implements Iterator<MapDTLink>

nodes/src/main/java/org/nodes/MapUTGraph.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class MapUTGraph<L, T> implements UTGraph<L, T>, HasPersistentNodes, HasP
5454
protected Map<L, Set<MapUTNode>> nodes = new LinkedHashMap<L, Set<MapUTNode>>();
5555
protected Set<T> tags = new LinkedHashSet<T>();
5656

57-
protected int numEdges = 0;
57+
protected long numLinks = 0;
5858
protected long modCount = 0;
5959

6060
protected int hash;
@@ -222,7 +222,7 @@ public MapUTLink connect(TNode<L, T> other, T tag)
222222
neighbors.add(mutOther);
223223
mutOther.neighbors.add(this);
224224

225-
numEdges++;
225+
numLinks++;
226226
modCount++;
227227

228228
return link;
@@ -571,7 +571,7 @@ public void remove()
571571

572572
dead = true;
573573

574-
numEdges --;
574+
numLinks --;
575575
modCount++;
576576
}
577577

@@ -712,9 +712,9 @@ public Set<L> labels()
712712
}
713713

714714
@Override
715-
public int numLinks()
715+
public long numLinks()
716716
{
717-
return numEdges;
717+
return numLinks;
718718
}
719719

720720
@Override
@@ -730,7 +730,7 @@ public List<? extends UTNode<L, T>> nodes()
730730
}
731731

732732
@Override
733-
public Collection<MapUTLink> links()
733+
public Iterable<MapUTLink> links()
734734
{
735735
return new LinkCollection();
736736
}
@@ -747,7 +747,7 @@ public Iterator<MapUTLink> iterator()
747747
@Override
748748
public int size()
749749
{
750-
return numLinks();
750+
return (int)numLinks();
751751
}
752752

753753
private class LCIterator implements Iterator<MapUTLink>

0 commit comments

Comments
 (0)