davegimo

es1

Aug 30th, 2023
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. class Website {
  7.     public int k;
  8.     public int np;
  9.  
  10.     public Website(int k, int np) {
  11.         this.k = k;
  12.         this.np = np;
  13.     }
  14.  
  15. }
  16.  
  17. public class Esercizio1 {
  18.     public Website[] websites;
  19.     public ArrayList<Website>[] wl;
  20.     public Random randomMatricola;
  21.     public Random randomChiave;
  22.     public int sizeOfWl;
  23.     public int numeroElementi;
  24.     public int iterazioni_totali_verifica;
  25.     public int numero_richieste_verifica;
  26.     public int iterazioni_totali_inserisci;
  27.     public int numero_richieste_inserisci;
  28.  
  29.     public Esercizio1() {
  30.         // this.websites = new Website[1000000];
  31.         this.sizeOfWl = 20000;
  32.         this.wl = new ArrayList[sizeOfWl];
  33.         iterazioni_totali_verifica = 0;
  34.         numero_richieste_verifica = 0;
  35.         iterazioni_totali_inserisci = 0;
  36.         numero_richieste_inserisci = 0;
  37.         numeroElementi = 0;
  38.         this.randomMatricola = new Random(922595);
  39.         this.randomChiave = new Random(7105419);
  40.  
  41.     }
  42.  
  43.     public void inserisci(int k, int np) {
  44.  
  45.         if (!this.verifica(k) && k >= 1 && k <= 1000000000 && np >= 1 && np <= 700) {
  46.             Website newWebsite = new Website(k, np);
  47.             int index = k % this.sizeOfWl;
  48.  
  49.             // non comprendo la differenza tra if ed else nell'add();
  50.             if (this.wl[index] == null) {
  51.                 this.wl[index] = new ArrayList<Website>();
  52.                 this.wl[index].add(newWebsite);
  53.  
  54.             } else {
  55.                 this.wl[index].add(newWebsite);
  56.             }
  57.  
  58.             this.numeroElementi++;
  59.             iterazioni_totali_inserisci++; // registro iterazione per caso arraylist nullo
  60.             numero_richieste_inserisci++; // quante volte chiamo la funzione
  61.  
  62.         }
  63.  
  64.     }
  65.  
  66.     public int size() {
  67.         return this.numeroElementi;
  68.     }
  69.  
  70.     public void stampa_(int k) {
  71.  
  72.         if (verifica(k)) {
  73.  
  74.             int index = k % sizeOfWl;
  75.  
  76.             for (int i = 0; i < this.wl[index].size(); i++) {
  77.                 if (wl[index].get(i).k == k) {
  78.                     System.out.println("<" + wl[index].get(i).k + ", " + wl[index].get(i).np + ">");
  79.                 }
  80.             }
  81.  
  82.         } else {
  83.             System.out.println("sito non presente");
  84.         }
  85.     }
  86.  
  87.     public void stampa(int k) {
  88.         int index = k % sizeOfWl;
  89.         for (int i = 0; i < this.wl[index].size(); i++) {
  90.             if (wl[index].get(i).k == k) {
  91.                 System.out.println("<" + wl[index].get(i).k + ", " + wl[index].get(i).np + ">");
  92.             }
  93.         }
  94.     }
  95.  
  96.     public boolean verifica(int k) {
  97.  
  98.         int index = k % sizeOfWl;
  99.  
  100.         if (this.wl[index] != null) {
  101.  
  102.             for (int i = 0; i < this.wl[index].size(); i++) {
  103.                 iterazioni_totali_verifica++;
  104.                 if (wl[index].get(i).k == k) {
  105.                     numero_richieste_verifica++; // incremento per iterazioni
  106.                     stampa(k);
  107.                     return true;
  108.                 }
  109.  
  110.             }
  111.         }
  112.         iterazioni_totali_verifica++; // accesso per zona di memoria
  113.         numero_richieste_verifica++; // quando chiamo la funzione
  114.         return false;
  115.     }
  116.  
  117.     public static void main(String[] args) {
  118.         Locale.setDefault(Locale.US);
  119.  
  120.         Esercizio1 es = new Esercizio1();
  121.         // System.out.println("numero iniziale di elementi inseriti nella struttura
  122.         // dati: " + es.size());
  123.  
  124.         // Generatore iniziale di coppie con numero di matricola
  125.         int CAP = 300;
  126.  
  127.         for (int i = 0; i < CAP; i++) {
  128.             int chiave = es.randomMatricola.nextInt(1000000000) + 1;
  129.             int numeroPagine = es.randomMatricola.nextInt(700) + 1;
  130.             // System.out.println(chiave + " " + numeroPagine);
  131.             es.inserisci(chiave, numeroPagine);
  132.         }
  133.  
  134.         int prev_accessi_verifica = es.iterazioni_totali_verifica;
  135.         String fileName = "output.txt";
  136.  
  137.         try {
  138.             // Create a FileWriter and BufferedWriter to write to the file
  139.             FileWriter fileWriter = new FileWriter(fileName);
  140.             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  141.  
  142.             // Your loop
  143.             for (int i = 0; i < CAP; i++) {
  144.                 int chiave = es.randomChiave.nextInt(1000000000) + 1;
  145.                 es.verifica(chiave);
  146.                 String line = chiave + " " + (es.iterazioni_totali_verifica - prev_accessi_verifica);
  147.                 bufferedWriter.write(line);
  148.                 bufferedWriter.newLine();
  149.                 prev_accessi_verifica = es.iterazioni_totali_verifica;
  150.             }
  151.  
  152.             // Close the BufferedWriter to flush and release resources
  153.             bufferedWriter.close();
  154.  
  155.             System.out.println("Lines written to " + fileName);
  156.         } catch (IOException e) {
  157.             e.printStackTrace();
  158.         }
  159.  
  160.         // System.out.println("Iterazioni inserisci " + es.iterazioni_totali_inserisci);
  161.         // System.out.println("richieste inserisci " + es.numero_richieste_inserisci);
  162.  
  163.         // System.out.println("Iterazioni verifica " + es.iterazioni_totali_verifica);
  164.         // System.out.println("richieste verifica " + es.numero_richieste_verifica);
  165.         System.out.println();
  166.         double NMA_verifica = (double) es.iterazioni_totali_verifica / es.numero_richieste_verifica;
  167.  
  168.         System.out.println("Numero medio di accesi (NMA) per verifica(): " + NMA_verifica);
  169.  
  170.         // System.out.println("Siti presenti: " + es.size());
  171.     }
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment