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

Strange behavior after clone #21

Open
AlexMost opened this issue Feb 24, 2017 · 3 comments
Open

Strange behavior after clone #21

AlexMost opened this issue Feb 24, 2017 · 3 comments

Comments

@AlexMost
Copy link

const Heap = require('heap');

function msgidComparator(e1, e2) {
    if (e1.msgid > e2.msgid) {
        return 1;
    }
    if (e2.msgid > e1.msgid) {
        return -1;
    }
    return 0;
}

const data = [
    { msgid: 'aaaaa', msgstr: '' },
    { msgid: 'ccccc', msgstr: '' },
    { msgid: 'eeeee', msgstr: '' },
    { msgid: 'bbbb', msgstr: '' },
    { msgid: 'fffff', msgstr: '' }
]

const h = new Heap(msgidComparator);
h.push(data[0]);
h.push(data[1]);
h.push(data[2]);
h.push(data[3]);
h.push(data[4]);

const h1 = h.copy();

console.log(h1.pop());
console.log(h1.pop());
console.log(h1.pop());
console.log(h1.pop());
console.log(h1.pop());

Expected to see:

{ msgid: 'aaaaa', msgstr: '' }
{ msgid: 'bbbb', msgstr: '' }
{ msgid: 'ccccc', msgstr: '' }
{ msgid: 'eeeee', msgstr: '' }
{ msgid: 'fffff', msgstr: '' }

but have instead

{ msgid: 'aaaaa', msgstr: '' }
{ msgid: 'eeeee', msgstr: '' }
{ msgid: 'fffff', msgstr: '' }
{ msgid: 'bbbb', msgstr: '' }
{ msgid: 'ccccc', msgstr: '' }

Original object behaves well, the order is correct but clone seems to be broken.

@safwan-moha
Copy link

Is this project active? Shall I look in to this issue?

@char8x
Copy link

char8x commented Aug 15, 2022

The same clone issue also mentioned here

@KyleJune
Copy link

I believe the issue is that the internal data gets copied as is but the compare function used by the heap does not get copied. The clone ends up using the default compare function for all operations done on it.

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

4 participants