473,749 Members | 2,665 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 2577

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

Similar topics

7
3266
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
4164
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
4072
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
6411
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
2225
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
2172
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
1709
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 wrongs place," but I have moved them all over the function and still they do not match.
5
6692
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 both can hold the same type of data: scalars or strings. I will use the terms "list" and "array" to...
1
7189
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 nothing new or useful contained in this article. Short Review In part one I showed you some...
0
8833
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.