/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ricercabinariascuola;

import java.util.Vector;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
 *
 * @author Marco
 */
public class SpecialVector extends Vector {
    
    public SpecialVector (String csvFileName) {
        String tmp;
        String[] lista;
        
        Scanner lettore = null;
        
        try {
            lettore = new Scanner (new File(csvFileName));
        }
        catch(FileNotFoundException ex) {
            System.out.println("Errore in apertura file: " + ex.toString());            
        }
        while (lettore.hasNextLine()) {            
            tmp = lettore.nextLine();
            lista = tmp.split(";");
            this.add(lista[1]);            
        }
    }
    
    int puoEsistere = 0;
    
    public int ricercaBin (String searchedString, int idxStart, int idxStop) {
        int mid = (int) ((idxStop + idxStart) / 2);
        
        while (puoEsistere < 2) {
            if (mid == idxStop) {
                puoEsistere ++;
            }

            String tmpS = (String) this.elementAt(mid);
            int tmp = tmpS.compareToIgnoreCase(searchedString);
            if (tmp == 0) {
                return mid;
            }        
            else if (tmp > 0) {
                return this.ricercaBin(searchedString, idxStart, mid - 1);
            }
            else {
                return this.ricercaBin(searchedString, mid + 1, idxStop);
            }
        }
        return - 1;
    }
    
    public String decodifica (int posizione, String csvFileName) {
        this.clear();
        
        String tmp;
        String[] lista;
        
        Scanner lettore = null;
        
        try {
            lettore = new Scanner (new File(csvFileName));
        }
        catch(FileNotFoundException ex) {
            System.out.println("Errore in apertura file: " + ex.toString());            
        }        
        while (lettore.hasNextLine()) {            
            tmp = lettore.nextLine();
            lista = tmp.split(";");
            this.add(lista[0]);       
        }
        
        return (String) this.elementAt(posizione);
    }
}
