473,554 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL sorting comparison function

1 New Member
Hi,

I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer.

What I try to do is sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory and the only thing I actually have is a char* to the beginning of the index file.

It is possible to access the individual records of a file by (say) record.getPos() and record.getLen() methods that return the position and the length of the field in that record. A record is simply a row of the index file and is loosely defined in a separate class. I am also able to retrieve the length of a complete record (which is fixed for every index file generated) so I could define new records if I want to.

Now what I would like to do is sort this index file using STL sort() and a vector containing some representation of the records in the index file. Because this index file is very large, I think pointers should be used in one way or another. Does anyone have an idea of how I can write a comparison function? I've been trying to do this, but no success. The problem is that I want the comparison function to loop through a vector of sortfields. The sortfields define the columns to be sorted on.

My idea now is to break the complete index file that is in memory (just a char* to the beginning of the file is all we have, we have to jump through the index file manually by adding the length of the record every time). That means, breaking up the file in either separate char*, record* or records... BEFORE sorting it! I guess pointers would be fastest.

The index file looks approximately like this (fields can be empty, don't worry about the semantics of the file, there are more fields but I left them out for space reasons):
1; 55000; 55000; 193400080001; 193400080001000 1; ; ;1
1; 55100; 55000; 193400230001; 193400230001000 1; ; ;1
1; 55120; 55000; 193400440001; 193400440001000 1; ; ;1

But now: How do I define the comparison function for the STL sort. Suppose we break the file into record*'s we get something like sort(vector.beg in(), vector.end(), vector_comparis on)

With vector comparison being a sort of functor like

(pseudocode)
class vector_comparis on
{
bool operator() (record* A, record* B)
{
if (columnA of recordA < columnA of recordB)
return true;
if (columnB of recordA < columnB of recordB)
return true;
...
else
return false; // the records are equal
}
}
The biggest problem here is that this comparison function will need to access external resources such as a list iterator over the 'columns to be sorted on', which is only made at runtime, the vector of indexfile representation record*/char*/records, which is made at runtime... I cannot write this comparison function because it needs a lot of things which are created at runtime... These are not known at compile time... Maybe I understand something incorrectly about functors and I don't even know if this problem can be solved in this way. I am just trying, it is my last effort before I give up on this problem :)

I hope my question is clear, if you need more information please let me know! I'll check back tomorrow
Nov 13 '06 #1
0 2564

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3243
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
22
4124
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
20
4034
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
3
6392
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include <stdio.h> void main(void) { char lines; int count = 0, i;
25
2196
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort function that will merge like elements into a chain, so that they won't have to be recompared as many times? Thanks!
4
2163
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
4
1697
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons made by eahc sorting function. I have having trouble getting the number of comparisons to match. One comment was that "the counts are in the...
5
6674
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common requirement of programs. If you don't know the difference between a list and array don't worry about it. A list is just an array without a name. They...
1
7174
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
0
7506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7782
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6123
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5423
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5142
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1114
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
823
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.