Patchwork [02,of,10] py3: replace None with -1 to sort an integer array

login
register
mail settings
Submitter Pulkit Goyal
Date May 31, 2017, 9:46 p.m.
Message ID <93f5b615c900ec9b74b1.1496267218@workspace>
Download mbox | patch
Permalink /patch/21097/
State Accepted
Headers show

Comments

Pulkit Goyal - May 31, 2017, 9:46 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1496254732 -19800
#      Wed May 31 23:48:52 2017 +0530
# Node ID 93f5b615c900ec9b74b141aaeb162041f59d5737
# Parent  111485d5d5a7d2c06a7010b53e111235d5d215fd
py3: replace None with -1 to sort an integer array

In Python 2:

>>> ls = [4, 2, None]
>>> sorted(ls)
[None, 2, 4]

In Python 3:

>>> ls = [4, 2, None]
>>> sorted(ls)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: NoneType() < int()

Therefore we replaced None with -1, and the safe part is that, None and -1 are
only the keys which are used for sorting so we don't need to convert the -1's
back to None.
Yuya Nishihara - June 1, 2017, 3:02 p.m.
On Thu, 01 Jun 2017 03:16:58 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1496254732 -19800
> #      Wed May 31 23:48:52 2017 +0530
> # Node ID 93f5b615c900ec9b74b141aaeb162041f59d5737
> # Parent  111485d5d5a7d2c06a7010b53e111235d5d215fd
> py3: replace None with -1 to sort an integer array
> 
> In Python 2:
> 
> >>> ls = [4, 2, None]
> >>> sorted(ls)
> [None, 2, 4]
> 
> In Python 3:
> 
> >>> ls = [4, 2, None]
> >>> sorted(ls)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unorderable types: NoneType() < int()
> 
> Therefore we replaced None with -1, and the safe part is that, None and -1 are
> only the keys which are used for sorting so we don't need to convert the -1's
> back to None.
> 
> diff --git a/mercurial/merge.py b/mercurial/merge.py
> --- a/mercurial/merge.py
> +++ b/mercurial/merge.py
> @@ -801,7 +801,7 @@
>  
>      # manifests fetched in order are going to be faster, so prime the caches
>      [x.manifest() for x in
> -     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
> +     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev() or -1)]

Suppose the order doesn't matter, we could use scmutil.intrev() until
wctx.rev() can be changed to INT_MAX.
Augie Fackler - June 1, 2017, 4:15 p.m.
On Thu, Jun 1, 2017 at 11:02 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> On Thu, 01 Jun 2017 03:16:58 +0530, Pulkit Goyal wrote:
>> # HG changeset patch
>> # User Pulkit Goyal <7895pulkit@gmail.com>
>> # Date 1496254732 -19800
>> #      Wed May 31 23:48:52 2017 +0530
>> # Node ID 93f5b615c900ec9b74b141aaeb162041f59d5737
>> # Parent  111485d5d5a7d2c06a7010b53e111235d5d215fd
>> py3: replace None with -1 to sort an integer array
>>
>> In Python 2:
>>
>> >>> ls = [4, 2, None]
>> >>> sorted(ls)
>> [None, 2, 4]
>>
>> In Python 3:
>>
>> >>> ls = [4, 2, None]
>> >>> sorted(ls)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> TypeError: unorderable types: NoneType() < int()
>>
>> Therefore we replaced None with -1, and the safe part is that, None and -1 are
>> only the keys which are used for sorting so we don't need to convert the -1's
>> back to None.
>>
>> diff --git a/mercurial/merge.py b/mercurial/merge.py
>> --- a/mercurial/merge.py
>> +++ b/mercurial/merge.py
>> @@ -801,7 +801,7 @@
>>
>>      # manifests fetched in order are going to be faster, so prime the caches
>>      [x.manifest() for x in
>> -     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
>> +     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev() or -1)]
>
> Suppose the order doesn't matter, we could use scmutil.intrev() until
> wctx.rev() can be changed to INT_MAX.

That's not a bad thought, though the order here only really matters
for revisions in history so that we load manifests in an efficient
order.

> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -801,7 +801,7 @@ 
 
     # manifests fetched in order are going to be faster, so prime the caches
     [x.manifest() for x in
-     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev())]
+     sorted(wctx.parents() + [p2, pa], key=lambda x: x.rev() or -1)]
 
     if followcopies:
         ret = copies.mergecopies(repo, wctx, p2, pa)