Wow, this reply was really fast
Thank you, this is what I was looking for.
Best regards
Christian[/quote]
You do not need any hacks with Long64_t, use container (array) of std::pair<int_int> + std::sort (replace _ with , - there are problems with this forum and formatting) instead.
It is also attached as macro together with a sample input table.
Sorrowy, CINT cannot handle it, so you have to compile it with ACLiC.
I am wondering whether code using stl methods pair and sort would be as easy.
Currently, I do not see how TTableSorter would be able to do double sorting.
[quote]1. Why does it compile w/o problems?
2. fN cannot be a constant, since I do not know it in advance.
Is it possible to do “p_t *arr = new p_t[fN]” and “arr[idx]->first”?
Best regards
Christian[/quote]
It’s a wrong question You should specify, which compiler (Ok, I guess, it’s g++). In C99 (not in C89) you can do such things:
void fun(int j)
{
int i = 10;
int arr[i];
int arr[j];
}
g++ supports variable-length arrays (C99). So, you have a mixture of C and C++ code, correct C++ compiler cannot compile it (it’s an ill-formed C++ program), C compiler cannot compile it (it’s not a C program), g++ can.
For example, MSVC 8’ compiler:
"ComeauTest.c", line 4: error: expression must have a constant value
int arr[i];
g++ -pedantic :
main.cpp:4: warning: ISO C++ forbids variable-size array `arr'
Use std::vector
Yes, it’s possible to create dynamic array of pairs (and use indicies and array subscript). But… you 'd better use std::vector Good modern C++ program should avoid where possible raw pointers, there are a lot of good smart-pointers, std::auto_ptr (very specific), there is std::vector, std::valarray etc.
C++ is a language, which supports exception handling. So, to avoid many problems and reduce complexity of code, C++ programmer must use RAII idiom.
However, instead of std::vector I would prefer to use the ROOT class TArray.
Most stl classes have ROOT equivalents, and I prefer to use these, but this
is another story
Since ROOT does not use exception classes, I had decided not to use them either.
This is ok, however, I prefer to use the ROOT classes whenever there are any.
This is one of the great things of real frameworks like MacApp and ROOT:
They offer you all classes that you need w/o leaving the framework!
BTW, there are no “standard classes” in C++! In contrast to Java, C++ was
designed as language only!
If you look at the ROOT source code then you will see, that ROOT has no
exception classes and ROOT code does not throw exceptions (besides the
standard execeptions mentioned by you).
This makes your program less-portable, and makes you vendor-dependent.
You are happy with your framework and do not see better alternative solutions (for example, if I need GUI, but I do not need any ROOT specific - I will use Qt).
The “real framework like MapApp” is not officially supported and developed more. And if you have a lot of code, which was MacApp-oriented == you have a pile of officially obsolete code.
Yes, I know, ROOT is not MacApp, it’s actively developed - I only want to say: the part of a program, which can be framework independant, MUST be framework independent. So, you can extract the heart of your programm very simply, without re-writing (re-debugging).
BTW, there are no "standard classes" in C++! In contrast to Java, C++ was
designed as language only!
Completely wrong statement. C++ standard library - is a set of standard C++ classes (and non-member functions). std::vector/list/map/set etc. - they are all standard classes. Notion of “standard class” is not limited by GUI, or socket, or file.
The C++ standard library is a very powerful tool (thanks to genius Alex Stepanov and Co). It makes C++ a really good worthwhile modern programming language. Without it, without templates/exceptions etc. you are back to 90s, you only have a language subset - but I guess, you are not an embedded systems programmer?
In your code:
do you use keyword new?
do you use new(std::nothrow) form?
If your answers are : 1. yes 2. no - your code can throw. I did not want to say “USE EXCEPTIONS!!!” in my previous post, I want to say “code should be exception safe, if it’s possible”. (BTW TArray or TFile or TH1F etc. stack objects examples of RAII).
I think, I should stop here Excuse me for this off-topic.
BTW, one of the reasons for Rene to develop ROOT was to ensure that the
framework will still be available in 20-30 years.
As you mentioned this is no longer the case with MacApp and may not be
the case with QT (although both MacApp and QT have the advantage that
the source code is available, which would not be the case for commercial
frameworks)!
[quote=“cstrato”]Currently, I do not see how TTableSorter would be able to do double sorting.[/quote]One can define this via vclass ctor root.cern.ch/root/htmldoc/TTable … ableSorter
TTableSorter ctor sorts the input table according the function “search”
- search - the function to compare the "key" and the table rows during sorting
typedef Int_t (*SEARCHMETHOD) (const void *, const void **);
- compare - the function to compare two table rows during searching
typedef Int_t (*COMPAREMETHOD)(const void **, const void **);
- firstRow - the first table row to sort from (=0 by default)
- numberRows - the number of the table rows to sort (=0 by default)
= 0 means sort all rows from the "firstRow" by the end of table
TTableSorter ctor sorts the input table according the function “search”
- search - the function to compare the "key" and the table rows during sorting
typedef Int_t (*SEARCHMETHOD) (const void *, const void **);
- compare - the function to compare two table rows during searching
typedef Int_t (*COMPAREMETHOD)(const void **, const void **);
- firstRow - the first table row to sort from (=0 by default)
- numberRows - the number of the table rows to sort (=0 by default)
= 0 means sort all rows from the "firstRow" by the end of table
[/code]The UNIX man page for “qsort” and “bsearch” subroutines may ngiev some extra information too.
[quote=“cstrato”]Thank you, however, I do not have time to figure that out.[/quote]Yes, people always are busy writing and debugging there own version of the code for weeks because they have no 5 min to read the man page [quote=“cstrato”] Furthermore, it seems that I have to store my data in TTable in order to use TTableSorter.[/quote]I do not know what you meant speaking “store”. TTable class is a wrapper around of the regular C array of the regular C structure. The TTable object can (if one wants) own that array too (i.e. the TTable object can create /delete array optionally). Check the various TTable ctors to see that (yes, it might have taken you another 3 min ). TTable can “adopt” the external array via Adopt method too.
One more commnet about TTableSorter . Pay your attention that one can create unlimited number of the different TTableSorter’s around of one and the same array of C-structure. TTableSorter doesn’t change the array itself. It provides the iterator for C-array using the user provided “search/compare” methods.