Java Implementation of LFU#4
Conversation
| FrequencyList(){ | ||
| list = new LinkedList<T>(); | ||
| } | ||
| FrequencyList(int f){ |
There was a problem hiding this comment.
Use similar spacing between functions
| // Update item frequency | ||
| nodeIt = frequencyList.listIterator(frequencyList.size() - 1); | ||
| nodeFrequencyMap.put(item, new Pair(frequency, nodeIt)); | ||
| return true; |
There was a problem hiding this comment.
Lot of indentation issues.
There was a problem hiding this comment.
Sorry will fix that ASAP!
| return list.listIterator(index); | ||
| } | ||
|
|
||
| public int getFrequency(){ |
There was a problem hiding this comment.
Maintain an ordering for getter and setter functions. Looks better ;)
There was a problem hiding this comment.
OKay that somehow makes sense as well!
|
|
@chinmaydd that's why I suggested to squash the commits! 😄 |
|
@chinmaydd will do the testcases part by tomorrow. Kindly take a look at the existing code for now! |
|
@salman-bhai Please use JUnit or some other testing tool to test your code with given test cases. Please do add the JUnit files. |
|
Alright @mohitreddy1996 will do that tonight! |
| @@ -0,0 +1,14 @@ | |||
| package lfu; | |||
There was a problem hiding this comment.
Try not to define common types like this on your own (as they will be prone to errors and re-work). There is a well known principle in programming called the DRY (DO NOT REPEAT YOURSELF).
Pairs are usually discouraged. Why not use an Auto_Value class here ?
This will also be a good learning for you.
|
|
||
| public class LFU<T> { | ||
|
|
||
| private int MAX_SIZE = 2; |
There was a problem hiding this comment.
This should be private final Integer.
try not to use primitives.
There was a problem hiding this comment.
Will do it. Any specific reason for avoiding primitives?
There was a problem hiding this comment.
Unless you are writing super-efficient low level programs, primitives don't have any real benefit. Primitives don't fit well in larger scheme of things. You will realize this when you work on Java enough.
For future references, there is nothing called "will do". You should consider making the change and only then reverting back. This ensures that code quality can be improved iteratively.
| private HashMap<Integer, ListIterator< FrequencyList<T> > > frequencyMap; | ||
| private int count = 0; | ||
|
|
||
| public void init() { |
There was a problem hiding this comment.
If the sole function of this init() is to be called from the constructor, then place this code in constructor itself.
| * @param <T> | ||
| */ | ||
|
|
||
| public class LFU<T> { |
There was a problem hiding this comment.
LFU is a bad class name. LeastFrequetlyUsedPage is a better name.
There was a problem hiding this comment.
Umm aren't long names not advised as the name of a class? LFU is a bad name though. Coulldn't think of an alternative though!
There was a problem hiding this comment.
Where did you read that Long names are not advisable ? Until 6 Words, you need not worry about long names. In my opinion, Java class name should be as descriptive as possible. If it's descriptive enough, it won't require any documentation.
In practice, I have personally written classes with names as long as 10 words. So it's fine.
| if(count >= MAX_SIZE) | ||
| delete(); | ||
|
|
||
| // If frequencyList is empty |
There was a problem hiding this comment.
Comments in java should end with '.'
| import java.util.LinkedList; | ||
| import java.util.ListIterator; | ||
|
|
||
| class FrequencyList<T> { |
There was a problem hiding this comment.
Most of this java code has too many lint / formatting errors. Did you try running a java linter on it ?
There was a problem hiding this comment.
Umm no. Didn't know what a Java linter was till now. Will do it now!
| public int getFrequency(){ | ||
| return frequency; | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
All code files should end with a new line
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * @author salman |
There was a problem hiding this comment.
If you do plan to add a javadoc, then add something useful here. And not just an @author
There was a problem hiding this comment.
Could you suggest something in this reference? Something which can be suited just for this.
There was a problem hiding this comment.
Since this is an instanciable class, you should use a singular noun describing what one instance of this class represents. For example
An element in cache that is least frequently used.
Honestly, I am skeptical about the name and responsibilities of this class.
|
|
||
| // If item exists | ||
| // Get FrequencyList of item | ||
| int frequency = nodeFrequencyMap.get(item).first; |
There was a problem hiding this comment.
Random error using IDE. Will change that ASAP.
| private int MAX_SIZE = 2; | ||
|
|
||
| private LinkedList< FrequencyList<T> > lfuList; | ||
| private HashMap<T, Pair<Integer, ListIterator<T> > > nodeFrequencyMap; |
There was a problem hiding this comment.
If I were you, I would never use ListIterator directory.
I would probably make it Iterable
There was a problem hiding this comment.
Any reason why you would do that?
There was a problem hiding this comment.
You can only iterate through an interator once. But if you take an iterable, you can easily call .iterator() on it, to get it's corresponding iterator.
Plus, ListIterator is too restrictive. I don't think you need any aspect of ListIterator that is not available in Iterator itself.
If you want gurantee over order of iteration, you should use Guava library's ImmutableCollection.
Insert, Locate, Delete Methods Added Changes made Documentation update (WebClub-NITK#3) * Documentation update * Changes made as suggested! New exception yet to be handled Code reorganised as per review Initial Java code for testcases added
Please Change if anything requires to be changed. Couldn't find proper convention for writing test cases.
Issue Resolved: #4
@adeepkit01 @mohitreddy1996 @chinmaydd Kindly review!
P.S. You can squash the commits if you want. 😄