Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created December 8, 2017 23:48
Show Gist options
  • Save Robijnvogel/1da4c9ba959590bfd87fb1dbbdb10108 to your computer and use it in GitHub Desktop.
Save Robijnvogel/1da4c9ba959590bfd87fb1dbbdb10108 to your computer and use it in GitHub Desktop.
/*
* 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 hashmap;
import java.util.HashMap;
import devobjects.Location;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
* @author Mathijs
*/
public class LocationMap extends HashMap<Location, Location> {
private final Map<Integer, Map<Integer, Map<Integer, Map<Integer, Location>>>> keyToValue;
private final Map<Integer, Map<Integer, Map<Integer, Map<Integer, Set<Location>>>>> valueToKeys;
public LocationMap() {
super();
keyToValue = new HashMap();
valueToKeys = new HashMap();
}
@Override
public Location put(Location origin, Location destination) {
//d
if (!keyToValue.containsKey(origin.getD())) {
keyToValue.put(origin.getD(), new HashMap());
}
Map<Integer, Map<Integer, Map<Integer, Location>>> xyzToValue = keyToValue.get(origin.getD());;
//x
if (!xyzToValue.containsKey(origin.getX())) {
xyzToValue.put(origin.getX(), new HashMap());
}
Map<Integer, Map<Integer, Location>> yzToValue = xyzToValue.get(origin.getX());;
//y
if (!yzToValue.containsKey(origin.getY())) {
yzToValue.put(origin.getY(), new HashMap());
}
Map<Integer, Location> zToValue = yzToValue.get(origin.getY());;
//z
zToValue.put(origin.getZ(), destination);
if (!valueToKeys.containsKey(destination.getD())) {
valueToKeys.put(destination.getD(), new HashMap());
}
Map<Integer, Map<Integer, Map<Integer, Set<Location>>>> xyzToKeys = valueToKeys.get(destination.getD());;
//x
if (!xyzToKeys.containsKey(destination.getX())) {
xyzToKeys.put(destination.getX(), new HashMap());
}
Map<Integer, Map<Integer, Set<Location>>> yzToKeys = xyzToKeys.get(destination.getX());;
//y
if (!yzToKeys.containsKey(destination.getY())) {
yzToKeys.put(destination.getY(), new HashMap());
}
Map<Integer, Set<Location>> zToKeys = yzToKeys.get(destination.getY());;
//z
if (!zToKeys.containsKey(destination.getZ())) {
zToKeys.put(destination.getZ(), new HashSet());
}
Set<Location> keys = zToKeys.get(destination.getZ());
keys.add(origin);
return super.put(origin, destination);
}
@Override
public Location remove(Object o) {
}
@Override
public Location get(Object o) {
if (!(Location.class.isInstance(o))) {
return null;
}
//else
Location origin = (Location) o;
Map<Integer, Map<Integer, Map<Integer, Location>>> xyzToValue = keyToValue.get(origin.getD());
if (xyzToValue == null) {
return null;
}
//else
Map<Integer, Map<Integer, Location>> yzToValue = xyzToValue.get(origin.getX());
if (yzToValue == null) {
return null;
}
//else
Map<Integer, Location> zToValue = yzToValue.get(origin.getY());
if (zToValue == null) {
return null;
}
//else
Location destination = zToValue.get(origin.getZ());
return destination; //even if it's null
}
@Override
public boolean containsKey(Object o) {
if (!(Location.class.isInstance(o))) {
return false;
}
//else
Location origin = (Location) o;
Map<Integer, Map<Integer, Map<Integer, Location>>> xyzToValue = keyToValue.get(origin.getD());
if (xyzToValue == null) {
return false;
}
//else
Map<Integer, Map<Integer, Location>> yzToValue = xyzToValue.get(origin.getX());
if (yzToValue == null) {
return false;
}
//else
Map<Integer, Location> zToValue = yzToValue.get(origin.getY());
if (zToValue == null) {
return false;
}
//else
Location destination = zToValue.get(origin.getZ());
return (destination != null);
}
@Override
public boolean containsValue(Object o) {
}
public Set<Location> getKeys(Location destination) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment