Advertisement
metalni

APS Labs 6 Staticko Rutiranje

Dec 14th, 2020 (edited)
1,710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class SLLNode<E> {
  6.     protected E element;
  7.     protected SLLNode<E> succ;
  8.  
  9.     public SLLNode(E elem, SLLNode<E> succ) {
  10.         this.element = elem;
  11.         this.succ = succ;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return element.toString();
  17.     }
  18. }
  19.  
  20. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  21.     K key;
  22.     E value;
  23.  
  24.     public MapEntry (K key, E val) {
  25.         this.key = key;
  26.         this.value = val;
  27.     }
  28.  
  29.     public int compareTo (K that) {
  30.         @SuppressWarnings("unchecked")
  31.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  32.         return this.key.compareTo(other.key);
  33.     }
  34.  
  35.     public String toString () {
  36.         return "<" + key + "," + value + ">";
  37.     }
  38. }
  39.  
  40. class HashMap<K extends Comparable<K>, E> {
  41.     private SLLNode<MapEntry<K,E>>[] buckets;
  42.  
  43.     @SuppressWarnings("unchecked")
  44.     public HashMap(int m) {
  45.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  46.     }
  47.  
  48.     private int hash(K key) {
  49.         return Math.abs(key.hashCode()) % buckets.length;
  50.     }
  51.  
  52.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  53.         int b = hash(targetKey);
  54.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  55.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  56.                 return curr;
  57.         }
  58.         return null;
  59.     }
  60.  
  61.     public void insert(K key, E val) {
  62.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  63.         int b = hash(key);
  64. //        for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  65. //            if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  66. //                curr.element = newEntry;
  67. //                return;
  68. //            }
  69. //        }
  70.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  71.     }
  72.  
  73.     public void delete(K key) {
  74.         int b = hash(key);
  75.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  76.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  77.                 if (pred == null)
  78.                     buckets[b] = curr.succ;
  79.                 else
  80.                     pred.succ = curr.succ;
  81.                 return;
  82.             }
  83.         }
  84.     }
  85.  
  86.     public String toString() {
  87.         String temp = "";
  88.         for (int i = 0; i < buckets.length; i++) {
  89.             temp += i + ":";
  90.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  91.                 temp += curr.element.toString() + " ";
  92.             }
  93.             temp += "\n";
  94.         }
  95.         return temp;
  96.     }
  97. }
  98.  
  99. public class RoutingHashJava {
  100.     public static boolean checkRange(String sourceIp, String sourceSubnet) {
  101.         int c1=0, c2=0;
  102.         String [] ip = sourceIp.split("\\.");
  103.         String [] subnet = sourceSubnet.split("\\.");
  104.         int [] ipOctaves = new int[4];
  105.         int [] subnetOctaves = new int[4];
  106.         for (String s:ip) {
  107.             ipOctaves[c1] = Integer.parseInt(s);
  108.             c1++;
  109.         }
  110.         for (String s:subnet) {
  111.             subnetOctaves[c2] = Integer.parseInt(s);
  112.             c2++;
  113.         }
  114.  
  115.         int finalIP = ((ipOctaves[0] & 0xFF) << 24) |
  116.                       ((ipOctaves[1] & 0xFF) << 16) |
  117.                       ((ipOctaves[2] & 0xFF) << 8)  |
  118.                       ((ipOctaves[3] & 0xFF) << 0);
  119.  
  120.         int finalSubnet = ((subnetOctaves[0] & 0xFF) << 24) |
  121.                           ((subnetOctaves[1] & 0xFF) << 16) |
  122.                           ((subnetOctaves[2] & 0xFF) << 8)  |
  123.                           ((subnetOctaves[3] & 0xFF) << 0);
  124.  
  125.         int mask = -1 << 8;
  126.  
  127.         return (finalIP & mask) == (finalSubnet & mask);
  128.     }
  129.  
  130.     public static void main(String [] args) throws IOException {
  131.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  132.         int nRouters = Integer.parseInt(input.readLine());
  133.         HashMap<String, String> hashMap = new HashMap<>(31);
  134.  
  135.         for(int i=0; i<nRouters; i++) {
  136.             String routerInterface = input.readLine();
  137.             String routingAddresses = input.readLine();
  138.             String[] split = routingAddresses.split(",");
  139.             for (String s: split)
  140.                 hashMap.insert(routerInterface, s);
  141.         }
  142. //        System.out.println(hashMap);
  143.  
  144.         int nAttempts = Integer.parseInt(input.readLine());
  145.         for(int i=0; i<nAttempts; i++) {
  146.             boolean check = false;
  147.             String routerInterface = input.readLine();
  148.             String routingAddress = input.readLine();
  149.             SLLNode<MapEntry<String, String>> search = hashMap.search(routerInterface);
  150.             if(search != null) {
  151.                 while(search != null) {
  152.                     check = checkRange(routingAddress, search.element.value);
  153.                     if(check == true)
  154.                         break;
  155.                     search = search.succ;
  156.                 }
  157.             }
  158.             if(check == true)
  159.                 System.out.println("postoi");
  160.             else
  161.                 System.out.println("ne postoi");
  162.         }
  163.     }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement