diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f44ca06 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/LFU/nbproject/private/ +/LFU/build/ +/LFU/dist/ \ No newline at end of file diff --git a/Java/FrequencyList.java b/Java/FrequencyList.java new file mode 100644 index 0000000..cefb03d --- /dev/null +++ b/Java/FrequencyList.java @@ -0,0 +1,40 @@ +package lfu; +import java.util.LinkedList; +import java.util.ListIterator; + +class FrequencyList { + int frequency; + LinkedList list; + + FrequencyList(){ + list = new LinkedList(); + } + FrequencyList(int f){ + list = new LinkedList(); + frequency = f; + } + + public void add(T t){ + list.add(t); + } + + public void remove(T t){ + list.remove(t); + } + + public ListIterator listIterator(int index){ + return list.listIterator(index); + } + + public int size(){ + return list.size(); + } + + public T get(int index){ + return list.get(index); + } + + public int getFrequency(){ + return frequency; + } +} \ No newline at end of file diff --git a/Java/LFU.java b/Java/LFU.java new file mode 100644 index 0000000..a78e791 --- /dev/null +++ b/Java/LFU.java @@ -0,0 +1,144 @@ +package lfu; +import java.util.LinkedList; +import java.util.ListIterator; +import java.util.HashMap; + +/** + * @author salman + * @param + */ + +public class LFU { + + private int MAX_SIZE = 2; + + private LinkedList< FrequencyList > lfuList; + private HashMap > > nodeFrequencyMap; + private HashMap > > frequencyMap; + private int count = 0; + + public void init() { + lfuList = new LinkedList< FrequencyList >(); + nodeFrequencyMap = new HashMap > >(); + + frequencyMap = new HashMap > >(); + count = 0; + } + + LFU(){ + init(); + } + + LFU(int size){ + MAX_SIZE = size; + init(); + } + + public void insert(T item){ + if(count >= MAX_SIZE) + delete(); + + // If frequencyList is empty + if(!frequencyMap.containsKey(1)){ + lfuList.listIterator(0).add(new FrequencyList(1)); + frequencyMap.put(1, lfuList.listIterator(0)); + } + + ListIterator< FrequencyList > fListIt = frequencyMap.get(1); + FrequencyList frequencyList = fListIt.next(); + fListIt.previous(); + frequencyList.listIterator(frequencyList.size()).add(item); + + // Update item frequency + nodeFrequencyMap.put(item, new Pair(1, frequencyList.listIterator(frequencyList.size() - 1))); + + // Update Count + count++; + } + + public boolean lookup(T item){ + + // If item does not exist + if(!nodeFrequencyMap.containsKey(item)){ + insert(item); + return false; + } + + // If item exists + // Get FrequencyList of item + int frequency = nodeFrequencyMap.get(item).first; + ListIterator nodeIt = nodeFrequencyMap.get(item).second; + ListIterator< FrequencyList > fListIt = frequencyMap.get(frequency); + FrequencyList frequencyList = fListIt.next(); + + fListIt.previous(); + System.out.println(item + " " + frequency + " " + frequencyList.getFrequency() + " " + fListIt); + + // Remove Item from current frequencyList + nodeIt.next(); + nodeIt.previous(); + nodeIt.remove(); + + // Check if frequencyList is empty + if(frequencyList.size() == 0){ + // Clean Up + frequencyMap.remove(frequencyList.getFrequency()); + fListIt.remove(); + } + + // Add item to list frequency + 1 + frequency++; + + // If frequencyList is empty + if(!frequencyMap.containsKey(frequency)){ + fListIt.add(new FrequencyList(frequency)); + fListIt.previous(); + frequencyMap.put(frequency, fListIt); + } + + frequencyList = fListIt.next(); + fListIt.previous(); + frequencyList.listIterator(frequencyList.size()).add(item); + + // Update item frequency + nodeIt = frequencyList.listIterator(frequencyList.size() - 1); + nodeFrequencyMap.put(item, new Pair(frequency, nodeIt)); + return true; + } + + public T delete(){ + ListIterator< FrequencyList > fListIt = lfuList.listIterator(0); + FrequencyList frequencyList = fListIt.next(); + + fListIt.previous(); + T item = frequencyList.get(0); + ListIterator nodeIt = nodeFrequencyMap.get(item).second; + + nodeIt.next(); + nodeIt.remove(); + nodeFrequencyMap.remove(item); + + if(frequencyList.size() == 0){ + // Clean Up + frequencyMap.remove(frequencyList.getFrequency()); + fListIt.remove(); + } + + // Update Count + count--; + System.out.println("Deleted "+item); + return item; + } + + public static void main(String args[]) { + LFU freqlfu = new LFU(); + + freqlfu.insert(10); + + System.out.println(freqlfu.lookup(10)); + System.out.println(freqlfu.lookup(10)); + System.out.println(freqlfu.lookup(20)); + System.out.flush(); + System.out.println(freqlfu.lookup(10)); + } +} diff --git a/Java/Pair.java b/Java/Pair.java new file mode 100644 index 0000000..3ac86c1 --- /dev/null +++ b/Java/Pair.java @@ -0,0 +1,14 @@ +package lfu; + +class Pair { + U first; + V second; + + Pair(){} + + Pair(U u, V v){ + first = u; + second = v; + } + +}