Skip to content

Java Implementation of LFU#4

Open
sbshah97 wants to merge 10 commits into
WebClub-NITK:masterfrom
sbshah97:java
Open

Java Implementation of LFU#4
sbshah97 wants to merge 10 commits into
WebClub-NITK:masterfrom
sbshah97:java

Conversation

@sbshah97
Copy link
Copy Markdown
Contributor

@sbshah97 sbshah97 commented Apr 29, 2017

Issue Resolved: #4

  • Not all test cases covered! Basic test cases are all covered.
  • Kindly do let us know for suggestions for the same!
  • Code has been made as a NetBeans IDE Project! Code compiles and runs in the same! Though src directory contains all the Java Source files!

@adeepkit01 @mohitreddy1996 @chinmaydd Kindly review!

P.S. You can squash the commits if you want. 😄

FrequencyList(){
list = new LinkedList<T>();
}
FrequencyList(int f){
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! 😄

Comment thread LFU/src/lfu/LFU.java Outdated
// Update item frequency
nodeIt = frequencyList.listIterator(frequencyList.size() - 1);
nodeFrequencyMap.put(item, new Pair(frequency, nodeIt));
return true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lot of indentation issues.

Copy link
Copy Markdown
Contributor Author

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!

return list.listIterator(index);
}

public int getFrequency(){
Copy link
Copy Markdown
Contributor

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 ;)

Copy link
Copy Markdown
Contributor Author

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
Copy link
Copy Markdown
Contributor

chinmaydd commented Apr 30, 2017

  • Write some test cases for your work. It is tough to evaluate the PR only based on source of the logic.
  • Try not including "merge" commits into your PR. The maintainers will have additional work in cherry picking the useful ones ;)
  • Do we really need project files? I guess only the Java ones should work.

@sbshah97
Copy link
Copy Markdown
Contributor Author

@chinmaydd that's why I suggested to squash the commits! 😄

@sbshah97
Copy link
Copy Markdown
Contributor Author

sbshah97 commented May 1, 2017

@chinmaydd will do the testcases part by tomorrow. Kindly take a look at the existing code for now!
@mohitreddy1996 @adeepkit01 If possible please do review this as well!

@mohitreddy1996
Copy link
Copy Markdown
Contributor

@salman-bhai Please use JUnit or some other testing tool to test your code with given test cases. Please do add the JUnit files.

@sbshah97
Copy link
Copy Markdown
Contributor Author

sbshah97 commented May 5, 2017

Alright @mohitreddy1996 will do that tonight!

Copy link
Copy Markdown

@ashish1294 ashish1294 left a 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.

Comment thread Java/Pair.java
@@ -0,0 +1,14 @@
package lfu;
Copy link
Copy Markdown

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.

Comment thread Java/LFU.java

public class LFU<T> {

private int MAX_SIZE = 2;
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown

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.

Comment thread Java/LFU.java
private HashMap<Integer, ListIterator< FrequencyList<T> > > frequencyMap;
private int count = 0;

public void init() {
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do that now

Comment thread Java/LFU.java
* @param <T>
*/

public class LFU<T> {
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor Author

@sbshah97 sbshah97 May 6, 2017

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!

Copy link
Copy Markdown

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.

Comment thread Java/LFU.java
if(count >= MAX_SIZE)
delete();

// If frequencyList is empty
Copy link
Copy Markdown

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 '.'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will be done!

Comment thread Java/FrequencyList.java
import java.util.LinkedList;
import java.util.ListIterator;

class FrequencyList<T> {
Copy link
Copy Markdown

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 ?

Copy link
Copy Markdown
Contributor Author

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!

Comment thread Java/FrequencyList.java
public int getFrequency(){
return frequency;
}
} No newline at end of file
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be done!

Comment thread Java/LFU.java
import java.util.HashMap;

/**
* @author salman
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown

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.

Comment thread Java/LFU.java

// If item exists
// Get FrequencyList of item
int frequency = nodeFrequencyMap.get(item).first;
Copy link
Copy Markdown

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 ?

Copy link
Copy Markdown
Contributor Author

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.

Comment thread Java/LFU.java
private int MAX_SIZE = 2;

private LinkedList< FrequencyList<T> > lfuList;
private HashMap<T, Pair<Integer, ListIterator<T> > > nodeFrequencyMap;
Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown

@ashish1294 ashish1294 May 7, 2017

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.

sbshah97 and others added 4 commits July 17, 2017 01:23
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants