Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

sbshah97
Copy link
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
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
Contributor Author

Choose a reason for hiding this comment

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

Agreed! 😄

// Update item frequency
nodeIt = frequencyList.listIterator(frequencyList.size() - 1);
nodeFrequencyMap.put(item, new Pair(frequency, nodeIt));
return true;
Copy link
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
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
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
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
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
Contributor Author

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

@sbshah97
Copy link
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
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
Contributor Author

sbshah97 commented May 5, 2017

Alright @mohitreddy1996 will do that tonight!

Copy link

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

@@ -0,0 +1,14 @@
package lfu;

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;

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
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?

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() {

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

* @param <T>
*/

public class LFU<T> {

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
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!

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

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
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!

import java.util.LinkedList;
import java.util.ListIterator;

class FrequencyList<T> {

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
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!

public int getFrequency(){
return frequency;
}
}

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
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!

import java.util.HashMap;

/**
* @author salman

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

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;

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

private int MAX_SIZE = 2;

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

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

@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