Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * @author Damjan
- */
- public class RoutingHashJava {
- public static void main(String[] args) throws IOException {
- BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
- CBHT<Router, IPs> routingTable = new CBHT<>(150);
- int numAddresses = Integer.parseInt(data.readLine());
- for (int i = 0; i < numAddresses; ++i) {
- String routerAddress = data.readLine();
- Router r = new Router(routerAddress);
- IPs ip = new IPs();
- String ips = data.readLine();
- if (ips.contains(",")) {
- String[] parts = ips.split(",");
- for (int j = 0; j < parts.length; ++j) {
- ip.addAddress(parts[j]);
- }
- } else {
- ip.addAddress(ips);
- }
- routingTable.insert(r, ip);
- }
- int check = Integer.parseInt(data.readLine());
- for (int k = 0; k < check; ++k) {
- String router = data.readLine();
- Router rR = new Router(router);
- String addresses = data.readLine();
- boolean postoi = routingTable.contains(rR, addresses);
- System.out.println(postoi ? "postoi" : "ne postoi");
- }
- data.close();
- }
- }
- class IPs {
- private List<String> addresses;
- public IPs() {
- addresses = new ArrayList<>();
- }
- public List<String> getAddresses() {
- return addresses;
- }
- public void addAddress(String address) {
- if (!addresses.contains(address)) {
- addresses.add(address);
- }
- }
- public boolean containsAddress(String address) {
- int index = address.lastIndexOf('.');
- address = address.substring(0, index);
- for (int i = 0; i < addresses.size(); ++i) {
- String takeIP = addresses.get(i);
- int indexNew = takeIP.lastIndexOf(".");
- takeIP = takeIP.substring(0, indexNew);
- return address.equals(takeIP);
- }
- return false;
- }
- }
- class Router implements Comparable<Router> {
- private String address;
- public Router(String address) {
- this.address = address;
- }
- public String getAddress() {
- return address;
- }
- @Override
- public boolean equals(Object obj) {
- Router r = (Router) obj;
- return address.equals(r.address);
- }
- @Override
- public int hashCode() {
- int sum = 0;
- for (int i = 0; i < address.length(); ++i) {
- sum += address.charAt(i);
- }
- return sum;
- }
- @Override
- public int compareTo(Router o) {
- if (address.equals(o.address)) {
- return 1;
- } else {
- return 0;
- }
- }
- }
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K, E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- boolean contains(K key, String val) {
- SLLNode<MapEntry<K, E>> tmp = search(key);
- if (tmp != null) {
- IPs addresses = (IPs) tmp.element.value;
- return addresses.containsAddress(val);
- } else {
- return false;
- }
- }
- public SLLNode<MapEntry<K, E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key is
- // equal
- // to targetKey. Return a link to that node (or null if there is none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.equals(((MapEntry<K, E>) curr.element).key)) {
- return curr;
- }
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- if (pred == null) {
- buckets[b] = curr.succ;
- } else {
- pred.succ = curr.succ;
- }
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- }
- class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry(K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo(K that) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K, E> other = (MapEntry<K, E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString() {
- return "<" + key + "," + value + ">";
- }
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment