Just for fun (took 10 minutes to write this, this e-mail took longer to write), here's a fun (albeit slightly inefficent) C++ way to do helps using the STL vector class and the STL sort method. The "Help" class needs the overloaded less-than operator because the STL sort method requires it (http://www.msoe.edu/eecs/ce/courseinfo/stl/sort.htm)

The Big O for the STL sort method is a very respectable O(n*log(n)) (http://www.sgi.com/tech/stl/sort.html). This is on par with quicksort, merge sort, Shell sort, heap sorts, etc. Bubbles and Insertion sorts are n^2, which is very inefficient.

---------------------------------------------------------------
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

Class Help
{
   public:
       int     level;
       string  keyword;
       string  text;
       bool operator< (const Help &lhs)
       {
           return (lhs.keyword < keyword);
       }
   private: //hide the below so the compiler doesn't generate them for us
       Help (const Help &);
Help &operator = (const Help &); };

Vector<Help> v_helps;

void load_helps()
{
   Help h;
   FILE *fp;

   fclose (fpReserve);
   if ((fp = fopen (HELPS_FILE, "r")) == NULL)
   {
writelogf ("Fatal error: cannot open help file %s for reading.", HELPS_FILE);
       exit (1);
   }

   for (;;)
   {

       h.level = fread_number (fp);
       h.keyword = fread_string (fp);
       if (keyword[0] == '$')
           break;

       h.text = fread_string (fp);
v_helps.push_back(h); top_help++;
   }
sort (v_helps.begin(), v_helps.end());
}
---------------------------------------------------------------



Dale Kingston wrote:

Whats there to this rewriting the entire loading process? Takes 5 seconds,
if your not willing to put out that much effort why are you trying to sort
in the first place?

It's simple:

int compare(const void *a, const void *b)
{
   struct help_data *hp1, *hp2;
   sk1 = (struct help_data *)a;
   sk2 = (struct help_data *)b;

        if (hp1->name == NULL )
                return 1;
        else if (hp2->name == NULL )
                return -1;
        if ( !str_cmp( hp1->name, "reserved" ) )
                return -1;
        else if ( !str_cmp( hp2->name, "reserved" ) )
                return 1;

        return strcmp(hp1->name, hp2->name);
   // not 100% sure on the order of those
}

code for loading:

for (num_helps = 1, pHelp = help_list; pHelp != NULL; num_helps++, pHelp =
pHelp->next);

Temp_help_array = malloc( sizeof(struct help_data) * (num_helps + 1) );

for (count = 0, pHelp = help_list; pHelp != NULL; count++, pHelp =
pHelp->next)
        temp_help_array[count] = pHelp;

temp_help_array[num_help+1] = NULL;

qsort( temp_help_array, num_helps, sizeof(struct help_data), compare );

help_list = temp_help_array[0];

for (count = 1, pHelp = help_list; count < num_helps+1; count++)
{
        pHelp->next = temp_help_array[count];
}

Now havn't tested any of this but in theiory it should work... Thats not
rewriting your loading, it's adding a small extra to it.

Another suggestion that I did to save me all this pain in the mud, was
converted my helpfiles to mysql, and the query line just requests it in
order. But that takes some rewriting :P



-----Original Message-----
From: Valnir [mailto:[EMAIL PROTECTED]
Sent: Thursday, August 12, 2004 8:36 AM
To: Dale Kingston
Subject: Re: Sorting.


Nope, cause I really didn't want to re-write the entire load process.. lazy
I guess. If anyone has any more input, it would be great too. Although I may
have to resort to this. :(

- Valnir

----- Original Message -----
From: "Dale Kingston" <[EMAIL PROTECTED]>
To: "Valnir" <[EMAIL PROTECTED]>; <[email protected]>
Sent: Thursday, August 12, 2004 10:27 AM
Subject: Re: Sorting.



I belive that cause helps are contained in a link list that qsort and that
doesn't work as well..

Have you tried loading your helps into a temporary array running it threw
the sorter and then remaking the list after it's sorted the area?


----- Original Message -----
From: "Valnir" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, August 12, 2004 8:05 AM
Subject: Sorting.



Ok, a while back I was asking how to sort my skills. Well, I figured

that

all out and it's working great. I got a new sorting issue though. I want
to
sort my HELPS. This is a whole different arena. Tried using the same
thought
process.. BOOM.

My helps are in the 'struct help_data' and 'struct help_data HELP_DATA'
format. Any help would be great. (and of course they load on boot into

the

'help_first' etc format.)

- Valnir


--
ROM mailing list
[email protected]
http://www.rom.org/cgi-bin/mailman/listinfo/rom





Reply via email to