I have two classes. Class Algorithm
implements a method findIntersections()
which is a sweep line algorithm to check for intersections in O(nLogN) time.
It also implements a function addSegments
which adds objects of type Segment (two points) to a priority Queue based on the x coordinate.
public class Algorithm {
public PriorityQueue pQueue = new PriortiyQueue();
//This function adds objects of type Segments to a priority queue
public void addSegments(List<Segment> segments) {
pQueue.add(segments);
//do something
}
//This function implements a sweep line algorithm to check for Intersections.
public void findIntersection() {
while (!pQueue.isEmpty()) {
p.poll(); //removes element from Queue
// do something
}
}
}
The other class Model
loads data from a CSV file into the priority Queue. This is an intensive process which I only want to do once.
On the other hand, checkForCollissions
is called millions of times.
I want to check for collisions between the supplied segment and the rest of the segments added in the priority queue from the csv file
I do not want to be adding elements to the priority queue from scratch each time. This would not be feasible.
public class Model { public Algorithm algoObj = new Algorithm(); public ArrayList<Segment> algoObj = new ArrayList<>(); public ArrayList<Segment> segments = new ArrayList<>(); public ArrayList<Segment> single_segment = new ArrayList<>(); public boolean loadCSV() { //read csv file while ((strLine = br.readLine()) != null) { segments.add(new Segment()); //Add all segments in CSV file to ArrayLisyt algo.addSegments(segments); //Adds 4000 objects of type segment to priority Queue } } //This function is called millions of times public boolean checkForCollisions(segment_to_check) { single_segment.add(segment_to_check); //Add 1 segment. algoObj.addSegments(single_segment); //Adds 1 object of type segment to priority Queue algoObj.findIntersection(); single_segment.remove(new Segment()); //Remove above segment to get back to original data } }
TL;DR
The problem I am having is that after the first call of checkForCollisions
the priority queue has changed since findIntersection()
works by polling elements from the queue, thus altering the queue.
How do I keep the priority queue created by algoObj.addSegments() from changing between function calls?
Does this have to do witch shallow and deep copying as explained here?
I tried creating a copy of the queue at the beginning of the function and then altering the copy:
public boolean checkForCollisions(segment_to_check) {
Algorithm copy = algoObj;
single_segment.add(segment_to_check); //Add 1 segment.
copy.addSegments(single_segment); //Adds 1 object of type segment to priority Queue
copy.findIntersection();
single_segment.remove(new Segment()); //Remove above segment to get back to original data
}
}
This however does not work as it still alters the priority queue of the original algoObj
.
I believe this is a beginner's question and stems from my lack of proper understanding when working with OO languages. Any help would be appreciated.