Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class SnapshotableMap {
  4.  
  5.     int id = 0;
  6.     private HashMap<String, Integer> map = new HashMap();
  7.  
  8.  
  9.     public void put(int key, int val){
  10.         String strKey = buildKey(key, id);
  11.         map.put(strKey, val);
  12.     }
  13.  
  14.     public int get(int key){
  15.         String mostRecentKey = buildKey(key, 0);
  16.         for(int i = 1; i<=id; i++){
  17.             String currentKey = buildKey(key, i);
  18.             if(map.containsKey(currentKey)){
  19.                 mostRecentKey = currentKey;
  20.             }else{
  21.                 break;
  22.             }
  23.         }
  24.        
  25.         return map.get(mostRecentKey);
  26.     }
  27.    
  28.     private String buildKey(int key, int snapshotId){
  29.         return snapshotId + " " + key;
  30.     }
  31.  
  32.     public int snapshot(){
  33.         id++;
  34.         return id-1;
  35.     }
  36.  
  37.     public int get(int key, int snapshotId){
  38.         return map.get(buildKey(key, snapshotId));
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement