modifying a Vector while iterating over it

87 views
Skip to first unread message

Russ P.

unread,
Jan 1, 2017, 5:22:13 PM1/1/17
to scala-user
Suppose I am iterating over a Vector:

var assigned = Vector[Trajectory]()

for (trajectory <- trajectories.sortBy(_.freezeTime)) assigned :+= deconflict(trajectory)

Here I am taking a list of aircraft trajectories and deconflicting them one at a time as they reach a time called the "freeze time." It works fine most of the time, but occasionally a conflict-free trajectory is not found. In that case, I wish to simulate a holding pattern (or other maneuver) to delay the flight, then I will try again later to deconflict it. I will simulate the delay with a simple time-shift for now and reinsert it into the list of trajectories (to be deconflicted later). It's freeze time will change. Is there a recommended way to do something like this? If so, what? Thanks.

Russ P.

unread,
Jan 3, 2017, 9:02:18 PM1/3/17
to scala-user
In case anyone is interested, I think I figured out a good way to handle this problem.

I use a while loop rather than a for loop. Here is the simplified pattern:

while (trajectories.nonEmpty) { // trajectories is a var with the original conflicted trajectories

  val trajectory = trajectories.head
  trajectories = trajectories.tail
  val trial = deconflict(trajectory)

  if (trial.success) assign(trajectory)
  else {

Russ Paielli

unread,
Jan 3, 2017, 9:05:19 PM1/3/17
to scala-user
Crap, I hit a tab and the darn thing posted! Oh well, you get the idea.

--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/e98lIpgfRAs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Russ Paielli

unread,
Jan 3, 2017, 9:10:16 PM1/3/17
to scala-user
I wonder who the genius was at google who decided that hitting the tab key should post a comment?

I had something like this happen to me years ago, causing me to send an important email before it was ready. I figured that they provide this functionality for people who don't have a mouse available. But the stupid thing should at least verify that you are ready to post or send, shouldn't it?

Russ P.

unread,
Jan 5, 2017, 10:42:23 AM1/5/17
to scala-user
I have a question about sorting efficiency. Suppose I have a sorted Vector around 2,000 long, and I wish to add an element in sorted order. The simple way is to just append the element and then resort (using trajectories.sortBy(_.freezeTime)). But is that efficient, or is there a better way to do it without "rolling your own"?

Oliver Ruebenacker

unread,
Jan 5, 2017, 10:46:37 AM1/5/17
to Russ P., scala-user

     Hello,

  It would be very inefficient to append and sort again.

  Most efficient is to find the place the new entry should be inserted using binary search, then split Vector at that place and concatenate again with the new element in between.

     Best, Oliver


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Dennis Haupt

unread,
Jan 5, 2017, 11:24:29 AM1/5/17
to Oliver Ruebenacker, Russ P., scala-user
this sounds like something a less than 32ary tree should be better at
 
Gesendet: Donnerstag, 05. Januar 2017 um 16:46 Uhr
Von: "Oliver Ruebenacker" <cur...@gmail.com>
An: "Russ P." <russ.p...@gmail.com>
Cc: scala-user <scala...@googlegroups.com>
Betreff: Re: [scala-user] Re: modifying a Vector while iterating over it
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

 

--

You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

Lanny Ripple

unread,
Jan 9, 2017, 4:06:15 PM1/9/17
to scala-user
If you are looking to continually process over elements in sorted order, pull off the smallest, modify its sort location, and the put it back in the collection to repeat until some stopping point you should look at heap implementations.  Without knowing your exact requirements though it's really tough to suggest what to use.

  -ljr
Reply all
Reply to author
Forward
0 new messages