/** * retrieve all cells * @return all cells of the grid */ public Collection<Cell<T>> getCells() { return quadTree.values(); }
private static Map<Id<PtStop>, PtStop> convertQuadTree2HashMap(QuadTree<PtStop> qTree){ Iterator<PtStop> ptStopIterator = qTree.values().iterator(); Map<Id<PtStop>, PtStop> ptStopHashMap = new ConcurrentHashMap<>(); while(ptStopIterator.hasNext()){ PtStop ptStop = ptStopIterator.next(); ptStopHashMap.put(ptStop.getId(), ptStop); } return ptStopHashMap; }
/** * tests that also for a large number of entries, values() returns the correct result. */ @Test public void testGetValues() { double minX = -1000; double minY = -5000; double maxX = 20000; double maxY = 12000; Random r = new Random(20181017L); List<String> expected = new ArrayList<>(1000); QuadTree<String> qt = new QuadTree<>(minX, minY, maxX, maxY); for (int i = 0; i < 1000; i++) { double x = minX + r.nextDouble() * (maxX - minX); double y = minY + r.nextDouble() * (maxY - minY); String value = "ITEM_" + i; qt.put(x, y, value); expected.add(value); } List<String> items = new ArrayList<>(qt.values()); Assert.assertEquals(1000, items.size()); items.sort(String::compareTo); expected.sort(String::compareTo); for (int i = 0; i < 1000; i++) { Assert.assertEquals(expected.get(i), items.get(i)); } }
/** * Tests that by serializing and de-serializing a QuadTree, all important attributes * of the QuadTree are maintained. At the moment, this is essentially the size-attribute. * @throws IOException * @throws ClassNotFoundException */ @Test public void testSerialization() throws IOException, ClassNotFoundException { QuadTree<String> qt = getTestTree(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(outStream); out.writeObject(qt); out.close(); ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray()); ObjectInputStream in = new ObjectInputStream(inStream); QuadTree<String> qt2 = (QuadTree<String>)in.readObject(); in.close(); assertEquals(qt.size(), qt2.size()); valuesTester(qt2.size(), qt2.values()); }
@Test public void testValuesIterator_ConcurrentModification() { QuadTree<String> qt = getTestTree(); Iterator<String> iter = qt.values().iterator(); assertTrue(iter.hasNext()); assertNotNull(iter.next()); qt.put(39.0, 52.1, "39.0 52.1"); assertTrue(iter.hasNext()); // hasNext() should not yet provoke exception try { iter.next(); fail("missing exception."); } catch (ConcurrentModificationException e) { log.info("catched expected exception: ", e); } }
Matrix originDestinationTravelDistanceMatrix = new Matrix("PtStopTravelDistanceMatrix", "Stop to stop origin destination travel distance matrix"); PtStop ptStopIds[] = ptStops.values().toArray( new PtStop[0] );
private Tuple<QuadTree<ActivityFacilityWithIndex>, ActivityFacilityImpl[]> getTuple(String activityType) { TreesBuilder treesBuilder = new TreesBuilder(CollectionUtils.stringToSet(activityType), this.scenario.getNetwork(), this.dccg); treesBuilder.setActTypeConverter(this.getConverter()); treesBuilder.createTrees(scenario.getActivityFacilities()); ActivityFacilityImpl[] facilities = treesBuilder.getFacilitiesOfType().get(activityType); /* * Create a copy of the treesBuilder.getQuadTreesOfType() outcome where the * ActivityFacility objects are replaced by ActivityFacilityWithIndex objects. * TODO: let the TreeBuilder use ActivityFacilityWithIndex objects directly? */ QuadTree<ActivityFacilityWithIndex> quadTree = null; QuadTree<ActivityFacility> qt = treesBuilder.getQuadTreesOfType().get(activityType); if (qt != null) { double minX = qt.getMinEasting(); double maxX = qt.getMaxEasting(); double minY = qt.getMinNorthing(); double maxY = qt.getMaxNorthing(); quadTree = new QuadTree<ActivityFacilityWithIndex>(minX, minY, maxX, maxY); for (ActivityFacility activityFacility : qt.values()) { quadTree.put(activityFacility.getCoord().getX(), activityFacility.getCoord().getY(), this.faciliesWithIndexMap.get(activityFacility.getId())); } } return new Tuple<QuadTree<ActivityFacilityWithIndex>, ActivityFacilityImpl[]>(quadTree, facilities); }
/** * Test {@link QuadTree#values()} that it returns the correct content. */ @Test public void testValues() { QuadTree<String> qt = getTestTree(); int size = qt.size(); assertEquals(6, size); // generic test valuesTester(qt.size(), qt.values()); // test that values() got updated after adding an object to the tree qt.put(80.0, 80.0, "80.0, 80.0"); assertEquals(size + 1, qt.size()); valuesTester(qt.size(), qt.values()); // test that values() got updated after removing an object to the tree assertTrue(qt.remove(80.0, 80.0, "80.0, 80.0")); assertEquals(size, qt.size()); valuesTester(qt.size(), qt.values()); // and remove one more... assertTrue(qt.remove(10.0, 10.0, "10.0, 10.0")); assertEquals(size - 1, qt.size()); valuesTester(qt.size(), qt.values()); // test the iterator Iterator<String> iter = qt.values().iterator(); iter.next(); try { iter.remove(); fail("expected UnsupportedOperationException, got none."); } catch (UnsupportedOperationException expected) {} }
/** * Test {@link QuadTree#clear()}. */ @Test public void testClear() { QuadTree<String> qt = getTestTree(); int size = qt.size(); assertTrue(size > 0); // it makes no sense to test clear() on an empty tree qt.clear(); assertEquals(0, qt.size()); valuesTester(0, qt.values()); }
/** * Tests that a once obtained values-collection is indeed a live view * on the QuadTree, so when the QuadTree changes, the view is updated * as well. */ @Test public void testValues_isView() { QuadTree<String> qt = getTestTree(); int size = qt.size(); Collection<String> values = qt.values(); valuesTester(qt.size(), values); qt.put(80.0, 80.0, "80.0, 80.0"); assertEquals(size + 1, qt.size()); valuesTester(size + 1, values); qt.put(75.0, 75.0, "75.0, 75.0"); assertEquals(size + 2, qt.size()); valuesTester(size + 2, values); assertTrue(qt.remove(80.0, 80.0, "80.0, 80.0")); assertEquals(size + 1, qt.size()); valuesTester(size + 1, values); }