nsTArray should have a Sort() method that takes a function pointer.
Categories
(Core :: XPCOM, enhancement, P5)
Tracking
()
People
(Reporter: tbsaunde, Unassigned)
References
(Blocks 1 open bug)
Details
(Whiteboard: [lang=c++])
Attachments
(2 files, 4 obsolete files)
|
4.83 KB,
patch
|
Details | Diff | Splinter Review | |
|
2.67 KB,
patch
|
Details | Diff | Splinter Review |
Comment 1•13 years ago
|
||
| Reporter | ||
Comment 2•13 years ago
|
||
Comment 3•13 years ago
|
||
| Reporter | ||
Comment 4•13 years ago
|
||
Comment 5•13 years ago
|
||
| Reporter | ||
Comment 6•13 years ago
|
||
Comment 7•13 years ago
|
||
Comment 8•13 years ago
|
||
Updated•13 years ago
|
| Reporter | ||
Comment 9•13 years ago
|
||
Comment 10•13 years ago
|
||
Comment 11•13 years ago
|
||
Comment 12•13 years ago
|
||
Comment 13•13 years ago
|
||
Comment 14•13 years ago
|
||
Comment 15•13 years ago
|
||
Comment 16•13 years ago
|
||
Comment 17•13 years ago
|
||
| Reporter | ||
Comment 18•13 years ago
|
||
Comment 19•13 years ago
|
||
| Reporter | ||
Comment 21•12 years ago
|
||
| Reporter | ||
Comment 22•12 years ago
|
||
| Assignee | ||
Updated•12 years ago
|
Comment 23•9 years ago
|
||
| Reporter | ||
Comment 24•9 years ago
|
||
Comment 25•9 years ago
|
||
Comment 26•9 years ago
|
||
Comment 27•9 years ago
|
||
Comment 28•9 years ago
|
||
Comment 29•7 years ago
|
||
Hi, I would like to work on this bug. Can anyone assign this to me?
Comment 30•7 years ago
|
||
From the previous code and comments this is what I have understood.
- The function mustake a function pointer that does the sorting.
- The elements to be sorted must be of the type elem_type
- Should internally use NS_QuickSort
Here are my doubts.
-
When NS_QuickSort is doing exactly the same why do we need another function? What are we supposed to do here that NS_QuickSort does not?
-
elem_type seems to be typedef of class E. I could find class E anywhere.
-
Much of the code seems to have moved. I couldnt find nsTArray.h in glue/ . Where are we supposed to code this?
Comment 31•7 years ago
|
||
(In reply to Srujana Peddinti from comment #30)
- When NS_QuickSort is doing exactly the same why do we need another function? What are we supposed to do here that NS_QuickSort does not?
NS_QuickSort requires extracting various bits from nsTArray; it should be more convenient to just ask the array to sort itself, and let the array figure out what needs to be passed to NS_QuickSort.
- elem_type seems to be typedef of class E. I could find class E anywhere.
E is one of the template parameters of nsTArray_Impl.
- Much of the code seems to have moved. I couldnt find nsTArray.h in glue/ . Where are we supposed to code this?
Ideally you should see where nsTArray.h is as a result of answer to question 2. :)
Comment 32•7 years ago
|
||
Yes, I found nsTArray.h but I wasnt sure if this is the file I have to code in because its folder changed.
So as I have understood, I need to code it in nsTArray.h but in ds/ directory using NS_QuickSort.
And write the necessary tests.
I will be right on it. Is there anything else I have to know?
Comment 33•7 years ago
|
||
-
In nsQuickSort.cpp the function NS_QuickSort() is taking void* and (unsigned)int as first 2 parameters. In the file there is no other declaration of NS_QuickSort(). But in nsTArray.h file the functions Elements(), Length() are passed. How come this works? I ve searched ds/ folder. Couldnt find an alternate declaration.
-
Also to the NS_QuickSort() function what is the last argument we pass? In the file nsQuickSort.h it was mentioned that it is extra data to pass to comparison function. But I couldn't understand it. Isnt the first argument ie array of elem_type* enough for comparison function to sort the array?
Comment 34•7 years ago
|
||
(In reply to Srujana Peddinti from comment #33)
- In nsQuickSort.cpp the function NS_QuickSort() is taking void* and (unsigned)int as first 2 parameters. In the file there is no other declaration of NS_QuickSort(). But in nsTArray.h file the functions Elements(), Length() are passed. How come this works? I ve searched ds/ folder. Couldnt find an alternate declaration.
Pointers automatically convert to void*. The result of Length() implicitly converts to unsigned int (and fortunately always fits into unsigned int due to the way nsTArray works).
- Also to the NS_QuickSort() function what is the last argument we pass? In the file nsQuickSort.h it was mentioned that it is extra data to pass to comparison function. But I couldn't understand it. Isnt the first argument ie array of elem_type* enough for comparison function to sort the array?
Not if the elements require external data to be sorted. Maybe the elements of the array are indices into some other data structure, and you want to sort the indices in a particular order. See e.g. the use of NS_QuickSort in dom/bindings/BindingUtils.cpp.
Comment 35•7 years ago
|
||
I'm pretty sure we already have a sort function in nsTArray that does what this wants.
Comment 36•7 years ago
|
||
(In reply to Nathan Froyd [:froydnj] from comment #34)
(In reply to Srujana Peddinti from comment #33)
Pointers automatically convert to
void*. The result ofLength()implicitly converts tounsigned int(and fortunately always fits intounsigned intdue to the waynsTArrayworks).
How come functions are passed without any arguments? Where are we telling that the array is the argument for Length() function?
Not if the elements require external data to be sorted. Maybe the elements of the array are indices into some other data structure, and you want to sort the indices in a particular order. See e.g. the use of
NS_QuickSortin dom/bindings/BindingUtils.cpp.
Yes, this makes sense.
Comment 37•7 years ago
|
||
I have read the documentations but I did not understand how to run the gtests on a subset of tests.
I have tried
-
changing GTEST_FILTER env. variable
-
mach gtest Testname
-
Using --gtest_filter= 'Testname' option.
These are the docs I read from.
https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/GTest
https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#running-test-programs-advanced-options
Can anyone help me out in running gtests?
Comment 38•7 years ago
|
||
(In reply to Srujana Peddinti from comment #37)
I have read the documentations but I did not understand how to run the gtests on a subset of tests.
I have tried
changing GTEST_FILTER env. variable
mach gtest Testname
Using --gtest_filter= 'Testname' option.
These are the docs I read from.
https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/GTest
https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#running-test-programs-advanced-optionsCan anyone help me out in running gtests?
The format for the filter is <suite_name>.<test_name>, so for example a command for testing the nsTArray sort function is:
./mach gtest TArray.test_comparator_objects
to run all nsTArray tests you can do:
./mach gtest TArray.*
Comment 39•7 years ago
|
||
The compare function in the quicksort function takes void pointers as arguments. But in one of the comments, it was decided to use elem_type pointers. So should I change the compare function declaration in quicksort from void* to elem_type*?
Comment 40•7 years ago
|
||
(In reply to Eric Rahm [:erahm] from comment #35)
I'm pretty sure we already have a sort function in nsTArray that does what this wants.
This is supposed to be more generic -- that method works on a type with a int Compare(const elem_type&, const elem_type&) method but this should use anything with the right signature as a comparator like e.g. a lambda.
Comment 41•7 years ago
|
||
(In reply to David Parks (dparks) [:handyman] from comment #40)
(In reply to Eric Rahm [:erahm] from comment #35)
I'm pretty sure we already have a sort function in nsTArray that does what this wants.
This is supposed to be more generic -- that method works on a type with a int Compare(const elem_type&, const elem_type&) method but this should use anything with the right signature as a comparator like e.g. a lambda.
nsTArray<int> foo({3, 2, 10});
foo.Sort([](int a, int b) { return a - b; })
works fine.
Comment 42•7 years ago
|
||
So is this bug still valid?
Comment 43•7 years ago
|
||
(In reply to Masatoshi Kimura [:emk] from comment #42)
So is this bug still valid?
If I understand correctly, no, but it's hard for me to figure out way the original purpose was. At this point we could definitely use better docs for nsTArray::Sort and converting a bunch of one-off comparators to lambdas would be useful (I think that was the original goal).
Comment 44•7 years ago
|
||
(In reply to Eric Rahm [:erahm] from comment #41)
nsTArray<int> foo({3, 2, 10}); foo.Sort([](int a, int b) { return a - b; })works fine.
Ah, I missed that there is an implicitly constructable CompareWrapper class.
Comment 45•4 years ago
|
||
The bug assignee didn't login in Bugzilla in the last 7 months, so the assignee is being reset.
Comment 46•4 years ago
|
||
Closing this bug because as noted in comment 44 we can already sort with a lambda in the Sort function call.
Description
•