How to create a binary search on a file in SCALA

0

Good morning everyone, I have the following file:

10,44,22
10,47,12
15,38,3
15,41,30
16,44,15
16,47,18
22,38,21
22,41,42
34,44,40
34,47,36
40,38,39
40,41,42
45,38,27
45,41,30
46,44,45
46,47,48

About this file I want to make different queries, for example bring me the values that have the rows (x, 44, y) then the program brings me 10,44,22; 16.44.15; 34.44.40; 46,44,45, once the rows have been obtained, separate the variables x, and then bring me 10,22, 16,15, 34,40, 46,45. For this I made a program that reads the entire file and as it finds the numbers I put a conditional to store the value x, and a vector:

        val in = newScanner("patSPO.csv")
        val con:convst = new convst()
        var yi:Int = 0
        val nodey:Value = Val(y).value
        yi=con.convN2I(nodey.name)
        while (scannerHasNext(in)) {
          val s  = in.nextt(',')
          val p = in.nextt(',')
          val o  = in.nextt('\n')
          if (p == yi) {
            val fields:Fields = Vector(s,o)

          }
        }

My problem at the moment is that I have to read the whole file every time I want to make a query, and this for very large files takes a lot of time, My question is if there is any way to upload this file to memory, for example a Map (ListMap, Collection, TreeMap) which allows me to make queries in a faster way, and does not have to consult the file every time need?

    
asked by Vladimir Rincon 20.06.2017 в 23:19
source

1 answer

0

A very comfortable way is using case classes :

case class Terna(x,y,z)

We build a sequence of these triples:

import scala.io.Source

val f = Source.fromFile("patSPO.csv")

val ternasIt = for {
  line <- f.getLines
  Array(a,b,c) = line.split(",").map(_.toInt)
} yield Terna(a,b,c)
val ternas = ternasIt.toSeq

And from here to operate. Following the example that you put, to bring the terns of type Ternas(_,44,_) :

val res = ternas collect { case Terna(x,44,z) => (x,z) }
    
answered by 25.10.2017 в 03:50