help doing blitz style linked lists for C++ ?

Community Forums/General Help/help doing blitz style linked lists for C++ ?

Robert Cummings(Posted 2009) [#1]
Hi,

I'm having to code in C++ at the moment, and miss Blitz's way of doing linked lists, which I really enjoy for simplicity and speed.

I'm looking for a linked list class that behaves really similar to it, in C++ please. Any recommendations? I'm not worried if its Blitzmax or Blitz3D, either will do...

Had a lot of headaches with stl lists, I hate them.


N(Posted 2009) [#2]
Could you describe what exactly you want from Blitz's linked lists (assuming you mean BlitzMax) and what you don't like about std::list so I can attempt to figure out what works best?


Robert Cummings(Posted 2009) [#3]
Hiya,

Well I'd like to define a class of my own such as:

class Thing
{
int x,y,z,stuff;
}

Then go:

Thing myList = new LinkedList();
Thing* pThing;

And access it kinda like:

While (myList.next())
{
pThing.x = 10;
}

AddLast, AddFirst, Remove etc... not sure how to make linked lists in C++ so "transparent" but C++ being the monster it is, its surely possible in some form or other, perhaps via inheritence or preprocessors or such (not experienced enough to work out how).

Basically I'd like to emulate something similar to how Blitz does its linked lists (and fast in execution) in C++. I don't know if its technically possible though.

Thanks for taking interest, Nil :)


N(Posted 2009) [#4]
As far as std::list goes, this is more or less the standard way to use it as far as I'm concerned (also included is a hacky macro and some brief explanation of things as I see them). Basically, there's a base class and an inheriting class, and you have a list for that base class. Any inheriting class will fit into the base class, so it works out in the end.

http://gist.github.com/87633 (on gist for syntax highlighting and so I don't lose the code later when I delete it)

More or less, what you're asking to do can already be done with std::list, so you might just be overcomplicating how it's used.

As far as your example, it doesn't really make sense to me, since it doesn't really make sense even as far as pseudo-code is concerned ("Thing myList = new LinkedList();" in particular), hence the bit of code I wrote.

If you can point out exactly what you need to cut out of the existing method, that's easier, because otherwise you're pretty much going to end up rolling your own linked list code or illustrate what it is you're already doing with std::list that doesn't work for you. Unfortunately (or fortunately, depending on how you view things), that aside, there isn't a standard linked list, and you'd have to use another library that just happens to include one, like glib or wxWidgets, to get a linked list, and even then those are usually specialized for their needs.

So you have to be a bit more specific about what it is you want. If possible, try to write a code example exactly how you'd expect to be able to access a list, and then there's probably a way to use std::list or at least you'll know what you want to do when writing your own (which is easy enough already).


steve_ancell(Posted 2009) [#5]
Nilium (Posted 9 hours ago) #2

Could you describe what exactly you want from Blitz's linked lists (assuming you mean BlitzMax) and what you don't like about std::list so I can attempt to figure out what works best?




I think what Rob is asking, is how to do in c++, what Custom Types do in blitz.

I think he wants to know how to do the following code, but in C++. Am I correct Rob ?

Type Foo
     Field var
End Type

For i = 0 to 9
     f.Foo = new Foo
     v\var = i
Next

For f.Foo = Each Foo
     Print v\var
Next



simonh(Posted 2009) [#6]
stl lists aren't that bad, once you get used to them.

list<Egg*> eggs_list; // a list of pointer to egg, called eggs_list

Egg* egg=new Egg; // new egg

eggs_list.push_back(egg); // add egg to eggs list

Then to loop through them:

list<Egg*>::iterator egg_it; // egg list iterator

for(egg_it=egg_list.begin();egg_it!=egg_list.end();egg_it++){

    Egg* egg=*it; get egg from iterator

    egg->Update();

}



Robert Cummings(Posted 2009) [#7]
Yup :)

Although I think I should just keep trying the std::list which i hate to heaven cos I don't understand it.


*(Posted 2009) [#8]
Rob your in the same boat as me, talk about difficult to understand. If I get any info I will email ya with a noob2list style on things :)


N(Posted 2009) [#9]
Although I think I should just keep trying the std::list which i hate to heaven cos I don't understand it.
May as well ask questions about what you don't understand while you're talking about it. Might be able to answer any questions you have about it.


Robert Cummings(Posted 2009) [#10]
Simon, your explanation was pretty good actually, you explained it in way that didn't ASSUME I was a pro C++ programmer.

When googling, i got confused to high heaven with some of the ridiculously badly written examples because they don't speak to me in a simple, clear way. It takes an intelligent person to communicate in an effective way.

Guys, building on simonh's example, how do I remove an item, and is the speed ok on stl lists?
Also, does the item get added to the end of the list?

Can you teach me the the basic blitz commands in STL list please?

AddLast
AddFirst
Remove
Clear

etc :) thanks a lot!

Hey Edzup, didn't realise you were still alive and kicking :)


N(Posted 2009) [#11]
I can't really comment on speed, since it depends entirely on what you're doing. vectors are faster for iterating, lists are faster for inserting and deleting very frequently, deques are sort of a middle-ground I suppose in regards to the two. You have to pick one based on what you're doing, so you'll have to figure that part out on your own.

Using push_back, your item does get added to the end of the list.

Can you teach me the the basic blitz commands in STL list please?

AddLast
AddFirst
Remove
Clear

In that order:
std::list<int> aList;

// AddLast
aList.push_back(20);
// AddFirst
aList.push_front(80);
// Remove
aList.remove(20);
// Clear
aList.clear();

Also, for a decent reference (a bit advanced, but it explains a lot of stuff pretty well with examples), check http://www.cplusplus.com/reference/stl/list/
There's also reference material for a lot of other stuff related to the STL and such on there.


Robert Cummings(Posted 2009) [#12]
Thanks a bunch!

What are vectors then?


N(Posted 2009) [#13]
They're more or less dynamic arrays that automatically resize themselves when you add/remove items to them.


Robert Cummings(Posted 2009) [#14]
I guess vectors are best for game lists or short lists of stuff like bullets and so on? How do I use them?


N(Posted 2009) [#15]
Same way you use a list, just can't use push_front() and front(). They're not good for things like bullets since you'd constantly be adding and removing those from the vector, and it does not work well for that. A list would be best for that. A vector would be better for storing map objects or something that don't get added/removed constantly.