-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java Implementation of LFU #4
base: master
Are you sure you want to change the base?
Conversation
LFU/src/lfu/FrequencyList.java
Outdated
FrequencyList(){ | ||
list = new LinkedList<T>(); | ||
} | ||
FrequencyList(int f){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use similar spacing between functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! 😄
LFU/src/lfu/LFU.java
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lot of indentation issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry will fix that ASAP!
LFU/src/lfu/FrequencyList.java
Outdated
return list.listIterator(index); | ||
} | ||
|
||
public int getFrequency(){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maintain an ordering for getter and setter functions. Looks better ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plenty to address for now.
@@ -0,0 +1,14 @@ | |||
package lfu; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be private final Integer.
try not to use primitives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do it. Any specific reason for avoiding primitives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the sole function of this init() is to be called from the constructor, then place this code in constructor itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do that now
* @param <T> | ||
*/ | ||
|
||
public class LFU<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LFU is a bad class name. LeastFrequetlyUsedPage is a better name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments in java should end with '.'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, will be done!
import java.util.LinkedList; | ||
import java.util.ListIterator; | ||
|
||
class FrequencyList<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm no. Didn't know what a Java linter was till now. Will do it now!
public int getFrequency(){ | ||
return frequency; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All code files should end with a new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be done!
import java.util.HashMap; | ||
|
||
/** | ||
* @author salman |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you suggest something in this reference? Something which can be suited just for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there so much space here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I were you, I would never use ListIterator directory.
I would probably make it Iterable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why you would do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. 😄