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

Update operation. #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Update operation. #24

wants to merge 1 commit into from

Conversation

karastojko
Copy link

No description provided.

@@ -33,6 +33,26 @@ export default class TinyQueue {
return this.data[0];
}

update(newValue, criteria) {
Copy link

Choose a reason for hiding this comment

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

Maybe

update(newValue, criteria) {
    const pos = this.data.findIndex(criteria);
    if (pos < 0) {
        return false;
    }

    this.data[pos] = newValue;
    
    const childPos = (pos << 1) + 1;
    const needsDown = (pos === 0 || (childPos < this.length && this.compare(newValue, this.data[childPos]) > 0));
    
    this[needsDown ? '_down' : '_up'](pos);

    return true;
}

@@ -1,4 +1,5 @@
export declare type Comparator<T> = (a: T, b: T) => number;
export declare type Criteria<T> = (val: T) => boolean;
Copy link

Choose a reason for hiding this comment

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

Suggested change
export declare type Criteria<T> = (val: T) => boolean;
export declare type Criteria<T> = (val: T, index: number, values: T[]) => boolean;

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.

2 participants