• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gephi / graphstore / #668

21 Nov 2026 04:07PM UTC coverage: 91.979% (+0.005%) from 91.974%
#668

push

web-flow
Make getEdge and getEdges(n1, n2) cover all edge types (#370)

* Make getEdge and getEdges(n1, n2) cover all edge types

The untyped pair lookups only looked at the default edge type, so edges of
other types between two nodes were invisible to getEdge(n1, n2) and
getEdges(n1, n2). They now cover every type: getEdge returns the first edge
of the lowest type and getEdges groups the edges by type, in ascending
order.

In views, getEdge(n1, n2) returns the first edge the view contains instead
of null when the store's first edge is filtered out. getEdges(n1, n2) now
looks up the edges under the read lock: the lookup ran before the iterator
took the lock, so a concurrent write could leave it with stale ids. The
undirected decorator and undirected views no longer return a directed
self-loop twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Describe the pair lookup helpers by the key they read

With the undirected decorator on a mixed graph, an undirected edge comes
from either helper, depending on which node has the larger store id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

70 of 76 new or added lines in 2 files covered. (92.11%)

2 existing lines in 2 files now uncovered.

12511 of 13602 relevant lines covered (91.98%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.74
/src/main/java/org/gephi/graph/impl/EdgeStore.java
1
/*
2
 * Copyright 2012-2013 Gephi Consortium
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16

17
package org.gephi.graph.impl;
18

19
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenCustomHashMap;
20
import it.unimi.dsi.fastutil.longs.LongHash;
21
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
22
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
23
import it.unimi.dsi.fastutil.objects.ObjectSet;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.BitSet;
27
import java.util.Collection;
28
import java.util.Collections;
29
import java.util.ConcurrentModificationException;
30
import java.util.HashSet;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Set;
34
import java.util.Spliterator;
35
import java.util.function.Consumer;
36
import java.util.function.Predicate;
37
import java.util.stream.Stream;
38
import java.util.stream.StreamSupport;
39
import org.gephi.graph.api.Edge;
40
import org.gephi.graph.api.EdgeIterable;
41
import org.gephi.graph.api.Node;
42

43
public class EdgeStore implements Collection<Edge>, EdgeIterable {
44

45
    // Const
46
    protected final static int NULL_ID = -1;
47
    protected final static int NODE_BITS = 31;
48
    protected static final Iterator<Edge> EMPTY_EDGE_ITERATOR = Collections.emptyIterator();
1 ✔
49
    // Locking (optional)
50
    protected final GraphLockImpl lock;
51
    // Version
52
    protected final GraphVersion version;
53
    // Types counting (optional)
54
    protected final EdgeTypeStore edgeTypeStore;
55
    // View store
56
    protected final GraphViewStore viewStore;
57
    // Spatial index
58
    protected final SpatialIndexImpl spatialIndex;
59
    // Configuration
60
    protected final ConfigurationImpl configuration;
61
    // Data
62
    protected int size;
63
    protected int garbageSize;
64
    protected int blocksCount;
65
    protected int currentBlockIndex;
66
    protected EdgeBlock blocks[];
67
    protected EdgeBlock currentBlock;
68
    protected Object2IntOpenHashMap dictionary;
69
    protected Long2ObjectOpenCustomHashMap<int[]>[] longDictionary;
70
    // Stats
71
    protected volatile int[] typeSize;
72
    protected int undirectedSize;
73
    protected int mutualEdgesSize;
74
    protected int[] mutualEdgesTypeSize;
75
    // Counts seen by the undirected graph (mutual pairs counted once), each written once per mutation
76
    protected int undirectedGraphSize;
77
    protected volatile int[] undirectedGraphTypeSize;
78
    // Graph type derived from undirectedSize and size, published for lock-free reads
79
    private volatile GraphType graphType;
80
    // Number of types with at least one edge, and whether there is more than one
81
    protected int nonEmptyTypeCount;
82
    private volatile boolean multiGraph;
83

84
    public EdgeStore() {
1 ✔
85
        initStore();
1 ✔
86
        this.lock = null;
1 ✔
87
        this.edgeTypeStore = null;
1 ✔
88
        this.viewStore = null;
1 ✔
89
        this.version = null;
1 ✔
90
        this.spatialIndex = null;
1 ✔
91
        this.configuration = new ConfigurationImpl();
1 ✔
92
    }
1 ✔
93

94
    public EdgeStore(final EdgeTypeStore edgeTypeStore, final SpatialIndexImpl spatialIndex, final ConfigurationImpl configuration, final GraphLockImpl lock, final GraphViewStore viewStore, final GraphVersion graphVersion) {
1 ✔
95
        initStore();
1 ✔
96
        this.lock = lock;
1 ✔
97
        this.edgeTypeStore = edgeTypeStore;
1 ✔
98
        this.viewStore = viewStore;
1 ✔
99
        this.version = graphVersion;
1 ✔
100
        this.spatialIndex = spatialIndex;
1 ✔
101
        this.configuration = configuration == null ? new ConfigurationImpl() : configuration;
1 ✔
102
    }
1 ✔
103

104
    protected static long getLongId(NodeImpl source, NodeImpl target, boolean directed) {
105
        if (directed) {
1 ✔
106
            long edgeId = ((long) source.storeId) << NODE_BITS;
1 ✔
107
            edgeId = edgeId | (long) (target.storeId);
1 ✔
108
            return edgeId;
1 ✔
109
        } else {
110
            long edgeId = ((long) (source.storeId > target.storeId ? source.storeId : target.storeId)) << NODE_BITS;
1 ✔
111
            edgeId = edgeId | (long) (source.storeId > target.storeId ? target.storeId : source.storeId);
1 ✔
112
            return edgeId;
1 ✔
113
        }
114
    }
115

116
    private void initStore() {
117
        this.size = 0;
1 ✔
118
        this.garbageSize = 0;
1 ✔
119
        this.blocksCount = 1;
1 ✔
120
        this.currentBlockIndex = 0;
1 ✔
121
        this.blocks = new EdgeBlock[GraphStoreConfiguration.EDGESTORE_DEFAULT_BLOCKS];
1 ✔
122
        this.blocks[0] = new EdgeBlock(0);
1 ✔
123
        this.currentBlock = blocks[currentBlockIndex];
1 ✔
124
        this.dictionary = new Object2IntOpenHashMap(GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE);
1 ✔
125
        this.dictionary.defaultReturnValue(NULL_ID);
1 ✔
126
        this.longDictionary = new Long2ObjectOpenCustomHashMap[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
127
        this.longDictionary[0] = new Long2ObjectOpenCustomHashMap(
1 ✔
128
                GraphStoreConfiguration.EDGESTORE_DEFAULT_DICTIONARY_SIZE,
129
                GraphStoreConfiguration.EDGESTORE_DICTIONARY_LOAD_FACTOR, new DictionaryHashStrategy());
130
        this.mutualEdgesTypeSize = new int[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
131
        this.typeSize = new int[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
132
        this.undirectedGraphTypeSize = new int[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
133
        this.undirectedSize = 0;
1 ✔
134
        this.mutualEdgesSize = 0;
1 ✔
135
        this.undirectedGraphSize = 0;
1 ✔
136
        this.nonEmptyTypeCount = 0;
1 ✔
137
        updateGraphType();
1 ✔
138
        updateMultiGraph();
1 ✔
139
    }
1 ✔
140

141
    private void ensureCapacity(final int capacity) {
142
        assert capacity > 0;
1 ✔
143

144
        int blockCapacity = currentBlock.getCapacity();
1 ✔
145
        while (capacity > blockCapacity) {
1 ✔
146
            if (currentBlockIndex == blocksCount - 1) {
1 ✔
147
                int blocksNeeded = (int) Math
1 ✔
148
                        .ceil((capacity - blockCapacity) / (double) GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE);
1 ✔
149
                for (int i = 0; i < blocksNeeded; i++) {
1 ✔
150
                    if (blocksCount == blocks.length) {
1 ✔
151
                        EdgeBlock[] newBlocks = new EdgeBlock[blocksCount + 1];
1 ✔
152
                        System.arraycopy(blocks, 0, newBlocks, 0, blocks.length);
1 ✔
153
                        blocks = newBlocks;
1 ✔
154
                    }
155
                    EdgeBlock block = blocks[blocksCount];
1 ✔
156
                    if (block == null) {
1 ✔
157
                        block = new EdgeBlock(blocksCount);
1 ✔
158
                        blocks[blocksCount] = block;
1 ✔
159
                    }
160
                    if (blockCapacity == 0 && i == 0) {
1 ✔
161
                        currentBlockIndex = blocksCount;
1 ✔
162
                        currentBlock = block;
1 ✔
163
                    }
164
                    blocksCount++;
1 ✔
165
                }
166
                break;
1 ✔
167
            } else {
168
                currentBlockIndex++;
1 ✔
169
                currentBlock = blocks[currentBlockIndex];
1 ✔
170
                blockCapacity = currentBlock.getCapacity();
1 ✔
171
            }
172
        }
173
    }
1 ✔
174

175
    private void trimDictionary() {
176
        dictionary.trim(Math.max(GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE, size * 2));
1 ✔
177
    }
1 ✔
178

179
    private void ensureHeadOutCapacity(final NodeImpl node, final int type) {
180
        EdgeImpl[] out = node.headOut;
1 ✔
181
        int outLength = out.length;
1 ✔
182
        if (type >= outLength) {
1 ✔
183
            EdgeImpl[] newArray = new EdgeImpl[type + 1];
1 ✔
184
            System.arraycopy(out, 0, newArray, 0, outLength);
1 ✔
185
            node.headOut = newArray;
1 ✔
186
        }
187
    }
1 ✔
188

189
    private void ensureHeadInCapacity(NodeImpl node, final int type) {
190
        EdgeImpl[] in = node.headIn;
1 ✔
191
        int inLength = in.length;
1 ✔
192
        if (type >= inLength) {
1 ✔
193
            EdgeImpl[] newArray = new EdgeImpl[type + 1];
1 ✔
194
            System.arraycopy(in, 0, newArray, 0, inLength);
1 ✔
195
            node.headIn = newArray;
1 ✔
196
        }
197
    }
1 ✔
198

199
    private void trimHeadOutCapacity(NodeImpl node, int length) {
200
        EdgeImpl[] out = node.headOut;
1 ✔
201
        int outLength = out.length;
1 ✔
202
        if (length < outLength) {
1 ✔
203
            EdgeImpl[] newArray = new EdgeImpl[length];
1 ✔
204
            System.arraycopy(out, 0, newArray, 0, length);
1 ✔
205
            node.headOut = newArray;
1 ✔
206
        }
207
    }
1 ✔
208

209
    private void trimHeadInCapacity(NodeImpl node, int length) {
210
        EdgeImpl[] in = node.headIn;
1 ✔
211
        int inLength = in.length;
1 ✔
212
        if (length < inLength) {
1 ✔
213
            EdgeImpl[] newArray = new EdgeImpl[length];
1 ✔
214
            System.arraycopy(in, 0, newArray, 0, length);
1 ✔
215
            node.headIn = newArray;
1 ✔
216
        }
217
    }
1 ✔
218

219
    private void ensureLongDictionaryCapacity(int type) {
220
        int length = longDictionary.length;
1 ✔
221
        if (type >= length) {
1 ✔
222
            Long2ObjectOpenCustomHashMap[] newArray = new Long2ObjectOpenCustomHashMap[type + 1];
1 ✔
223
            System.arraycopy(longDictionary, 0, newArray, 0, length);
1 ✔
224
            for (int i = length; i <= type; i++) {
1 ✔
225
                Long2ObjectOpenCustomHashMap newMap = new Long2ObjectOpenCustomHashMap(
1 ✔
226
                        GraphStoreConfiguration.EDGESTORE_DEFAULT_DICTIONARY_SIZE,
227
                        GraphStoreConfiguration.EDGESTORE_DICTIONARY_LOAD_FACTOR, new DictionaryHashStrategy());
228
                newArray[i] = newMap;
1 ✔
229
            }
230
            int[] newMutualEdgesTypeSize = new int[type + 1];
1 ✔
231
            System.arraycopy(mutualEdgesTypeSize, 0, newMutualEdgesTypeSize, 0, length);
1 ✔
232
            int[] newTypeSize = new int[type + 1];
1 ✔
233
            System.arraycopy(typeSize, 0, newTypeSize, 0, length);
1 ✔
234
            int[] newUndirectedGraphTypeSize = new int[type + 1];
1 ✔
235
            System.arraycopy(undirectedGraphTypeSize, 0, newUndirectedGraphTypeSize, 0, length);
1 ✔
236

237
            // Assigned only once all arrays are allocated, so a failed allocation leaves the store unchanged
238
            longDictionary = newArray;
1 ✔
239
            mutualEdgesTypeSize = newMutualEdgesTypeSize;
1 ✔
240
            typeSize = newTypeSize;
1 ✔
241
            undirectedGraphTypeSize = newUndirectedGraphTypeSize;
1 ✔
242
        }
243
    }
1 ✔
244

245
    private void insertOutEdge(EdgeImpl edge) {
246
        NodeImpl source = edge.source;
1 ✔
247
        int type = edge.type;
1 ✔
248

249
        ensureHeadOutCapacity(source, type);
1 ✔
250

251
        int edgeId = edge.getStoreId();
1 ✔
252
        EdgeImpl[] headOutArray = source.headOut;
1 ✔
253
        EdgeImpl headOutEdge = headOutArray[type];
1 ✔
254
        if (headOutEdge != null) {
1 ✔
255
            headOutEdge.previousOutEdge = edgeId;
1 ✔
256
            edge.nextOutEdge = headOutEdge.storeId;
1 ✔
257
        }
258
        headOutArray[type] = edge;
1 ✔
259
    }
1 ✔
260

261
    private void insertInEdge(EdgeImpl edge) {
262
        NodeImpl target = edge.target;
1 ✔
263
        int type = edge.type;
1 ✔
264

265
        ensureHeadInCapacity(target, type);
1 ✔
266

267
        int edgeId = edge.getStoreId();
1 ✔
268
        EdgeImpl[] headInArray = target.headIn;
1 ✔
269
        EdgeImpl headInEdge = headInArray[type];
1 ✔
270
        if (headInEdge != null) {
1 ✔
271
            headInEdge.previousInEdge = edgeId;
1 ✔
272
            edge.nextInEdge = headInEdge.storeId;
1 ✔
273
        }
274
        headInArray[type] = edge;
1 ✔
275
    }
1 ✔
276

277
    private void removeOutEdge(EdgeImpl edge) {
278
        int previousOutEdgeId = edge.previousOutEdge;
1 ✔
279
        int nextOutEdgeId = edge.nextOutEdge;
1 ✔
280
        int type = edge.type;
1 ✔
281

282
        EdgeImpl nextOutEdge = null;
1 ✔
283
        if (nextOutEdgeId != EdgeStore.NULL_ID) {
1 ✔
284
            nextOutEdge = get(nextOutEdgeId);
1 ✔
285
            nextOutEdge.previousOutEdge = previousOutEdgeId;
1 ✔
286
        }
287

288
        if (previousOutEdgeId == EdgeStore.NULL_ID) {
1 ✔
289
            NodeImpl source = edge.source;
1 ✔
290
            EdgeImpl[] headOutArray = source.headOut;
1 ✔
291
            headOutArray[type] = nextOutEdge;
1 ✔
292
            if (nextOutEdge == null && type > GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT - 1 && type == headOutArray.length - 1) {
1 ✔
293
                trimHeadOutCapacity(source, headOutArray.length - 1);
1 ✔
294
            }
295
        } else {
1 ✔
296
            EdgeImpl previousOutEdge = get(previousOutEdgeId);
1 ✔
297
            previousOutEdge.nextOutEdge = nextOutEdgeId;
1 ✔
298
        }
299

300
        edge.nextOutEdge = EdgeStore.NULL_ID;
1 ✔
301
        edge.previousOutEdge = EdgeStore.NULL_ID;
1 ✔
302
    }
1 ✔
303

304
    private void removeInEdge(EdgeImpl edge) {
305
        int previousInEdgeId = edge.previousInEdge;
1 ✔
306
        int nextInEdgeId = edge.nextInEdge;
1 ✔
307
        int type = edge.type;
1 ✔
308

309
        EdgeImpl nextInEdge = null;
1 ✔
310
        if (nextInEdgeId != EdgeStore.NULL_ID) {
1 ✔
311
            nextInEdge = get(nextInEdgeId);
1 ✔
312
            nextInEdge.previousInEdge = previousInEdgeId;
1 ✔
313
        }
314

315
        if (previousInEdgeId == EdgeStore.NULL_ID) {
1 ✔
316
            NodeImpl target = edge.target;
1 ✔
317
            EdgeImpl[] headInArray = target.headIn;
1 ✔
318
            headInArray[type] = nextInEdge;
1 ✔
319
            if (nextInEdge == null && type > GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT - 1 && type == headInArray.length - 1) {
1 ✔
320
                trimHeadInCapacity(target, headInArray.length - 1);
1 ✔
321
            }
322
        } else {
1 ✔
323
            EdgeImpl previousInEdge = get(previousInEdgeId);
1 ✔
324
            previousInEdge.nextInEdge = nextInEdgeId;
1 ✔
325
        }
326

327
        edge.nextInEdge = EdgeStore.NULL_ID;
1 ✔
328
        edge.previousInEdge = EdgeStore.NULL_ID;
1 ✔
329
    }
1 ✔
330

331
    @Override
332
    public void clear() {
333
        if (!isEmpty()) {
1 ✔
334
            incrementVersion();
1 ✔
335
        }
336

337
        for (EdgeStoreIterator itr = new EdgeStoreIterator(); itr.hasNext();) {
1 ✔
338
            EdgeImpl edge = itr.next();
1 ✔
339
            clearEndpoints(edge);
1 ✔
340
            edge.setStoreId(EdgeStore.NULL_ID);
1 ✔
341
            edge.nextOutEdge = EdgeStore.NULL_ID;
1 ✔
342
            edge.previousOutEdge = EdgeStore.NULL_ID;
1 ✔
343
            edge.nextInEdge = EdgeStore.NULL_ID;
1 ✔
344
            edge.previousInEdge = EdgeStore.NULL_ID;
1 ✔
345
            edge.setMutual(false);
1 ✔
346
        }
1 ✔
347

348
        initStore();
1 ✔
349
    }
1 ✔
350

351
    // Resets the adjacency heads and degrees of the edge's source and target
352
    private void clearEndpoints(EdgeImpl edge) {
353
        NodeImpl source = edge.source;
1 ✔
354
        NodeImpl target = edge.target;
1 ✔
355
        if (source.headOut.length > GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT) {
1 ✔
356
            source.headOut = new EdgeImpl[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
357
        } else if (edge.type < source.headOut.length) {
1 ✔
358
            source.headOut[edge.type] = null;
1 ✔
359
        }
360
        if (target.headIn.length > GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT) {
1 ✔
361
            target.headIn = new EdgeImpl[GraphStoreConfiguration.EDGESTORE_DEFAULT_TYPE_COUNT];
1 ✔
362
        } else if (edge.type < target.headIn.length) {
1 ✔
363
            target.headIn[edge.type] = null;
1 ✔
364
        }
365
        source.outDegree = 0;
1 ✔
366
        source.inDegree = 0;
1 ✔
367
        source.mutualDegree = 0;
1 ✔
368
        target.outDegree = 0;
1 ✔
369
        target.inDegree = 0;
1 ✔
370
        target.mutualDegree = 0;
1 ✔
371
    }
1 ✔
372

373
    @Override
374
    public int size() {
375
        return size;
1 ✔
376
    }
377

378
    public int undirectedSize() {
379
        return undirectedGraphSize;
1 ✔
380
    }
381

382
    public int size(int type) {
383
        EdgeTypeStore.checkValidId(type);
1 ✔
384
        int[] sizes = typeSize;
1 ✔
385
        return type < sizes.length ? sizes[type] : 0;
1 ✔
386
    }
387

388
    public int undirectedSize(int type) {
389
        EdgeTypeStore.checkValidId(type);
1 ✔
390
        int[] sizes = undirectedGraphTypeSize;
1 ✔
391
        return type < sizes.length ? sizes[type] : 0;
1 ✔
392
    }
393

394
    @Override
395
    public boolean isEmpty() {
396
        return size == 0;
1 ✔
397
    }
398

399
    @Override
400
    public EdgeStoreIterator iterator() {
401
        return new EdgeStoreIterator();
1 ✔
402
    }
403

404
    @Override
405
    public Spliterator<Edge> spliterator() {
406
        int end = blocksCount;
1 ✔
407
        return new EdgeSpliterator(0, end);
1 ✔
408
    }
409

410
    @Override
411
    public Stream<Edge> stream() {
412
        return StreamSupport.stream(spliterator(), false);
1 ✔
413
    }
414

415
    @Override
416
    public Stream<Edge> parallelStream() {
417
        return StreamSupport.stream(spliterator(), true);
1 ✔
418
    }
419

420
    public Spliterator<Edge> spliteratorUndirected() {
421
        int end = blocksCount;
1 ✔
422
        return new FilteredSizedEdgeSpliterator(0, end, e -> !isUndirectedToIgnore(e), undirectedSize());
1 ✔
423
    }
424

425
    public Spliterator<Edge> spliteratorType(int type, boolean undirected) {
426
        int end = blocksCount;
1 ✔
427
        return new FilteredSizedEdgeSpliterator(0, end,
1 ✔
428
                e -> e.getType() == type && (!undirected || !isUndirectedToIgnore(e)),
1 ✔
429
                undirected ? undirectedSize(type) : size(type));
1 ✔
430
    }
431

432
    public Spliterator<Edge> spliteratorSelfLoop() {
433
        int end = blocksCount;
1 ✔
434
        return new FilteredEdgeSpliterator(0, end, EdgeImpl::isSelfLoop);
1 ✔
435
    }
436

437
    protected Spliterator<Edge> newFilteredSpliterator(java.util.function.Predicate<EdgeImpl> filter) {
438
        int end = blocksCount;
1 ✔
439
        return new FilteredEdgeSpliterator(0, end, filter);
1 ✔
440
    }
441

442
    protected Spliterator<Edge> newFilteredSizedSpliterator(java.util.function.Predicate<EdgeImpl> filter, int size) {
443
        int end = blocksCount;
1 ✔
444
        return new FilteredSizedEdgeSpliterator(0, end, filter, size);
1 ✔
445
    }
446

447
    public EdgeStoreIterator iteratorUndirected() {
448
        return new UndirectedEdgeStoreIterator();
1 ✔
449
    }
450

451
    public SelfLoopIterator iteratorSelfLoop() {
452
        return new SelfLoopIterator();
1 ✔
453
    }
454

455
    public EdgeTypeIterator iteratorType(final int type, final boolean undirected) {
456
        EdgeTypeStore.checkValidId(type);
1 ✔
457
        return new EdgeTypeIterator(type, undirected);
1 ✔
458
    }
459

460
    public EdgeOutIterator edgeOutIterator(final Node node) {
461
        checkValidNodeObject(node);
1 ✔
462
        return new EdgeOutIterator((NodeImpl) node);
1 ✔
463
    }
464

465
    public EdgeInIterator edgeInIterator(final Node node) {
466
        checkValidNodeObject(node);
1 ✔
467
        return new EdgeInIterator((NodeImpl) node);
1 ✔
468
    }
469

470
    public EdgeInOutIterator edgeIterator(final Node node, boolean locking) {
471
        checkValidNodeObject(node);
1 ✔
472
        return new EdgeInOutIterator((NodeImpl) node, locking);
1 ✔
473
    }
474

475
    public EdgeInOutMultiIterator edgeIterator(final Iterator<NodeImpl> nodeIterator, boolean locking) {
476
        return new EdgeInOutMultiIterator(nodeIterator, locking);
1 ✔
477
    }
478

479
    public Iterator<Edge> edgeUndirectedIterator(final Node node, boolean locking) {
480
        checkValidNodeObject(node);
1 ✔
481
        return undirectedIterator(new EdgeInOutIterator((NodeImpl) node, locking));
1 ✔
482
    }
483

484
    public EdgeTypeOutIterator edgeOutIterator(final Node node, int type) {
485
        checkValidNodeObject(node);
1 ✔
486
        return new EdgeTypeOutIterator((NodeImpl) node, type);
1 ✔
487
    }
488

489
    public Iterator<Edge> edgeUndirectedIterator(final Node node, int type) {
490
        checkValidNodeObject(node);
1 ✔
491
        return undirectedIterator(new EdgeTypeInOutIterator((NodeImpl) node, type));
1 ✔
492
    }
493

494
    public EdgeTypeInIterator edgeInIterator(final Node node, int type) {
495
        checkValidNodeObject(node);
1 ✔
496
        return new EdgeTypeInIterator((NodeImpl) node, type);
1 ✔
497
    }
498

499
    public EdgeTypeInOutIterator edgeIterator(final Node node, int type) {
500
        checkValidNodeObject(node);
1 ✔
501
        return new EdgeTypeInOutIterator((NodeImpl) node, type);
1 ✔
502
    }
503

504
    public NeighborsIterator neighborOutIterator(final Node node) {
505
        checkValidNodeObject(node);
1 ✔
506
        return new NeighborsIterator((NodeImpl) node, new EdgeOutIterator((NodeImpl) node));
1 ✔
507
    }
508

509
    public NeighborsIterator neighborOutIterator(final Node node, int type) {
510
        checkValidNodeObject(node);
1 ✔
511
        return new NeighborsIterator((NodeImpl) node, new EdgeTypeOutIterator((NodeImpl) node, type));
1 ✔
512
    }
513

514
    public NeighborsIterator neighborInIterator(final Node node) {
515
        checkValidNodeObject(node);
1 ✔
516
        return new NeighborsIterator((NodeImpl) node, new EdgeInIterator((NodeImpl) node));
1 ✔
517
    }
518

519
    public NeighborsIterator neighborInIterator(final Node node, int type) {
520
        checkValidNodeObject(node);
1 ✔
521
        return new NeighborsIterator((NodeImpl) node, new EdgeTypeInIterator((NodeImpl) node, type));
1 ✔
522
    }
523

524
    public NeighborsIterator neighborIterator(Node node) {
525
        checkValidNodeObject(node);
1 ✔
526
        return new NeighborsUndirectedIterator((NodeImpl) node, new EdgeInOutIterator((NodeImpl) node, true));
1 ✔
527
    }
528

529
    public NeighborsIterator neighborIterator(final Node node, int type) {
530
        checkValidNodeObject(node);
1 ✔
531
        return new NeighborsUndirectedIterator((NodeImpl) node, new EdgeTypeInOutIterator((NodeImpl) node, type));
1 ✔
532
    }
533

534
    public Iterator<Edge> edgesUndirectedIterator(final Node node1, final Node node2) {
535
        checkValidNodeObject(node1);
1 ✔
536
        checkValidNodeObject(node2);
1 ✔
537
        return undirectedIterator(getAll(node1, node2, true));
1 ✔
538
    }
539

540
    public Iterator<Edge> edgesUndirectedIterator(final Node node1, final Node node2, int type) {
541
        checkValidNodeObject(node1);
1 ✔
542
        checkValidNodeObject(node2);
1 ✔
543
        return undirectedIterator(getAll(node1, node2, type, true));
1 ✔
544
    }
545

546
    public EdgeImpl get(int id) {
547
        checkValidId(id);
1 ✔
548

549
        return blocks[id / GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE].get(id);
1 ✔
550
    }
551

552
    // Only used for Graph.getEdgeByStoreId
553
    public EdgeImpl getForGetByStoreId(int id) {
554
        if (id < 0 || !isValidIndex(id)) {
1 ✔
555
            return null;
1 ✔
556
        }
557
        return blocks[id / GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE].get(id);
1 ✔
558
    }
559

560
    public EdgeImpl get(final Object id) {
561
        checkNonNullObject(id);
1 ✔
562

563
        int index = dictionary.getInt(id);
1 ✔
564
        if (index != EdgeStore.NULL_ID) {
1 ✔
565
            return get(index);
1 ✔
566
        }
567
        return null;
1 ✔
568
    }
569

570
    public EdgeImpl get(final Node source, final Node target, boolean undirectedDecorator) {
571
        return get(source, target, undirectedDecorator, null);
1 ✔
572
    }
573

574
    // First edge between source and target, all types, in ascending type order, that the view contains if not null;
575
    // the caller holds the read lock
576
    EdgeImpl get(final Node source, final Node target, boolean undirectedDecorator, GraphViewImpl view) {
577
        checkNonNullObject(source);
1 ✔
578
        checkNonNullObject(target);
1 ✔
579
        NodeImpl sourceImpl = (NodeImpl) source;
1 ✔
580
        NodeImpl targetImpl = (NodeImpl) target;
1 ✔
581

582
        int[] typeSizes = typeSize;
1 ✔
583
        for (int type = 0; type < typeSizes.length; type++) {
1 ✔
584
            if (typeSizes[type] > 0) {
1 ✔
585
                EdgeImpl edge = getInType(sourceImpl, targetImpl, type, undirectedDecorator, view);
1 ✔
586
                if (edge != null) {
1 ✔
587
                    return edge;
1 ✔
588
                }
589
            }
590
        }
591
        return null;
1 ✔
592
    }
593

594
    public Iterator<Edge> getAll(final Node source, final Node target, boolean undirectedDecorator) {
595
        checkNonNullObject(source);
1 ✔
596
        checkNonNullObject(target);
1 ✔
597
        NodeImpl sourceImpl = (NodeImpl) source;
1 ✔
598
        NodeImpl targetImpl = (NodeImpl) target;
1 ✔
599

600
        readLock();
1 ✔
601
        int[] ids = null;
1 ✔
602
        try {
603
            int[] typeSizes = typeSize;
1 ✔
604
            for (int type = 0; type < typeSizes.length; type++) {
1 ✔
605
                if (typeSizes[type] > 0) {
1 ✔
606
                    ids = concat(ids, getAllIdsInType(sourceImpl, targetImpl, type, undirectedDecorator));
1 ✔
607
                }
608
            }
NEW
609
        } catch (Throwable t) {
×
NEW
610
            readUnlock();
×
NEW
611
            throw t;
×
612
        }
1 ✔
613
        return edgesIterator(ids);
1 ✔
614
    }
615

616
    public EdgeImpl get(final Node source, final Node target, final int type, boolean undirectedDecorator) {
617
        checkNonNullObject(source);
1 ✔
618
        checkNonNullObject(target);
1 ✔
619
        EdgeTypeStore.checkValidId(type);
1 ✔
620

621
        if (type < longDictionary.length) {
1 ✔
622
            return getInType((NodeImpl) source, (NodeImpl) target, type, undirectedDecorator, null);
1 ✔
623
        }
624
        return null;
1 ✔
625
    }
626

627
    public Iterator<Edge> getAll(final Node source, final Node target, final int type, boolean undirectedDecorator) {
628
        checkNonNullObject(source);
1 ✔
629
        checkNonNullObject(target);
1 ✔
630
        EdgeTypeStore.checkValidId(type);
1 ✔
631

632
        readLock();
1 ✔
633
        int[] ids = null;
1 ✔
634
        try {
635
            if (type < longDictionary.length) {
1 ✔
636
                ids = getAllIdsInType((NodeImpl) source, (NodeImpl) target, type, undirectedDecorator);
1 ✔
637
            }
NEW
638
        } catch (Throwable t) {
×
NEW
639
            readUnlock();
×
NEW
640
            throw t;
×
641
        }
1 ✔
642
        return edgesIterator(ids);
1 ✔
643
    }
644

645
    private EdgeImpl getInType(NodeImpl source, NodeImpl target, int type, boolean undirectedDecorator, GraphViewImpl view) {
646
        EdgeImpl edge = first(getIdsInType(source, target, type, undirectedDecorator), view);
1 ✔
647
        if (edge == null) {
1 ✔
648
            edge = first(getReverseIdsInType(source, target, type, undirectedDecorator), view);
1 ✔
649
        }
650
        return edge;
1 ✔
651
    }
652

653
    private EdgeImpl first(int[] ids, GraphViewImpl view) {
654
        if (ids != null) {
1 ✔
655
            if (view == null) {
1 ✔
656
                return get(ids[0]);
1 ✔
657
            }
658
            for (int id : ids) {
1 ✔
659
                EdgeImpl edge = get(id);
1 ✔
660
                if (view.containsEdge(edge)) {
1 ✔
661
                    return edge;
1 ✔
662
                }
663
            }
664
        }
665
        return null;
1 ✔
666
    }
667

668
    private int[] getAllIdsInType(NodeImpl source, NodeImpl target, int type, boolean undirectedDecorator) {
669
        return concat(getIdsInType(source, target, type, undirectedDecorator), getReverseIdsInType(source, target, type, undirectedDecorator));
1 ✔
670
    }
671

672
    // Store ids of the given type under the key from source to target, or under the undirected key on undirected
673
    // graphs and, for undirected edges, on mixed graphs without the undirected decorator
674
    private int[] getIdsInType(NodeImpl source, NodeImpl target, int type, boolean undirectedDecorator) {
675
        Long2ObjectOpenCustomHashMap<int[]> dico = longDictionary[type];
1 ✔
676
        if (isUndirectedGraph()) {
1 ✔
677
            return dico.get(getLongId(source, target, false));
1 ✔
678
        } else if (isMixedGraph() && !undirectedDecorator) {
1 ✔
679
            int[] index = dico.get(getLongId(source, target, true));
1 ✔
680
            if (index == null && target.storeId > source.storeId) {
1 ✔
681
                // The undirected key is also the key of the directed edges from target to source
682
                index = dico.get(getLongId(source, target, false));
1 ✔
683
                if (index != null && get(index[0]).isDirected()) {
1 ✔
684
                    return null;
1 ✔
685
                }
686
            }
687
            return index;
1 ✔
688
        }
689
        return dico.get(getLongId(source, target, true));
1 ✔
690
    }
691

692
    // Store ids of the given type under the key from target to source, which the undirected decorator also reads on
693
    // directed and mixed graphs, where the undirected key is one of the two directed keys
694
    private int[] getReverseIdsInType(NodeImpl source, NodeImpl target, int type, boolean undirectedDecorator) {
695
        // A self-loop's reverse key is its own key
696
        if (undirectedDecorator && source != target && !isUndirectedGraph()) {
1 ✔
697
            return longDictionary[type].get(getLongId(target, source, true));
1 ✔
698
        }
699
        return null;
1 ✔
700
    }
701

702
    private static int[] concat(int[] ids, int[] otherIds) {
703
        if (ids == null) {
1 ✔
704
            return otherIds;
1 ✔
705
        } else if (otherIds == null) {
1 ✔
706
            return ids;
1 ✔
707
        }
708
        int[] result = Arrays.copyOf(ids, ids.length + otherIds.length);
1 ✔
709
        System.arraycopy(otherIds, 0, result, ids.length, otherIds.length);
1 ✔
710
        return result;
1 ✔
711
    }
712

713
    // Iterates the given store ids and releases the read lock, taken by the caller, once exhausted
714
    private Iterator<Edge> edgesIterator(int[] ids) {
715
        if (ids == null) {
1 ✔
716
            readUnlock();
1 ✔
717
            return EMPTY_EDGE_ITERATOR;
1 ✔
718
        }
719
        return new EdgesIterator(ids);
1 ✔
720
    }
721

722
    public EdgeImpl getMutualEdge(final Edge e) {
723
        checkNonNullEdgeObject(e);
1 ✔
724

725
        EdgeImpl edge = (EdgeImpl) e;
1 ✔
726
        checkEdgeExists(edge);
1 ✔
727

728
        return getMutual(edge);
1 ✔
729
    }
730

731
    private EdgeImpl getMutual(final EdgeImpl edge) {
732
        return get(edge.target, edge.source, edge.type, false);
1 ✔
733
    }
734

735
    public boolean setEdgeType(final Edge e, int type) {
736
        checkNonNullEdgeObject(e);
1 ✔
737

738
        if (e.getType() == type) {
1 ✔
739
            return false;
1 ✔
740
        }
741

742
        int oldType = e.getType();
1 ✔
743
        EdgeImpl edge = (EdgeImpl) e;
1 ✔
744
        if (edge.storeId != EdgeStore.NULL_ID) {
1 ✔
745
            EdgeTypeStore.checkValidId(type);
1 ✔
746
            ensureLongDictionaryCapacity(type);
1 ✔
747
            Long2ObjectOpenCustomHashMap<int[]> newDico = longDictionary[type];
1 ✔
748

749
            long longId = getLongId(edge.source, edge.target, edge.isDirected());
1 ✔
750
            int[] newDicoValue = newDico.get(longId);
1 ✔
751
            if (newDicoValue != null && !configuration.isEnableParallelEdgesSameType()) {
1 ✔
752
                return false;
1 ✔
753
            }
754
            checkUndirectedNotExist(edge, type);
1 ✔
755

756
            edgeTypeStore.registerEdgeType(type);
1 ✔
757
            boolean wasMutual = edge.isMutual();
1 ✔
758
            removeFromDico(edge, edge.storeId);
1 ✔
759
            decrementTypeSize(oldType);
1 ✔
760

761
            removeOutEdge(edge);
1 ✔
762
            removeInEdge(edge);
1 ✔
763
            edge.type = type;
1 ✔
764
            insertOutEdge(edge);
1 ✔
765
            insertInEdge(edge);
1 ✔
766

767
            addToDico(newDico, newDicoValue, edge, longId);
1 ✔
768
            incrementTypeSize(type);
1 ✔
769
            updateUndirectedGraphSize(oldType);
1 ✔
770
            updateUndirectedGraphSize(type);
1 ✔
771
            updateMultiGraph();
1 ✔
772

773
            if (viewStore != null) {
1 ✔
774
                viewStore.setEdgeType(edge, oldType, wasMutual);
1 ✔
775
            }
776

777
            incrementVersion();
1 ✔
778
        } else {
1 ✔
779
            edge.type = type;
1 ✔
780
        }
781

782
        return true;
1 ✔
783
    }
784

785
    private void removeFromDico(EdgeImpl edge, int id) {
786
        int type = edge.type;
1 ✔
787
        NodeImpl source = edge.source;
1 ✔
788
        NodeImpl target = edge.target;
1 ✔
789
        boolean directed = edge.isDirected();
1 ✔
790

791
        Long2ObjectOpenCustomHashMap<int[]> dico = longDictionary[type];
1 ✔
792
        long longId = getLongId(source, target, directed);
1 ✔
793
        int[] dicoValue = dico.get(longId);
1 ✔
794
        if (dicoValue.length == 1) {
1 ✔
795
            dico.remove(longId);
1 ✔
796
        } else {
797
            int[] newDicoValue = new int[dicoValue.length - 1];
1 ✔
798
            int j = 0;
1 ✔
799
            for (int i = 0; i < dicoValue.length; i++) {
1 ✔
800
                int v = dicoValue[i];
1 ✔
801
                if (v != id) {
1 ✔
802
                    newDicoValue[j++] = v;
1 ✔
803
                }
804
            }
805
            dico.put(longId, newDicoValue);
1 ✔
806
        }
807

808
        // Only an edge paired with a reverse edge releases one, parallel edges may leave others paired
809
        if (directed && !edge.isSelfLoop() && edge.isMutual()) {
1 ✔
810
            int[] index = longDictionary[type].get(getLongId(edge.target, edge.source, true));
1 ✔
811
            if (index != null) {
1 ✔
812
                for (int i = 0; i < index.length; i++) {
1 ✔
813
                    EdgeImpl mutual = get(index[i]);
1 ✔
814
                    if (mutual.isMutual()) {
1 ✔
815
                        edge.setMutual(false);
1 ✔
816

817
                        mutual.setMutual(false);
1 ✔
818
                        source.mutualDegree--;
1 ✔
819
                        target.mutualDegree--;
1 ✔
820
                        mutualEdgesSize--;
1 ✔
821
                        mutualEdgesTypeSize[type]--;
1 ✔
822
                        break;
1 ✔
823
                    }
824
                }
825
            }
826
        }
827
    }
1 ✔
828

829
    private void addToDico(Long2ObjectOpenCustomHashMap<int[]> dico, int[] dicoValue, EdgeImpl edge, long longId) {
830
        if (dicoValue == null) {
1 ✔
831
            dicoValue = new int[] { edge.storeId };
1 ✔
832
        } else {
833
            dicoValue = Arrays.copyOf(dicoValue, dicoValue.length + 1);
1 ✔
834
            dicoValue[dicoValue.length - 1] = edge.storeId;
1 ✔
835
        }
836
        dico.put(longId, dicoValue);
1 ✔
837

838
        if (edge.isDirected() && !edge.isSelfLoop()) {
1 ✔
839
            int type = edge.type;
1 ✔
840
            int[] index = longDictionary[type].get(getLongId(edge.target, edge.source, true));
1 ✔
841
            if (index != null) {
1 ✔
842
                for (int i = 0; i < index.length; i++) {
1 ✔
843
                    EdgeImpl mutual = get(index[i]);
1 ✔
844
                    if (!mutual.isMutual()) {
1 ✔
845
                        mutual.setMutual(true);
1 ✔
846
                        edge.setMutual(true);
1 ✔
847
                        edge.source.mutualDegree++;
1 ✔
848
                        edge.target.mutualDegree++;
1 ✔
849
                        mutualEdgesSize++;
1 ✔
850
                        mutualEdgesTypeSize[type]++;
1 ✔
851
                        break;
1 ✔
852
                    }
853
                }
854
            }
855
        }
856
    }
1 ✔
857

858
    @Override
859
    public boolean add(final Edge e) {
860
        checkNonNullEdgeObject(e);
1 ✔
861

862
        EdgeImpl edge = (EdgeImpl) e;
1 ✔
863
        if (edge.storeId == EdgeStore.NULL_ID) {
1 ✔
864
            int type = edge.type;
1 ✔
865
            EdgeTypeStore.checkValidId(type);
1 ✔
866
            checkIdDoesntExist(e.getId());
1 ✔
867
            checkSourceTargets(edge);
1 ✔
868
            checkUndirectedNotExist(edge);
1 ✔
869

870
            boolean directed = edge.isDirected();
1 ✔
871
            NodeImpl source = edge.source;
1 ✔
872
            NodeImpl target = edge.target;
1 ✔
873

874
            ensureLongDictionaryCapacity(type);
1 ✔
875
            Long2ObjectOpenCustomHashMap<int[]> dico = longDictionary[type];
1 ✔
876
            long longId = getLongId(source, target, directed);
1 ✔
877
            int[] dicoValue = dico.get(longId);
1 ✔
878
            if (dicoValue != null && !configuration.isEnableParallelEdgesSameType()) {
1 ✔
879
                return false;
1 ✔
880
            }
881

882
            if (edgeTypeStore != null) {
1 ✔
883
                edgeTypeStore.registerEdgeType(type);
1 ✔
884
            }
885
            incrementVersion();
1 ✔
886

887
            if (garbageSize > 0) {
1 ✔
888
                for (int i = 0; i < blocksCount; i++) {
1 ✔
889
                    EdgeBlock edgeBlock = blocks[i];
1 ✔
890
                    if (edgeBlock.hasGarbage()) {
1 ✔
891
                        edgeBlock.set(edge);
1 ✔
892
                        garbageSize--;
1 ✔
893
                        dictionary.put(edge.getId(), edge.storeId);
1 ✔
894
                        break;
1 ✔
895
                    }
896
                }
897
            } else {
898
                ensureCapacity(1);
1 ✔
899
                currentBlock.add(edge);
1 ✔
900
                dictionary.put(edge.getId(), edge.storeId);
1 ✔
901
            }
902

903
            insertOutEdge(edge);
1 ✔
904
            insertInEdge(edge);
1 ✔
905

906
            source.outDegree++;
1 ✔
907
            target.inDegree++;
1 ✔
908

909
            addToDico(dico, dicoValue, edge, longId);
1 ✔
910

911
            if (viewStore != null) {
1 ✔
912
                viewStore.addEdge(edge);
1 ✔
913
            }
914
            edge.indexAttributes();
1 ✔
915

916
            if (!directed) {
1 ✔
917
                undirectedSize++;
1 ✔
918
            }
919

920
            size++;
1 ✔
921
            incrementTypeSize(type);
1 ✔
922
            updateUndirectedGraphSize(type);
1 ✔
923
            updateGraphType();
1 ✔
924
            updateMultiGraph();
1 ✔
925
            return true;
1 ✔
926
        } else if (isValidIndex(edge.storeId) && get(edge.storeId) == edge) {
1 ✔
927
            return false;
1 ✔
928
        } else {
929
            throw new IllegalArgumentException("The edge already belongs to another store");
1 ✔
930
        }
931
    }
932

933
    @Override
934
    public boolean remove(final Object o) {
935
        checkNonNullEdgeObject(o);
1 ✔
936

937
        EdgeImpl edge = (EdgeImpl) o;
1 ✔
938
        int id = edge.storeId;
1 ✔
939
        if (id != EdgeStore.NULL_ID) {
1 ✔
940
            checkEdgeExists(edge);
1 ✔
941

942
            incrementVersion();
1 ✔
943

944
            if (viewStore != null) {
1 ✔
945
                viewStore.removeEdge(edge);
1 ✔
946
            }
947

948
            edge.destroyAttributes();
1 ✔
949

950
            int storeIndex = id / GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE;
1 ✔
951
            EdgeBlock block = blocks[storeIndex];
1 ✔
952
            block.remove(edge);
1 ✔
953

954
            removeOutEdge(edge);
1 ✔
955
            removeInEdge(edge);
1 ✔
956

957
            boolean directed = edge.isDirected();
1 ✔
958
            NodeImpl source = edge.source;
1 ✔
959
            NodeImpl target = edge.target;
1 ✔
960

961
            source.outDegree--;
1 ✔
962
            target.inDegree--;
1 ✔
963

964
            size--;
1 ✔
965
            decrementTypeSize(edge.type);
1 ✔
966
            garbageSize++;
1 ✔
967
            dictionary.remove(edge.getId());
1 ✔
968
            trimDictionary();
1 ✔
969

970
            for (int i = storeIndex; i == (blocksCount - 1) && block.garbageLength == block.nodeLength && i >= 0;) {
1 ✔
971
                if (i != 0) {
1 ✔
972
                    blocks[i] = null;
1 ✔
973
                    blocksCount--;
1 ✔
974
                    garbageSize -= block.nodeLength;
1 ✔
975
                    block = blocks[--i];
1 ✔
976
                    currentBlock = block;
1 ✔
977
                    currentBlockIndex--;
1 ✔
978
                } else {
979
                    currentBlock.clear();
1 ✔
980
                    garbageSize = 0;
1 ✔
981
                    break;
1 ✔
982
                }
983
            }
984

985
            removeFromDico(edge, id);
1 ✔
986

987
            if (!directed) {
1 ✔
988
                undirectedSize--;
1 ✔
989
            }
990
            updateUndirectedGraphSize(edge.type);
1 ✔
991
            updateGraphType();
1 ✔
992
            updateMultiGraph();
1 ✔
993

994
            if (edgeTypeStore != null) {
1 ✔
995
                // TODO - if type count is zero, do smthing
996
            }
997

998
            return true;
1 ✔
999
        }
1000
        return false;
1 ✔
1001
    }
1002

1003
    @Override
1004
    public boolean contains(Object o) {
1005
        checkNonNullEdgeObject(o);
1 ✔
1006

1007
        EdgeImpl edge = (EdgeImpl) o;
1 ✔
1008
        int id = edge.getStoreId();
1 ✔
1009
        if (id != EdgeStore.NULL_ID) {
1 ✔
1010
            if (get(id) == edge) {
1 ✔
1011
                return true;
1 ✔
1012
            }
1013
        }
1014
        return false;
1 ✔
1015
    }
1016

1017
    public boolean containsId(final Object id) {
1018
        return dictionary.containsKey(id);
1 ✔
1019
    }
1020

1021
    public boolean containsAnyType(NodeImpl source, NodeImpl target) {
1022
        int typeLength = longDictionary.length;
1 ✔
1023
        for (int i = 0; i < typeLength; i++) {
1 ✔
1024
            if (contains(source, target, i)) {
1 ✔
1025
                return true;
1 ✔
1026
            }
1027
        }
1028
        return false;
1 ✔
1029
    }
1030

1031
    public boolean contains(NodeImpl source, NodeImpl target, int type) {
1032
        checkNonNullObject(source);
1 ✔
1033
        checkNonNullObject(target);
1 ✔
1034
        EdgeTypeStore.checkValidId(type);
1 ✔
1035

1036
        if (type < longDictionary.length) {
1 ✔
1037
            if (isUndirectedGraph()) {
1 ✔
1038
                return longDictionary[type].containsKey(getLongId(source, target, false));
1 ✔
1039
            } else if (isMixedGraph()) {
1 ✔
1040
                if (longDictionary[type].containsKey(getLongId(source, target, true))) {
1 ✔
1041
                    return true;
1 ✔
1042
                } else if (target.storeId > source.storeId) {
1 ✔
1043
                    int[] index = longDictionary[type].get(getLongId(source, target, false));
1 ✔
1044
                    if (index != null) {
1 ✔
1045
                        for (int i = 0; i < index.length; i++) {
1 ✔
1046
                            EdgeImpl mutual = get(index[i]);
1 ✔
1047
                            if (!mutual.isDirected()) {
1 ✔
1048
                                return true;
1 ✔
1049
                            }
1050
                        }
1051
                    }
1052
                }
×
1053
            } else {
1054
                return longDictionary[type].containsKey(getLongId(source, target, true));
1 ✔
1055
            }
1056
        }
1057
        return false;
1 ✔
1058
    }
1059

1060
    @Override
1061
    public EdgeImpl[] toArray() {
1062
        readLock();
1 ✔
1063

1064
        EdgeImpl[] array = new EdgeImpl[size];
1 ✔
1065
        if (garbageSize == 0) {
1 ✔
1066
            for (int i = 0; i < blocksCount; i++) {
1 ✔
1067
                EdgeBlock block = blocks[i];
1 ✔
1068
                System.arraycopy(block.backingArray, 0, array, block.offset, block.nodeLength);
1 ✔
1069
            }
1070
        } else {
1071
            EdgeStoreIterator itr = iterator();
1 ✔
1072
            int offset = 0;
1 ✔
1073
            while (itr.hasNext()) {
1 ✔
1074
                EdgeImpl n = itr.next();
1 ✔
1075
                array[offset++] = n;
1 ✔
1076
            }
1 ✔
1077
        }
1078

1079
        readUnlock();
1 ✔
1080
        return array;
1 ✔
1081
    }
1082

1083
    @Override
1084
    public <T> T[] toArray(T[] array) {
1085
        checkNonNullObject(array);
1 ✔
1086

1087
        readLock();
1 ✔
1088

1089
        if (array.length < size()) {
1 ✔
1090
            array = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), size());
1 ✔
1091
        }
1092
        if (garbageSize == 0) {
1 ✔
1093
            for (int i = 0; i < blocksCount; i++) {
1 ✔
1094
                EdgeBlock block = blocks[i];
1 ✔
1095
                System.arraycopy(block.backingArray, 0, array, block.offset, block.nodeLength);
1 ✔
1096
            }
1097
        } else {
1098
            EdgeStoreIterator itr = iterator();
×
1099
            int offset = 0;
×
1100
            while (itr.hasNext()) {
×
1101
                EdgeImpl n = itr.next();
×
1102
                array[offset++] = (T) n;
×
1103
            }
×
1104
        }
1105
        readUnlock();
1 ✔
1106

1107
        return array;
1 ✔
1108
    }
1109

1110
    @Override
1111
    public Collection<Edge> toCollection() {
1112
        readLock();
1 ✔
1113

1114
        List<Edge> list = new ArrayList<>(size);
1 ✔
1115
        EdgeStoreIterator itr = iterator();
1 ✔
1116
        while (itr.hasNext()) {
1 ✔
1117
            EdgeImpl n = itr.next();
1 ✔
1118
            list.add(n);
1 ✔
1119
        }
1 ✔
1120

1121
        readUnlock();
1 ✔
1122
        return list;
1 ✔
1123
    }
1124

1125
    @Override
1126
    public Set<Edge> toSet() {
1127
        readLock();
1 ✔
1128

1129
        Set<Edge> set = new HashSet<>(size);
1 ✔
1130
        EdgeStoreIterator itr = iterator();
1 ✔
1131
        while (itr.hasNext()) {
1 ✔
1132
            EdgeImpl n = itr.next();
1 ✔
1133
            set.add(n);
1 ✔
1134
        }
1 ✔
1135

1136
        readUnlock();
1 ✔
1137
        return set;
1 ✔
1138
    }
1139

1140
    @Override
1141
    public boolean containsAll(Collection<?> c) {
1142
        checkCollection(c);
1 ✔
1143

1144
        if (!c.isEmpty()) {
1 ✔
1145
            int found = 0;
1 ✔
1146
            for (Object o : c) {
1 ✔
1147
                if (contains((EdgeImpl) o)) {
1 ✔
1148
                    found++;
1 ✔
1149
                }
1150
            }
1 ✔
1151
            return found == c.size();
1 ✔
1152
        }
1153
        return true;
1 ✔
1154
    }
1155

1156
    @Override
1157
    public boolean addAll(Collection<? extends Edge> c) {
1158
        checkCollection(c);
1 ✔
1159

1160
        if (!c.isEmpty()) {
1 ✔
1161
            int capacityNeeded = c.size() - garbageSize;
1 ✔
1162
            if (capacityNeeded > 0) {
1 ✔
1163
                ensureCapacity(capacityNeeded);
1 ✔
1164
            }
1165
            boolean changed = false;
1 ✔
1166
            Iterator<? extends Edge> itr = c.iterator();
1 ✔
1167
            while (itr.hasNext()) {
1 ✔
1168
                Edge e = itr.next();
1 ✔
1169
                if (add(e)) {
1 ✔
1170
                    changed = true;
1 ✔
1171
                }
1172
            }
1 ✔
1173
            return changed;
1 ✔
1174
        }
1175
        return false;
1 ✔
1176
    }
1177

1178
    @Override
1179
    public boolean removeAll(Collection<?> c) {
1180
        checkCollection(c);
1 ✔
1181

1182
        if (!c.isEmpty()) {
1 ✔
1183
            boolean changed = false;
1 ✔
1184
            Iterator itr = c.iterator();
1 ✔
1185
            while (itr.hasNext()) {
1 ✔
1186
                Object o = itr.next();
1 ✔
1187
                if (remove(o)) {
1 ✔
1188
                    changed = true;
1 ✔
1189
                }
1190
            }
1 ✔
1191
            return changed;
1 ✔
1192
        }
1193
        return false;
1 ✔
1194
    }
1195

1196
    @Override
1197
    public boolean retainAll(Collection<?> c) {
1198
        checkCollection(c);
1 ✔
1199

1200
        ObjectSet<EdgeImpl> set = new ObjectOpenHashSet(c.size());
1 ✔
1201
        for (Object o : c) {
1 ✔
1202
            checkNonNullObject(o);
1 ✔
1203
            checkEdgeExists((EdgeImpl) o);
1 ✔
1204
            set.add((EdgeImpl) o);
1 ✔
1205
        }
1 ✔
1206

1207
        boolean changed = false;
1 ✔
1208
        EdgeStoreIterator itr = iterator();
1 ✔
1209
        while (itr.hasNext()) {
1 ✔
1210
            EdgeImpl e = itr.next();
1 ✔
1211
            if (!set.contains(e)) {
1 ✔
1212
                itr.remove();
1 ✔
1213
                changed = true;
1 ✔
1214
            }
1215
        }
1 ✔
1216
        return changed;
1 ✔
1217
    }
1218

1219
    public int deepHashCode() {
1220
        int hash = 7;
1 ✔
1221
        hash = 67 * hash + this.size;
1 ✔
1222
        EdgeStoreIterator itr = this.iterator();
1 ✔
1223
        while (itr.hasNext()) {
1 ✔
1224
            hash = 67 * hash + itr.next().hashCode();
1 ✔
1225
        }
1226
        return hash;
1 ✔
1227
    }
1228

1229
    public boolean deepEquals(EdgeStore obj) {
1230
        if (obj == null || this.size != obj.size) {
1 ✔
1231
            return false;
×
1232
        }
1233
        EdgeStoreIterator itr1 = this.iterator();
1 ✔
1234
        EdgeStoreIterator itr2 = obj.iterator();
1 ✔
1235
        while (itr1.hasNext()) {
1 ✔
1236
            if (!itr2.hasNext()) {
1 ✔
1237
                return false;
×
1238
            }
1239
            if (!itr1.next().equals(itr2.next())) {
1 ✔
1240
                return false;
1 ✔
1241
            }
1242
        }
1243
        return true;
1 ✔
1244
    }
1245

1246
    public boolean isAdjacent(Node node1, Node node2, int type) {
1247
        checkValidNodeObject(node1);
1 ✔
1248
        checkValidNodeObject(node2);
1 ✔
1249

1250
        return contains((NodeImpl) node1, (NodeImpl) node2, type);
1 ✔
1251
    }
1252

1253
    public boolean isAdjacent(Node node1, Node node2) {
1254
        checkValidNodeObject(node1);
1 ✔
1255
        checkValidNodeObject(node2);
1 ✔
1256

1257
        int typeLength = longDictionary.length;
1 ✔
1258
        for (int i = 0; i < typeLength; i++) {
1 ✔
1259
            if (contains((NodeImpl) node1, (NodeImpl) node2, i)) {
1 ✔
1260
                return true;
1 ✔
1261
            }
1262
        }
1263

1264
        return false;
1 ✔
1265
    }
1266

1267
    public boolean isIncident(EdgeImpl edge1, EdgeImpl edge2) {
1268
        return edge1.source == edge2.source || edge1.target == edge2.target || edge1.source == edge2.target || edge1.target == edge2.source;
1 ✔
1269
    }
1270

1271
    public boolean isIncident(NodeImpl node, EdgeImpl edge) {
1272
        return edge.source == node || edge.target == node;
1 ✔
1273
    }
1274

1275
    protected Iterator<Edge> undirectedIterator(Iterator<Edge> edgeIterator) {
1276
        return new UndirectedIterator(edgeIterator);
1 ✔
1277
    }
1278

1279
    @Override
1280
    public void doBreak() {
1281
        readUnlock();
×
1282
    }
×
1283

1284
    void checkUndirectedNotExist(EdgeImpl edge) {
1285
        checkUndirectedNotExist(edge, edge.type);
1 ✔
1286
    }
1 ✔
1287

1288
    // Checks that no edge of the other directedness connects the same nodes with the given type
1289
    void checkUndirectedNotExist(EdgeImpl edge, int type) {
1290
        if (type < longDictionary.length) {
1 ✔
1291
            if (edge.isDirected() && !isDirectedGraph()) {
1 ✔
1292
                int[] index = longDictionary[type].get(getLongId(edge.source, edge.target, false));
1 ✔
1293
                if (index != null && !get(index[0]).isDirected()) {
1 ✔
1294
                    throw new IllegalArgumentException("An undirected edge already exists");
1 ✔
1295
                }
1296
            } else if (!edge.isDirected() && !isUndirectedGraph()) {
1 ✔
1297
                int[] index = longDictionary[type].get(getLongId(edge.source, edge.target, true));
1 ✔
1298
                if (index != null && get(index[0]).isDirected()) {
1 ✔
1299
                    throw new IllegalArgumentException("An directed edge already exists");
1 ✔
1300
                }
1301
                index = longDictionary[type].get(getLongId(edge.target, edge.source, true));
1 ✔
1302
                if (index != null && get(index[0]).isDirected()) {
1 ✔
1303
                    throw new IllegalArgumentException("An directed edge already exists");
1 ✔
1304
                }
1305
            }
1306
        }
1307
    }
1 ✔
1308

1309
    void checkIdDoesntExist(Object id) {
1310
        if (dictionary.containsKey(id)) {
1 ✔
1311
            throw new IllegalArgumentException("The edge id already exist");
1 ✔
1312
        }
1313
    }
1 ✔
1314

1315
    // Called under the write lock once size and undirectedSize are both up to date
1316
    private void updateGraphType() {
1317
        GraphType type;
1318
        if (undirectedSize == 0) {
1 ✔
1319
            type = GraphType.DIRECTED;
1 ✔
1320
        } else if (undirectedSize == size) {
1 ✔
1321
            type = GraphType.UNDIRECTED;
1 ✔
1322
        } else {
1323
            type = GraphType.MIXED;
1 ✔
1324
        }
1325
        if (graphType != type) {
1 ✔
1326
            graphType = type;
1 ✔
1327
        }
1328
    }
1 ✔
1329

1330
    private void incrementTypeSize(int type) {
1331
        if (typeSize[type]++ == 0) {
1 ✔
1332
            nonEmptyTypeCount++;
1 ✔
1333
        }
1334
    }
1 ✔
1335

1336
    private void decrementTypeSize(int type) {
1337
        if (--typeSize[type] == 0) {
1 ✔
1338
            nonEmptyTypeCount--;
1 ✔
1339
        }
1340
    }
1 ✔
1341

1342
    // Called under the write lock once size, mutualEdgesSize and the type's counts are up to date
1343
    private void updateUndirectedGraphSize(int type) {
1344
        undirectedGraphSize = size - mutualEdgesSize;
1 ✔
1345
        undirectedGraphTypeSize[type] = typeSize[type] - mutualEdgesTypeSize[type];
1 ✔
1346
    }
1 ✔
1347

1348
    // Called under the write lock once nonEmptyTypeCount is up to date
1349
    private void updateMultiGraph() {
1350
        boolean multi = nonEmptyTypeCount > 1;
1 ✔
1351
        if (multiGraph != multi) {
1 ✔
1352
            multiGraph = multi;
1 ✔
1353
        }
1354
    }
1 ✔
1355

1356
    boolean isMultiGraph() {
1357
        return multiGraph;
1 ✔
1358
    }
1359

1360
    boolean isMixedGraph() {
1361
        return graphType == GraphType.MIXED;
1 ✔
1362
    }
1363

1364
    boolean isUndirectedGraph() {
1365
        return graphType == GraphType.UNDIRECTED;
1 ✔
1366
    }
1367

1368
    boolean isDirectedGraph() {
1369
        return graphType == GraphType.DIRECTED;
1 ✔
1370
    }
1371

1372
    protected boolean isValidIndex(int id) {
1373
        return id >= 0 && id < currentBlock.offset + currentBlock.nodeLength;
1 ✔
1374
    }
1375

1376
    void checkCollection(final Collection<?> collection) {
1377
        if (collection == this) {
1 ✔
1378
            throw new IllegalArgumentException("Can't pass itself");
1 ✔
1379
        }
1380
    }
1 ✔
1381

1382
    void checkNonNullObject(final Object o) {
1383
        if (o == null) {
1 ✔
1384
            throw new NullPointerException();
1 ✔
1385
        }
1386
    }
1 ✔
1387

1388
    void checkValidNodeObject(final Node n) {
1389
        if (n == null) {
1 ✔
1390
            throw new NullPointerException();
1 ✔
1391
        }
1392
        if (!(n instanceof NodeImpl)) {
1 ✔
1393
            throw new ClassCastException("Object must be a NodeImpl object");
×
1394
        }
1395
        if (((NodeImpl) n).storeId == NodeStore.NULL_ID) {
1 ✔
1396
            throw new IllegalArgumentException("Node should belong to a store");
1 ✔
1397
        }
1398
    }
1 ✔
1399

1400
    void checkNonNullEdgeObject(final Object o) {
1401
        if (o == null) {
1 ✔
1402
            throw new NullPointerException();
1 ✔
1403
        }
1404
        if (!(o instanceof EdgeImpl)) {
1 ✔
1405
            throw new ClassCastException("Object must be a EdgeImpl object");
×
1406
        }
1407
    }
1 ✔
1408

1409
    void checkSourceTargets(final EdgeImpl e) {
1410
        if (e.source == null || e.target == null) {
1 ✔
1411
            throw new NullPointerException();
1 ✔
1412
        }
1413
        if (e.source.storeId == NodeStore.NULL_ID || e.target.storeId == NodeStore.NULL_ID) {
1 ✔
1414
            throw new RuntimeException("Source and target nodes should be valid and belong to a store");
1 ✔
1415
        }
1416
    }
1 ✔
1417

1418
    void checkEdgeExists(final EdgeImpl edge) {
1419
        if (get(edge.storeId) != edge) {
1 ✔
1420
            throw new IllegalArgumentException("The edge is invalid");
1 ✔
1421
        }
1422
    }
1 ✔
1423

1424
    void checkValidId(final int id) {
1425
        if (id < 0 || !isValidIndex(id)) {
1 ✔
1426
            throw new IllegalArgumentException("Edge id=" + id + " is invalid");
1 ✔
1427
        }
1428
    }
1 ✔
1429

1430
    void readLock() {
1431
        if (lock != null) {
1 ✔
1432
            lock.readLock();
1 ✔
1433
        }
1434
    }
1 ✔
1435

1436
    void readUnlock() {
1437
        if (lock != null) {
1 ✔
1438
            lock.readUnlock();
1 ✔
1439
        }
1440
    }
1 ✔
1441

1442
    void checkWriteLock() {
1443
        if (lock != null) {
1 ✔
1444
            lock.checkHoldWriteLock();
1 ✔
1445
        }
1446
    }
1 ✔
1447

1448
    private void incrementVersion() {
1449
        if (version != null) {
1 ✔
1450
            version.incrementAndGetEdgeVersion();
1 ✔
1451
        }
1452
        if (spatialIndex != null) {
1 ✔
1453
            spatialIndex.incrementVersion();
1 ✔
1454
        }
1455
    }
1 ✔
1456

1457
    boolean isUndirectedToIgnore(EdgeImpl edge) {
1458
        return edge.isMutual() && edge.source.storeId < edge.target.storeId;
1 ✔
1459
    }
1460

1461
    int maxStoreId() {
1462
        return currentBlock.offset + currentBlock.nodeLength;
1 ✔
1463
    }
1464

1465
    // Clears the bits of the free store ids below maxStoreId(), as listed in each block's garbage array
1466
    void clearFreeStoreIds(BitSet bits) {
1467
        for (int i = 0; i < blocksCount; i++) {
1 ✔
1468
            EdgeBlock block = blocks[i];
1 ✔
1469
            for (int j = 0; j < block.garbageLength; j++) {
1 ✔
1470
                bits.clear(block.offset + block.garbageArray[j] - Short.MIN_VALUE);
1 ✔
1471
            }
1472
        }
1473
    }
1 ✔
1474

1475
    private enum GraphType {
1 ✔
1476
        DIRECTED, UNDIRECTED, MIXED
1 ✔
1477
    }
1478

1479
    protected static class EdgeBlock {
1480

1481
        protected final int offset;
1482
        protected final short[] garbageArray;
1483
        protected final EdgeImpl[] backingArray;
1484
        protected int nodeLength;
1485
        protected int garbageLength;
1486

1487
        public EdgeBlock(int index) {
1 ✔
1488
            this.offset = index * GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE;
1 ✔
1489
            if (GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE >= Short.MAX_VALUE - Short.MIN_VALUE) {
1490
                throw new RuntimeException("BLOCK SIZE can't exceed 65535");
1491
            }
1492
            this.garbageArray = new short[GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE];
1 ✔
1493
            this.backingArray = new EdgeImpl[GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE];
1 ✔
1494
        }
1 ✔
1495

1496
        public boolean hasGarbage() {
1497
            return garbageLength > 0;
1 ✔
1498
        }
1499

1500
        public int getCapacity() {
1501
            return GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE - nodeLength - garbageLength;
1 ✔
1502
        }
1503

1504
        public void add(EdgeImpl k) {
1505
            int i = nodeLength++;
1 ✔
1506
            backingArray[i] = k;
1 ✔
1507
            k.setStoreId(i + offset);
1 ✔
1508
        }
1 ✔
1509

1510
        public void set(EdgeImpl k) {
1511
            int i = garbageArray[--garbageLength] - Short.MIN_VALUE;
1 ✔
1512
            backingArray[i] = k;
1 ✔
1513
            k.setStoreId(i + offset);
1 ✔
1514
        }
1 ✔
1515

1516
        public EdgeImpl get(int id) {
1517
            return backingArray[id - offset];
1 ✔
1518
        }
1519

1520
        public void remove(EdgeImpl k) {
1521
            int i = k.getStoreId() - offset;
1 ✔
1522
            backingArray[i] = null;
1 ✔
1523
            garbageArray[garbageLength++] = (short) (i + Short.MIN_VALUE);
1 ✔
1524
            k.setStoreId(NULL_ID);
1 ✔
1525
        }
1 ✔
1526

1527
        public void clear() {
1528
            nodeLength = 0;
1 ✔
1529
            garbageLength = 0;
1 ✔
1530
        }
1 ✔
1531
    }
1532

1533
    private static class DictionaryHashStrategy implements LongHash.Strategy {
1534

1535
        @Override
1536
        public int hashCode(long l) {
1537
            return (int) (l ^ (l >>> 32));
1 ✔
1538
        }
1539

1540
        @Override
1541
        public boolean equals(long l1, long l2) {
1542
            return l1 == l2;
1 ✔
1543
        }
1544
    }
1545

1546
    protected class EdgeStoreIterator implements Iterator<Edge> {
1547

1548
        protected int blockIndex;
1549
        protected EdgeImpl[] backingArray;
1550
        protected int blockLength;
1551
        protected int cursor;
1552
        protected EdgeImpl pointer;
1553

1554
        public EdgeStoreIterator() {
1 ✔
1555
            readLock();
1 ✔
1556
            this.backingArray = blocks[blockIndex].backingArray;
1 ✔
1557
            this.blockLength = blocks[blockIndex].nodeLength;
1 ✔
1558
        }
1 ✔
1559

1560
        @Override
1561
        public boolean hasNext() {
1562
            pointer = null;
1 ✔
1563
            while (cursor == blockLength || ((pointer = backingArray[cursor++]) == null)) {
1 ✔
1564
                if (cursor == blockLength) {
1 ✔
1565
                    if (++blockIndex < blocksCount) {
1 ✔
1566
                        backingArray = blocks[blockIndex].backingArray;
1 ✔
1567
                        blockLength = blocks[blockIndex].nodeLength;
1 ✔
1568
                        cursor = 0;
1 ✔
1569
                    } else {
1570
                        break;
1571
                    }
1572
                }
1573
            }
1574
            if (pointer == null) {
1 ✔
1575
                readUnlock();
1 ✔
1576
                return false;
1 ✔
1577
            }
1578
            return true;
1 ✔
1579
        }
1580

1581
        @Override
1582
        public EdgeImpl next() {
1583
            return pointer;
1 ✔
1584
        }
1585

1586
        @Override
1587
        public void remove() {
1588
            checkWriteLock();
1 ✔
1589
            EdgeStore.this.remove(pointer);
1 ✔
1590
        }
1 ✔
1591
    }
1592

1593
    protected final class UndirectedEdgeStoreIterator extends EdgeStoreIterator {
1594

1595
        public UndirectedEdgeStoreIterator() {
1 ✔
1596
            super();
1 ✔
1597
        }
1 ✔
1598

1599
        @Override
1600
        public boolean hasNext() {
1601
            pointer = null;
1 ✔
1602
            while (pointer == null) {
1 ✔
1603
                if (!super.hasNext()) {
1 ✔
1604
                    return false;
1 ✔
1605
                }
1606
                if (isUndirectedToIgnore(pointer)) {
1 ✔
1607
                    pointer = null;
1 ✔
1608
                }
1609
            }
1610
            return true;
1 ✔
1611
        }
1612

1613
        @Override
1614
        public void remove() {
1615
            checkWriteLock();
1 ✔
1616
            if (pointer.isMutual()) {
1 ✔
1617
                throw new UnsupportedOperationException(
1 ✔
1618
                        "Removing directed edges from undirected iterator is not supported");
1619
            }
1620
            EdgeStore.this.remove(pointer);
1 ✔
1621
        }
1 ✔
1622
    }
1623

1624
    protected final class EdgeTypeIterator extends EdgeStoreIterator {
1625

1626
        private final int type;
1627
        private final boolean undirected;
1628

1629
        public EdgeTypeIterator(int type, boolean undirected) {
1 ✔
1630
            super();
1 ✔
1631
            this.type = type;
1 ✔
1632
            this.undirected = undirected;
1 ✔
1633
        }
1 ✔
1634

1635
        @Override
1636
        public boolean hasNext() {
1637
            pointer = null;
1 ✔
1638
            while (pointer == null) {
1 ✔
1639
                if (!super.hasNext()) {
1 ✔
1640
                    return false;
1 ✔
1641
                }
1642
                if (pointer.getType() != type || (undirected && isUndirectedToIgnore(pointer))) {
1 ✔
1643
                    pointer = null;
1 ✔
1644
                }
1645
            }
1646
            return true;
1 ✔
1647
        }
1648

1649
        @Override
1650
        public void remove() {
1651
            checkWriteLock();
1 ✔
1652
            EdgeStore.this.remove(pointer);
1 ✔
1653
        }
1 ✔
1654
    }
1655

1656
    protected final class SelfLoopIterator extends EdgeStoreIterator {
1657

1658
        public SelfLoopIterator() {
1 ✔
1659
            super();
1 ✔
1660
        }
1 ✔
1661

1662
        @Override
1663
        public boolean hasNext() {
1664
            pointer = null;
1 ✔
1665
            while (pointer == null) {
1 ✔
1666
                if (!super.hasNext()) {
1 ✔
1667
                    return false;
1 ✔
1668
                }
1669
                if (!pointer.isSelfLoop()) {
1 ✔
1670
                    pointer = null;
1 ✔
1671
                }
1672
            }
1673
            return true;
1 ✔
1674
        }
1675
    }
1676

1677
    /**
1678
     * Abstract base class for iterating over edges connected to nodes. Provides common logic for handling both incoming
1679
     * and outgoing edges.
1680
     */
1681
    protected abstract class AbstractEdgeInOutIterator implements Iterator<Edge> {
1682

1683
        protected final boolean locking;
1684
        protected int outTypeLength;
1685
        protected int inTypeLength;
1686
        protected EdgeImpl[] outArray;
1687
        protected EdgeImpl[] inArray;
1688
        protected int typeIndex = 0;
1 ✔
1689
        protected EdgeImpl pointer;
1690
        protected EdgeImpl lastEdge;
1691
        protected boolean out = true;
1 ✔
1692

1693
        protected AbstractEdgeInOutIterator(boolean locking) {
1 ✔
1694
            this.locking = locking;
1 ✔
1695
            if (locking) {
1 ✔
1696
                readLock();
1 ✔
1697
            }
1698
        }
1 ✔
1699

1700
        /**
1701
         * Initialize arrays for the current node. Called when starting iteration for a new node.
1702
         */
1703
        protected void initializeForNode(NodeImpl node) {
1704
            outArray = node.headOut;
1 ✔
1705
            outTypeLength = outArray.length;
1 ✔
1706
            inArray = node.headIn;
1 ✔
1707
            inTypeLength = inArray.length;
1 ✔
1708
            typeIndex = 0;
1 ✔
1709
            pointer = null;
1 ✔
1710
            out = true;
1 ✔
1711
        }
1 ✔
1712

1713
        /**
1714
         * Called when the current node has no more edges. Should return true if there are more nodes to process, false
1715
         * otherwise.
1716
         */
1717
        protected abstract boolean moveToNextNode();
1718

1719
        @Override
1720
        public boolean hasNext() {
1721
            while (pointer == null) {
1 ✔
1722
                if (out) {
1 ✔
1723
                    while (pointer == null && typeIndex < outTypeLength) {
1 ✔
1724
                        pointer = outArray[typeIndex++];
1 ✔
1725
                    }
1726
                    if (pointer == null) {
1 ✔
1727
                        out = false;
1 ✔
1728
                        typeIndex = 0;
1 ✔
1729
                    }
1730
                }
1731
                if (!out) {
1 ✔
1732
                    while (pointer == null && typeIndex < inTypeLength) {
1 ✔
1733
                        pointer = inArray[typeIndex++];
1 ✔
1734
                        while (pointer != null && pointer.isSelfLoop()) {
1 ✔
1735
                            int id = pointer.nextInEdge;
1 ✔
1736
                            if (id != EdgeStore.NULL_ID) {
1 ✔
1737
                                pointer = get(id);
1 ✔
1738
                            } else {
1739
                                pointer = null;
1 ✔
1740
                            }
1741
                        }
1 ✔
1742
                    }
1743
                }
1744

1745
                if (pointer == null) {
1 ✔
1746
                    // No more edges for current node, try next node
1747
                    if (!moveToNextNode()) {
1 ✔
1748
                        if (locking) {
1 ✔
1749
                            readUnlock();
1 ✔
1750
                        }
1751
                        return false;
1 ✔
1752
                    }
1753
                }
1754
            }
1755
            return true;
1 ✔
1756
        }
1757

1758
        @Override
1759
        public EdgeImpl next() {
1760
            lastEdge = pointer;
1 ✔
1761
            if (out) {
1 ✔
1762
                int id = lastEdge.nextOutEdge;
1 ✔
1763
                if (id != EdgeStore.NULL_ID) {
1 ✔
1764
                    pointer = get(id);
1 ✔
1765
                } else {
1766
                    pointer = null;
1 ✔
1767
                }
1768
            } else {
1 ✔
1769
                int id = EdgeStore.NULL_ID;
1 ✔
1770
                while (id == EdgeStore.NULL_ID) {
1 ✔
1771
                    id = pointer.nextInEdge;
1 ✔
1772
                    if (id != EdgeStore.NULL_ID) {
1 ✔
1773
                        pointer = get(id);
1 ✔
1774
                        if (pointer.isSelfLoop()) {
1 ✔
1775
                            id = EdgeStore.NULL_ID;
1 ✔
1776
                        }
1777
                    } else {
1778
                        pointer = null;
1 ✔
1779
                        break;
1 ✔
1780
                    }
1781
                }
1782
            }
1783
            return lastEdge;
1 ✔
1784
        }
1785

1786
        @Override
1787
        public void remove() {
1788
            checkWriteLock();
1 ✔
1789
            EdgeStore.this.remove(lastEdge);
1 ✔
1790
        }
1 ✔
1791
    }
1792

1793
    /**
1794
     * Iterator for edges connected to a single node (both incoming and outgoing).
1795
     */
1796
    protected final class EdgeInOutIterator extends AbstractEdgeInOutIterator {
1797

1798
        public EdgeInOutIterator(NodeImpl node, boolean locking) {
1 ✔
1799
            super(locking);
1 ✔
1800
            initializeForNode(node);
1 ✔
1801
        }
1 ✔
1802

1803
        @Override
1804
        protected boolean moveToNextNode() {
1805
            // Single node iterator - no more nodes to process
1806
            return false;
1 ✔
1807
        }
1808
    }
1809

1810
    /**
1811
     * Iterator for edges connected to multiple nodes (both incoming and outgoing). Iterates through all edges of all
1812
     * provided nodes without creating separate iterators.
1813
     */
1814
    protected final class EdgeInOutMultiIterator extends AbstractEdgeInOutIterator {
1815

1816
        private final Iterator<NodeImpl> nodeIterator;
1817

1818
        public EdgeInOutMultiIterator(Iterator<NodeImpl> nodeIterator, boolean locking) {
1 ✔
1819
            super(locking);
1 ✔
1820
            this.nodeIterator = nodeIterator;
1 ✔
1821
            // Initialize with first node if available
1822
            if (nodeIterator.hasNext()) {
1 ✔
1823
                NodeImpl node = nodeIterator.next();
1 ✔
1824
                checkValidNodeObject(node);
1 ✔
1825
                initializeForNode(node);
1 ✔
1826
            }
1827
        }
1 ✔
1828

1829
        @Override
1830
        protected boolean moveToNextNode() {
1831
            if (nodeIterator.hasNext()) {
1 ✔
1832
                NodeImpl node = nodeIterator.next();
1 ✔
1833
                checkValidNodeObject(node);
1 ✔
1834
                initializeForNode(node);
1 ✔
1835
                return true;
1 ✔
1836
            }
1837
            return false;
1 ✔
1838
        }
1839
    }
1840

1841
    protected final class EdgeOutIterator implements Iterator<Edge> {
1842

1843
        protected final int typeLength;
1844
        protected EdgeImpl[] outArray;
1845
        protected int typeIndex = 0;
1 ✔
1846
        protected EdgeImpl pointer;
1847
        protected EdgeImpl lastEdge;
1848

1849
        public EdgeOutIterator(NodeImpl node) {
1 ✔
1850
            readLock();
1 ✔
1851
            outArray = node.headOut;
1 ✔
1852
            typeLength = outArray.length;
1 ✔
1853
        }
1 ✔
1854

1855
        @Override
1856
        public boolean hasNext() {
1857
            if (pointer == null) {
1 ✔
1858
                while (pointer == null && typeIndex < typeLength) {
1 ✔
1859
                    pointer = outArray[typeIndex++];
1 ✔
1860
                }
1861
                if (pointer == null) {
1 ✔
1862
                    readUnlock();
1 ✔
1863
                    return false;
1 ✔
1864
                }
1865
            }
1866
            return true;
1 ✔
1867
        }
1868

1869
        @Override
1870
        public EdgeImpl next() {
1871
            lastEdge = pointer;
1 ✔
1872
            int id = lastEdge.nextOutEdge;
1 ✔
1873
            if (id != EdgeStore.NULL_ID) {
1 ✔
1874
                pointer = get(id);
1 ✔
1875
            } else {
1876
                pointer = null;
1 ✔
1877
            }
1878
            return lastEdge;
1 ✔
1879
        }
1880

1881
        @Override
1882
        public void remove() {
1883
            checkWriteLock();
1 ✔
1884
            EdgeStore.this.remove(lastEdge);
1 ✔
1885
        }
1 ✔
1886
    }
1887

1888
    protected final class EdgeInIterator implements Iterator<Edge> {
1889

1890
        protected final int typeLength;
1891
        protected EdgeImpl[] inArray;
1892
        protected int typeIndex = 0;
1 ✔
1893
        protected EdgeImpl pointer;
1894
        protected EdgeImpl lastEdge;
1895

1896
        public EdgeInIterator(NodeImpl node) {
1 ✔
1897
            readLock();
1 ✔
1898
            inArray = node.headIn;
1 ✔
1899
            typeLength = inArray.length;
1 ✔
1900
        }
1 ✔
1901

1902
        @Override
1903
        public boolean hasNext() {
1904
            if (pointer == null) {
1 ✔
1905
                while (pointer == null && typeIndex < typeLength) {
1 ✔
1906
                    pointer = inArray[typeIndex++];
1 ✔
1907
                }
1908
                if (pointer == null) {
1 ✔
1909
                    readUnlock();
1 ✔
1910
                    return false;
1 ✔
1911
                }
1912
            }
1913
            return true;
1 ✔
1914
        }
1915

1916
        @Override
1917
        public EdgeImpl next() {
1918
            lastEdge = pointer;
1 ✔
1919
            int id = lastEdge.nextInEdge;
1 ✔
1920
            if (id != EdgeStore.NULL_ID) {
1 ✔
1921
                pointer = get(id);
1 ✔
1922
            } else {
1923
                pointer = null;
1 ✔
1924
            }
1925
            return lastEdge;
1 ✔
1926
        }
1927

1928
        @Override
1929
        public void remove() {
1930
            checkWriteLock();
1 ✔
1931
            EdgeStore.this.remove(lastEdge);
1 ✔
1932
        }
1 ✔
1933
    }
1934

1935
    protected final class EdgeTypeInOutIterator implements Iterator<Edge> {
1936

1937
        protected final int type;
1938
        protected EdgeImpl lastEdge;
1939
        protected EdgeImpl outPointer;
1940
        protected EdgeImpl inPointer;
1941
        protected boolean out = true;
1 ✔
1942

1943
        public EdgeTypeInOutIterator(NodeImpl node, int type) {
1 ✔
1944
            EdgeTypeStore.checkValidId(type);
1 ✔
1945
            this.type = type;
1 ✔
1946
            readLock();
1 ✔
1947
            EdgeImpl[] outArray = node.headOut;
1 ✔
1948
            EdgeImpl[] inArray = node.headIn;
1 ✔
1949
            outPointer = type < outArray.length ? outArray[type] : null;
1 ✔
1950
            inPointer = type < inArray.length ? inArray[type] : null;
1 ✔
1951
        }
1 ✔
1952

1953
        @Override
1954
        public boolean hasNext() {
1955
            if (outPointer == null) {
1 ✔
1956
                if (out) {
1 ✔
1957
                    out = false;
1 ✔
1958
                    while (inPointer != null && inPointer.isSelfLoop()) {
1 ✔
1959
                        int id = inPointer.nextInEdge;
1 ✔
1960
                        if (id != EdgeStore.NULL_ID) {
1 ✔
1961
                            inPointer = get(id);
1 ✔
1962
                        } else {
1963
                            inPointer = null;
1 ✔
1964
                        }
1965
                    }
1 ✔
1966
                }
1967
                if (inPointer == null) {
1 ✔
1968
                    readUnlock();
1 ✔
1969
                    return false;
1 ✔
1970
                }
1971
            }
1972
            return true;
1 ✔
1973
        }
1974

1975
        @Override
1976
        public EdgeImpl next() {
1977
            if (out) {
1 ✔
1978
                lastEdge = outPointer;
1 ✔
1979
                int id = lastEdge.nextOutEdge;
1 ✔
1980
                if (id != EdgeStore.NULL_ID) {
1 ✔
1981
                    outPointer = get(id);
1 ✔
1982
                } else {
1983
                    outPointer = null;
1 ✔
1984
                }
1985
            } else {
1 ✔
1986
                lastEdge = inPointer;
1 ✔
1987
                int id = EdgeStore.NULL_ID;
1 ✔
1988
                while (id == EdgeStore.NULL_ID) {
1 ✔
1989
                    id = inPointer.nextInEdge;
1 ✔
1990
                    if (id != EdgeStore.NULL_ID) {
1 ✔
1991
                        inPointer = get(id);
1 ✔
1992
                        if (inPointer.isSelfLoop()) {
1 ✔
1993
                            id = EdgeStore.NULL_ID;
1 ✔
1994
                        }
1995
                    } else {
1996
                        inPointer = null;
1 ✔
1997
                        break;
1 ✔
1998
                    }
1999
                }
2000
            }
2001

2002
            return lastEdge;
1 ✔
2003
        }
2004

2005
        public void reset(NodeImpl node) {
2006
            EdgeImpl[] outArray = node.headOut;
×
2007
            EdgeImpl[] inArray = node.headIn;
×
2008
            outPointer = type < outArray.length ? outArray[type] : null;
×
2009
            inPointer = type < inArray.length ? inArray[type] : null;
×
2010
        }
×
2011

2012
        @Override
2013
        public void remove() {
2014
            checkWriteLock();
1 ✔
2015
            EdgeStore.this.remove(lastEdge);
1 ✔
2016
        }
1 ✔
2017
    }
2018

2019
    protected final class EdgeTypeOutIterator implements Iterator<Edge> {
2020

2021
        protected final int type;
2022
        protected EdgeImpl lastEdge;
2023
        protected EdgeImpl pointer;
2024

2025
        public EdgeTypeOutIterator(NodeImpl node, int type) {
1 ✔
2026
            EdgeTypeStore.checkValidId(type);
1 ✔
2027
            this.type = type;
1 ✔
2028
            readLock();
1 ✔
2029
            EdgeImpl[] outArray = node.headOut;
1 ✔
2030
            pointer = type < outArray.length ? outArray[type] : null;
1 ✔
2031
        }
1 ✔
2032

2033
        @Override
2034
        public boolean hasNext() {
2035
            if (pointer == null) {
1 ✔
2036
                readUnlock();
1 ✔
2037
                return false;
1 ✔
2038
            }
2039
            return true;
1 ✔
2040
        }
2041

2042
        @Override
2043
        public EdgeImpl next() {
2044
            lastEdge = pointer;
1 ✔
2045
            int id = lastEdge.nextOutEdge;
1 ✔
2046
            if (id != EdgeStore.NULL_ID) {
1 ✔
2047
                pointer = get(id);
1 ✔
2048
            } else {
2049
                pointer = null;
1 ✔
2050
            }
2051
            return lastEdge;
1 ✔
2052
        }
2053

2054
        public void reset(NodeImpl node) {
2055
            EdgeImpl[] outArray = node.headOut;
×
2056
            pointer = type < outArray.length ? outArray[type] : null;
×
2057
        }
×
2058

2059
        @Override
2060
        public void remove() {
2061
            checkWriteLock();
1 ✔
2062
            EdgeStore.this.remove(lastEdge);
1 ✔
2063
        }
1 ✔
2064
    }
2065

2066
    protected class EdgesIterator implements Iterator<Edge> {
2067

2068
        protected final int[] indices;
2069
        protected int index;
2070

2071
        // The read lock is taken by the caller
2072
        public EdgesIterator(int[] indices) {
1 ✔
2073
            this.indices = indices;
1 ✔
2074
        }
1 ✔
2075

2076
        @Override
2077
        public boolean hasNext() {
2078
            boolean res = index < indices.length;
1 ✔
2079
            if (!res) {
1 ✔
2080
                readUnlock();
1 ✔
2081
            }
2082
            return res;
1 ✔
2083
        }
2084

2085
        @Override
2086
        public Edge next() {
2087
            return get(indices[index++]);
1 ✔
2088
        }
2089

2090
        @Override
2091
        public void remove() {
2092
            checkWriteLock();
×
2093
            EdgeStore.this.remove(get(indices[index - 1]));
×
2094
        }
×
2095
    }
2096

2097
    protected final class EdgeTypeInIterator implements Iterator<Edge> {
2098

2099
        protected final int type;
2100
        protected EdgeImpl lastEdge;
2101
        protected EdgeImpl pointer;
2102

2103
        public EdgeTypeInIterator(NodeImpl node, int type) {
1 ✔
2104
            EdgeTypeStore.checkValidId(type);
1 ✔
2105
            this.type = type;
1 ✔
2106
            readLock();
1 ✔
2107
            EdgeImpl[] inArray = node.headIn;
1 ✔
2108
            pointer = type < inArray.length ? inArray[type] : null;
1 ✔
2109
        }
1 ✔
2110

2111
        @Override
2112
        public boolean hasNext() {
2113
            if (pointer == null) {
1 ✔
2114
                readUnlock();
1 ✔
2115
                return false;
1 ✔
2116
            }
2117
            return true;
1 ✔
2118
        }
2119

2120
        @Override
2121
        public EdgeImpl next() {
2122
            lastEdge = pointer;
1 ✔
2123
            int id = lastEdge.nextInEdge;
1 ✔
2124
            if (id != EdgeStore.NULL_ID) {
1 ✔
2125
                pointer = get(id);
1 ✔
2126
            } else {
2127
                pointer = null;
1 ✔
2128
            }
2129
            return lastEdge;
1 ✔
2130
        }
2131

2132
        public void reset(NodeImpl node) {
2133
            EdgeImpl[] inArray = node.headIn;
×
2134
            pointer = type < inArray.length ? inArray[type] : null;
×
2135
        }
×
2136

2137
        @Override
2138
        public void remove() {
2139
            checkWriteLock();
1 ✔
2140
            EdgeStore.this.remove(lastEdge);
1 ✔
2141
        }
1 ✔
2142
    }
2143

2144
    protected class NeighborsIterator implements Iterator<Node> {
2145

2146
        protected final NodeImpl node;
2147
        protected final Iterator<Edge> itr;
2148

2149
        public NeighborsIterator(NodeImpl node, Iterator<Edge> itr) {
1 ✔
2150
            this.node = node;
1 ✔
2151
            this.itr = itr;
1 ✔
2152
        }
1 ✔
2153

2154
        @Override
2155
        public boolean hasNext() {
2156
            return itr.hasNext();
1 ✔
2157
        }
2158

2159
        @Override
2160
        public Node next() {
2161
            Edge e = itr.next();
1 ✔
2162
            return e.getSource() == node ? e.getTarget() : e.getSource();
1 ✔
2163
        }
2164

2165
        @Override
2166
        public void remove() {
2167
            throw new UnsupportedOperationException("Remove not supported for this iterator");
×
2168
        }
2169
    }
2170

2171
    protected final class NeighborsUndirectedIterator extends NeighborsIterator {
2172

2173
        protected EdgeImpl pointer;
2174

2175
        public NeighborsUndirectedIterator(NodeImpl node, Iterator<Edge> itr) {
1 ✔
2176
            super(node, itr);
1 ✔
2177
        }
1 ✔
2178

2179
        @Override
2180
        public boolean hasNext() {
2181
            pointer = null;
1 ✔
2182
            while (pointer == null || isUndirectedToIgnore(pointer)) {
1 ✔
2183
                if (!itr.hasNext()) {
1 ✔
2184
                    return false;
1 ✔
2185
                }
2186
                pointer = (EdgeImpl) itr.next();
1 ✔
2187
            }
2188
            return true;
1 ✔
2189
        }
2190

2191
        @Override
2192
        public Node next() {
2193
            return pointer.getSource() == node ? pointer.getTarget() : pointer.getSource();
1 ✔
2194
        }
2195
    }
2196

2197
    protected final class UndirectedIterator implements Iterator<Edge> {
2198

2199
        protected final Iterator<Edge> itr;
2200
        protected EdgeImpl pointer;
2201

2202
        public UndirectedIterator(Iterator<Edge> itr) {
1 ✔
2203
            this.itr = itr;
1 ✔
2204
        }
1 ✔
2205

2206
        @Override
2207
        public boolean hasNext() {
2208
            pointer = null;
1 ✔
2209
            while (pointer == null || isUndirectedToIgnore(pointer)) {
1 ✔
2210
                if (!itr.hasNext()) {
1 ✔
2211
                    return false;
1 ✔
2212
                }
2213
                pointer = (EdgeImpl) itr.next();
1 ✔
2214
            }
2215
            return true;
1 ✔
2216
        }
2217

2218
        @Override
2219
        public EdgeImpl next() {
2220
            return pointer;
1 ✔
2221
        }
2222

2223
        @Override
2224
        public void remove() {
2225
            checkWriteLock();
1 ✔
2226
            if (pointer.isMutual()) {
1 ✔
2227
                throw new UnsupportedOperationException(
1 ✔
2228
                        "Removing directed edges from undirected iterator is not supported");
2229
            }
2230
            EdgeStore.this.remove(pointer);
1 ✔
2231
        }
1 ✔
2232
    }
2233

2234
    private class EdgeSpliterator implements Spliterator<Edge> {
2235

2236
        protected final int endBlockExclusive;
2237
        protected int blockIndex;
2238
        protected int indexInBlock;
2239
        protected EdgeImpl[] currentArray;
2240
        protected int currentLength;
2241
        protected final int expectedVersion;
2242
        protected int totalSize;
2243
        protected int consumed;
2244

2245
        EdgeSpliterator(int startBlock, int endBlockExclusive) {
2246
            this(startBlock, endBlockExclusive, EdgeStore.this.size());
1 ✔
2247
        }
1 ✔
2248

2249
        EdgeSpliterator(int startBlock, int endBlockExclusive, int totalSize) {
1 ✔
2250
            this.blockIndex = startBlock;
1 ✔
2251
            this.endBlockExclusive = endBlockExclusive;
1 ✔
2252
            this.expectedVersion = version != null ? version.getEdgeVersion() : 0;
1 ✔
2253
            this.consumed = 0;
1 ✔
2254

2255
            // Use the total store size for the root spliterator (covering all blocks)
2256
            if (startBlock == 0 && endBlockExclusive == blocksCount) {
1 ✔
2257
                this.totalSize = totalSize;
1 ✔
2258
            } else {
2259
                // For split spliterators, compute proportionally
2260
                this.totalSize = computeExactSize(startBlock, endBlockExclusive);
1 ✔
2261
            }
2262

2263
            if (startBlock < endBlockExclusive) {
1 ✔
2264
                EdgeStore.EdgeBlock b = blocks[startBlock];
1 ✔
2265
                currentArray = b.backingArray;
1 ✔
2266
                currentLength = b.nodeLength;
1 ✔
2267
                indexInBlock = 0;
1 ✔
2268
            } else {
1 ✔
2269
                currentArray = null;
×
2270
                currentLength = 0;
×
2271
                indexInBlock = 0;
×
2272
            }
2273
        }
1 ✔
2274

2275
        private int computeExactSize(int start, int end) {
2276
            int sum = 0;
1 ✔
2277
            for (int i = start; i < end; i++) {
1 ✔
2278
                EdgeStore.EdgeBlock b = blocks[i];
1 ✔
2279
                if (b != null) {
1 ✔
2280
                    // Exact count: nodeLength minus garbageLength
2281
                    sum += (b.nodeLength - b.garbageLength);
1 ✔
2282
                }
2283
            }
2284
            return sum;
1 ✔
2285
        }
2286

2287
        protected void advanceBlock() {
2288
            blockIndex++;
1 ✔
2289
            if (blockIndex < endBlockExclusive) {
1 ✔
2290
                EdgeStore.EdgeBlock b = blocks[blockIndex];
1 ✔
2291
                currentArray = b.backingArray;
1 ✔
2292
                currentLength = b.nodeLength;
1 ✔
2293
                indexInBlock = 0;
1 ✔
2294
            } else {
1 ✔
2295
                currentArray = null;
1 ✔
2296
                currentLength = 0;
1 ✔
2297
                indexInBlock = 0;
1 ✔
2298
            }
2299
        }
1 ✔
2300

2301
        protected void checkForComodification() {
2302
            if (version != null && expectedVersion != version.getEdgeVersion()) {
1 ✔
2303
                throw new ConcurrentModificationException();
1 ✔
2304
            }
2305
        }
1 ✔
2306

2307
        @Override
2308
        public boolean tryAdvance(Consumer<? super Edge> action) {
2309
            checkForComodification();
1 ✔
2310
            while (currentArray != null) {
1 ✔
2311
                while (indexInBlock < currentLength) {
1 ✔
2312
                    EdgeImpl n = currentArray[indexInBlock++];
1 ✔
2313
                    if (n != null) {
1 ✔
2314
                        consumed++;
1 ✔
2315
                        action.accept(n);
1 ✔
2316
                        return true;
1 ✔
2317
                    }
2318
                }
1 ✔
2319
                advanceBlock();
1 ✔
2320
            }
2321
            return false;
1 ✔
2322
        }
2323

2324
        protected EdgeSpliterator createSplit(int startBlock, int endBlockExclusive) {
2325
            return new EdgeSpliterator(startBlock, endBlockExclusive);
1 ✔
2326
        }
2327

2328
        @Override
2329
        public Spliterator<Edge> trySplit() {
2330
            // Only split at block boundaries to preserve encounter order
2331
            if (indexInBlock != 0) {
1 ✔
2332
                return null;
×
2333
            }
2334

2335
            int currentPos = blockIndex;
1 ✔
2336
            int remainingBlocks = endBlockExclusive - currentPos;
1 ✔
2337

2338
            if (remainingBlocks <= 1) {
1 ✔
2339
                return null;
1 ✔
2340
            }
2341

2342
            int mid = currentPos + remainingBlocks / 2;
1 ✔
2343

2344
            // Create left half
2345
            EdgeSpliterator left = createSplit(currentPos, mid);
1 ✔
2346

2347
            // Update this spliterator to become the right half
2348
            blockIndex = mid;
1 ✔
2349
            if (mid < endBlockExclusive) {
1 ✔
2350
                EdgeStore.EdgeBlock b = blocks[mid];
1 ✔
2351
                currentArray = b.backingArray;
1 ✔
2352
                currentLength = b.nodeLength;
1 ✔
2353
                indexInBlock = 0;
1 ✔
2354
            } else {
1 ✔
2355
                currentArray = null;
×
2356
                currentLength = 0;
×
2357
                indexInBlock = 0;
×
2358
            }
2359

2360
            // Update this spliterator size
2361
            this.totalSize = totalSize - left.totalSize;
1 ✔
2362

2363
            return left;
1 ✔
2364
        }
2365

2366
        @Override
2367
        public long estimateSize() {
2368
            // Use the exact totalSize minus what we've consumed
2369
            long remaining = totalSize - consumed;
1 ✔
2370
            return remaining < 0 ? 0 : remaining;
1 ✔
2371
        }
2372

2373
        @Override
2374
        public int characteristics() {
2375
            return Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.SIZED | Spliterator.SUBSIZED;
1 ✔
2376
        }
2377
    }
2378

2379
    private class FilteredSizedEdgeSpliterator extends EdgeSpliterator {
2380

2381
        protected final Predicate<EdgeImpl> filter;
2382
        // True only for the root spliterator, where totalSize is the caller-supplied,
2383
        // filter-aware count. Sub-ranges created by trySplit fall back to the inherited,
2384
        // unfiltered block count and must drop SIZED.
2385
        protected boolean exactSize;
2386

2387
        FilteredSizedEdgeSpliterator(int startBlock, int endBlockExclusive, Predicate<EdgeImpl> filter, int totalSize) {
1 ✔
2388
            super(startBlock, endBlockExclusive, totalSize);
1 ✔
2389
            this.filter = filter;
1 ✔
2390
            this.exactSize = (startBlock == 0 && endBlockExclusive == blocksCount);
1 ✔
2391
        }
1 ✔
2392

2393
        @Override
2394
        public boolean tryAdvance(Consumer<? super Edge> action) {
2395
            checkForComodification();
1 ✔
2396
            while (currentArray != null) {
1 ✔
2397
                while (indexInBlock < currentLength) {
1 ✔
2398
                    EdgeImpl n = currentArray[indexInBlock++];
1 ✔
2399
                    if (n != null && filter.test(n)) {
1 ✔
2400
                        consumed++;
1 ✔
2401
                        action.accept(n);
1 ✔
2402
                        return true;
1 ✔
2403
                    }
2404
                }
1 ✔
2405
                advanceBlock();
1 ✔
2406
            }
2407
            return false;
1 ✔
2408
        }
2409

2410
        @Override
2411
        public Spliterator<Edge> trySplit() {
2412
            Spliterator<Edge> left = super.trySplit();
1 ✔
2413
            if (left != null) {
1 ✔
2414
                // Once split, neither half can guarantee an exact filtered size, so drop SIZED.
2415
                this.exactSize = false;
1 ✔
2416
                ((FilteredSizedEdgeSpliterator) left).exactSize = false;
1 ✔
2417
            }
2418
            return left;
1 ✔
2419
        }
2420

2421
        protected EdgeSpliterator createSplit(int startBlock, int endBlockExclusive) {
2422
            return new FilteredSizedEdgeSpliterator(startBlock, endBlockExclusive, filter, totalSize);
1 ✔
2423
        }
2424

2425
        @Override
2426
        public int characteristics() {
2427
            int base = Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL;
1 ✔
2428
            return exactSize ? base | Spliterator.SIZED : base;
1 ✔
2429
        }
2430
    }
2431

2432
    private final class FilteredEdgeSpliterator extends FilteredSizedEdgeSpliterator {
2433

2434
        FilteredEdgeSpliterator(int startBlock, int endBlockExclusive, Predicate<EdgeImpl> filter) {
1 ✔
2435
            super(startBlock, endBlockExclusive, filter, EdgeStore.this.size());
1 ✔
2436
        }
1 ✔
2437

2438
        protected EdgeSpliterator createSplit(int startBlock, int endBlockExclusive) {
2439
            return new FilteredEdgeSpliterator(startBlock, endBlockExclusive, filter);
1 ✔
2440
        }
2441

2442
        @Override
2443
        public int characteristics() {
2444
            return Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.NONNULL;
1 ✔
2445
        }
2446
    }
2447

2448
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc