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

Refactoring part in adding process in Separate chaning feature #47

Open
Qorxi opened this issue Apr 2, 2021 · 0 comments
Open

Refactoring part in adding process in Separate chaning feature #47

Qorxi opened this issue Apr 2, 2021 · 0 comments

Comments

@Qorxi
Copy link

Qorxi commented Apr 2, 2021

https://github.com/williamfiset/data-structures/blob/b63739f38cbc405d5d2ebcbae2ce02f37bde35fe/com/williamfiset/datastructures/hashtable/HashTableSeparateChaining.java#L152

I think improvement is needed in the bucketInsertEntry () method, because by going through this situation according to the bucket value being null in 152 rows, the same condition may appear as return null in the bucketSeekEntry method, accordingly we understand that there is no addition in that index according to the bucketIndex-given in the condition of correctness of the condition in 152 rows. Therefore, I think there is no need for additional checking, so it should not be logged into the bucketSeekEntry () method, we can do such a refactoring by considering these (I sampled the change over your codes):

private V bucketInsertEntry (int bucketIndex, Entry <K, V> entry) {

    LinkedList <Entry <K, V >> bucket = table [bucketIndex];

    if (bucket == null) {
        table [bucketIndex] = bucket = new LinkedList<>();
        bucket.add (entry);
        if (++ size> threshold) resizeTable ();
        return null; // Use null to indicate that there was no previous entry
    }

    Entry <K, V> existentEntry = bucketSeekEntry (bucketIndex, entry.key);
    if (!existentEntry == null) {
      V oldVal = existentEntry.value;
      existentEntry.value = entry.value;
      return oldVal;
    }
     
    return null;
  } 
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

No branches or pull requests

1 participant