Quantcast

modifying a Vector while iterating over it

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

modifying a Vector while iterating over it

Russ P.
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Russ P.
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Russ P.
Crap, I hit a tab and the darn thing posted! Oh well, you get the idea.

On Tue, Jan 3, 2017 at 6:02 PM, Russ P. <[hidden email]> wrote:
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.



--

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Russ P.
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?

On Tue, Jan 3, 2017 at 6:05 PM, Russ Paielli <[hidden email]> wrote:
Crap, I hit a tab and the darn thing posted! Oh well, you get the idea.

On Tue, Jan 3, 2017 at 6:02 PM, Russ P. <[hidden email]> wrote:
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.



--



--

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Russ P.
In reply to this post by Russ P.
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"?

On Tuesday, January 3, 2017 at 6:02:18 PM UTC-8, Russ P. wrote:
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Oliver Ruebenacker

     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


On Thu, Jan 5, 2017 at 10:42 AM, Russ P. <[hidden email]> wrote:
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"?


On Tuesday, January 3, 2017 at 6:02:18 PM UTC-8, Russ P. wrote:
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aw: Re: [scala-user] Re: modifying a Vector while iterating over it

Dennis Haupt-2
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" <[hidden email]>
An: "Russ P." <[hidden email]>
Cc: scala-user <[hidden email]>
Betreff: Re: [scala-user] Re: modifying a Vector while iterating over it
 
     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
 
 
On Thu, Jan 5, 2017 at 10:42 AM, Russ P. <[hidden email]> wrote:
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"?


On Tuesday, January 3, 2017 at 6:02:18 PM UTC-8, Russ P. wrote:
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 {
 
 
 
 


On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.
 

 

--
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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: modifying a Vector while iterating over it

Lanny Ripple
In reply to this post by Russ P.
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

On Thursday, January 5, 2017 at 9:42:23 AM UTC-6, Russ P. wrote:
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"?

On Tuesday, January 3, 2017 at 6:02:18 PM UTC-8, Russ P. wrote:
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 {






On Sunday, January 1, 2017 at 2:22:13 PM UTC-8, Russ P. wrote:
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.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...