PDA

View Full Version : Sorting textures names



Leyder Dylan
09-29-2001, 06:20 AM
Hi,

I've a problem.

In a file, I've :

Brown
Ceilin
Floor
Wood
Ceilin
Ceilin
Floor


I would like to sort the textures names for
only conserve the names who appear one time.

So, in my outpu file, I would like to have :

Brown
Ceilin
Floor
Wood

Michael Steinberg
09-29-2001, 08:20 AM
Read a line of the file.

Test whether it is in the linked list.
if not (
Store it in the linked list.
write it to the new file.
)
until there is no line left.

Evil-Dog
09-29-2001, 08:21 AM
Hi !

I suggest you use STL and keep your names in a list and before adding a new name test if it already exists in the list !
I do this with my TextureManager so it never created the same texture 2 times.
If you know know about STL just tell me ! http://www.opengl.org/discussion_boards/ubb/smile.gif

--------------------------
Evil-Dog
*Sleep is a waste of time*

Evil-Dog
09-29-2001, 08:23 AM
I mean...if you DONT know about STL tell me...hehe


----------------------------
Evil-Dog
*Sleep is a waste of time*

Leyder Dylan
09-29-2001, 08:53 AM
What is STL ?

http://www.opengl.org/discussion_boards/ubb/smile.gif

Zengar
09-29-2001, 10:22 AM
God, is it so difficult?

You can do it like that. It's one from tousand ways.
1. Sort all names into array(hope you can do it - it's damned easy).
2. Sorry, samle in paskal
for k:=0 to LastIndex-1 do
if CompareStrings(TArray[k], TArray[k+1])=0 then TArray[k]:='';
3. Write all array members that are not ''(empty), from 0 to LastIndex-1 into file.

Hop!

[This message has been edited by Zengar (edited 09-29-2001).]

Evil-Dog
09-29-2001, 10:44 AM
Zengar, you don't need to make Leyder Dylan
feel like he's stupid with your "God is it so difficult ?"
Everyone here is here to learn and help others.

Leyder Dylan STL is Standard Template Library I think
You can use it to create list very easily
like
#include <vector> //A vector in STL is like an array
using namespace std;

vector< string* > vecTextureName;
vecTexture.push_back( your texturename here );
and to test if the texture you have to go throught your vector with an iterator
look for some doc about STL you'll find it very useful ! http://www.opengl.org/discussion_boards/ubb/smile.gif I won't give you all the code like that hehhehe I can only show you the path, you're the one who must walk through it.
hehehee good luck man !

------------------------------
Evil-Dog
*Sleep is a waste of time*

MarsuGL
09-29-2001, 01:35 PM
#include <string.h>
Strcmp(..) function can be usefull.

Leyder Dylan
09-30-2001, 02:38 AM
Help me please http://www.opengl.org/discussion_boards/ubb/smile.gif

Zengar
09-30-2001, 05:01 AM
Sorry, I agree I was too rude http://www.opengl.org/discussion_boards/ubb/frown.gif
Have you tried my idea? It's interesting if it works(he-he)

Bob
09-30-2001, 05:40 AM
This is some code I put together in about 5 minutes. It uses STL to sort and remove uniqes entries.



#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class outputString
{
public:
void operator() (const string &amp;s) const {cout << s << endl;}
};

void main(void)
{
vector<string> listOfNames;

listOfNames.push_back("brown");
listOfNames.push_back("ceiling");
listOfNames.push_back("floor");
listOfNames.push_back("wood");
listOfNames.push_back("ceiling");
listOfNames.push_back("wood");
listOfNames.push_back("ceiling");
listOfNames.push_back("floor");
listOfNames.push_back("ceiling");
listOfNames.push_back("brown");
listOfNames.push_back("floor");
listOfNames.push_back("floor");
listOfNames.push_back("ceiling");

sort(listOfNames.begin(), listOfNames.end());

vector<string>::iterator listOfNamesEnd = unique(listOfNames.begin(), listOfNames.end());
for_each(listOfNames.begin(), listOfNamesEnd, outputString());
}

Hope you understand the code. I think it's pretty string forward and easy to understand, but that might have to do with the fact that I know a little STL http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Bob
09-30-2001, 05:45 AM
By the way, you can insert this line,



for_each(listOfNames.begin(), listOfNames.end(), outputString());

before and after sort() and unique() to see what happend to the list of entries.

Questions Burner
09-30-2001, 05:45 AM
Originally posted by Leyder Dylan:
Hi,

I've a problem.

In a file, I've :

Brown
Ceilin
Floor
Wood
Ceilin
Ceilin
Floor


I would like to sort the textures names for
only conserve the names who appear one time.

So, in my outpu file, I would like to have :

Brown
Ceilin
Floor
Wood




//using standard template library, STL for short.

#include <list>
#include <string>
using namespace std;

//declare linked list to hold strings
list<string> lst;

//then add everything to the list at the back
lst.push_back(texture_name)

//use this function
lst.unique();

//now there's no duplicates inside the list.

Leyder Dylan
09-30-2001, 08:50 AM
Hi,

It's work. Thanks a million

But, I've another problem. I want to write the string in a file but it's doesn't work.

No error at the compilation but error at the runtime.

I've only added a line in the class :

class outputString
{
public: void operator() (const string &s) const {cout << s << endl;
fprintf(Temp1,"%s\n", s);
}
};

And it's impossible to put the string in a array like this :

strcpy(Tab[0], s);


Convertion error.

DFrey
09-30-2001, 09:06 AM
Use s.c_str() instead of s.

Bob
09-30-2001, 11:15 PM
Yepp, DFrey got it. s is a string object, the STL-component string. If you want to convert this string to a char *, you must use s.c_str().

Questions Burner, your code will not remove all duplicate entries. The definition of unique() is that it will remove any equal entries next to each other. unique only guaratees that (*I) != (*I+1) for any value of I, where I is an iterator of the proper type. Meaning, if two entries are separated by another entry, none of them will be removed. You must sort the list before calling unique() to remove ALL duplicate entries.

Questions Burner
10-01-2001, 03:20 AM
Thanks for reminding me, I forgot to sort them.