Skip to content

Update operation. #24

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;


export default class TinyQueue<T> {
public data : T[];
Expand All @@ -7,4 +8,5 @@ export default class TinyQueue<T> {
peek () : T | undefined;
pop () : T | undefined;
push (item: T) : void;
update (newItem: T, criteria? : Criteria<T>);
}
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

let pos = this.data.findIndex(function(value, index, array) {
return criteria(value);
});
if (pos >= 0) {
this.data[pos] = newValue;
let childPos = (pos << 1) + 1;
if (pos == 0)
this._down(pos);
else if (childPos >= this.length)
this._up(pos);
else if (this.compare(newValue, this.data[childPos]) < 0)
this._up(pos);
else if (this.compare(newValue, this.data[childPos]) > 0)
this._down(pos);
return true;
}
return false;
}

_up(pos) {
const {data, compare} = this;
const item = data[pos];
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,23 @@ test('handles init with empty array', (t) => {

t.end();
});

test('updates few elements', (t) => {
const queue = new TinyQueue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(1);

let res = queue.update(4, (val) => { return val == 1 });
t.equal(res, true);
t.equal(queue.length, 4);
t.equal(queue.peek(), 1);

res = queue.update(4, (val) => { return val == 1 });
t.equal(res, true);
t.equal(queue.length, 4);
t.equal(queue.peek(), 2);

t.end();
});