0

I have a Queue of class Component:

class Component(node:Integer, start:Integer, c_size:Integer);

var queue = Queue[Component]();

I would like to access an element from the queue by value and modify it, somehow like this:

if(queue.contains(neighbour)) {
   queue[neighbour].c_size += 1;
}

How can I do this?

2
  • Are you sure you need a Queue here? Commented Dec 20, 2015 at 15:52
  • Yes, I am using it in some other places as well. Commented Dec 20, 2015 at 15:55

2 Answers 2

1

It seems a bit strange to do this with a Queue, because in theory you only want to access first and/or last element of a queue. May be a ListMap would be a better idea?

I have tried something in console (Scala 2.11.7):

scala> case class Component(node:Int, start:Int, size:Int)
defined class Component
scala> val lm = ListMap[Int,Component]()
lm: scala.collection.mutable.ListMap[Int,Component] = Map()
scala> def addToList(lm:ListMap[Int,Component], element:Component) =
     | lm += (lm.size -> element)
addToList: (lm: scala.collection.mutable.ListMap[Int,Component], element: Component)scala.collection.mutable.ListMap[Int,Component]

scala> addToList(lm , Component(1,2,3))
res8: scala.collection.mutable.ListMap[Int,Component] = Map(0 -> Component(1,2,3))

scala> addToList(lm , Component(2,32,43))
res9: scala.collection.mutable.ListMap[Int,Component] = Map(1 -> Component(2,32,43), 0 -> Component(1,2,3))

scala> addToList(lm , Component(3,324,543))
res10: scala.collection.mutable.ListMap[Int,Component] = Map(2 -> Component(3,324,543), 0 -> Component(1,2,3), 1 -> Component(2,32,43))
scala> def upNeighbour(lm:ListMap[Int, Component], node:Int) = {
     | lm.withFilter({
     | case (k, v) => (v.node == node) }).map ({
     | case (k,v) => lm(k) = v.copy(node=v.node+1)})
     | }
upNeighbour: (lm: scala.collection.mutable.ListMap[Int,Component], node: Int)scala.collection.mutable.Iterable[Unit]
scala> upNeighbour(lm, 2)
res12: scala.collection.mutable.Iterable[Unit] = ArrayBuffer(())
scala> lm
res13: scala.collection.mutable.ListMap[Int,Component] = Map(1 -> Component(3,32,43), 0 -> Component(1,2,3), 2 -> Component(3,324,543))

Note: As you can see, modifying an element resulted in removing that element and inserting the other one, with the same key. Because the key is preserved, you can still iterate by the sorted version of keys and get the original order. I havent's checked if the behaviour is the same if you use var instead, though.

Sign up to request clarification or add additional context in comments.

Comments

1

This behaviour can be achieved by using map and then "performing a check for equality".

One open question in this solution is if all elements that are equal to your criteria should be changed or only the first one.

Given example assumes all elements that fulfil the requirement should be changed.

Example implementation

Data

import scala.collection.immutable.Queue

case class ReducedComponent(node:Int)

val example = Queue(1,2,3,4,3,1) map ReducedComponent

Replacing by value: All occurences which have value

def replaceByValue(in: ReducedComponent) = example.map(c => if (c == in) c.copy(node = c.node +1) else c)

Example in REPL

scala> replaceByValue(ReducedComponent(3))
res4: scala.collection.immutable.Queue[ReducedComponent] = 
  Queue(ReducedComponent(1), ReducedComponent(2), ReducedComponent(4), ReducedComponent(4), ReducedComponent(4), ReducedComponent(1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.