Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-05

linked list to criticize

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

spir ☣

Hello,

Finally did it!
Managed to write in Pascal a linked list type, with all common operations (put, change, remove, find & more). If anyone is nice enough to have a look, comment, and criticize: http://spir.wikidot.com/pascal-doubly-linked-list
(There's an extensive intro text, and a test suite with output.)

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Graeme Geldenhuys
to spir ☣: Finally did it! Managed to write in Pascal a linked list type, with all common operations…

2010/5/2 spir ☣ :

(There's an extensive intro text, and a test suite with output.)

Did you know FPC comes with a unit testing framework called fpcUnit? <fpc>/src/packages/fcl-fpcunit/src

Alternatively you can also use DUnit2 which I ported to Free Pascal a while back, and has a bit more features and fixes than fpcUnit. http://github.com/graemeg/dunit2
To get a copy of the source code repository:
git clone git://github.com/graemeg/dunit2.git
or click the "download source" link in top left corner.

Using a unit testing framework, it is usually much easier to structure your tests, or even write your tests.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Paul Nicholls
to spir ☣: Finally did it! Managed to write in Pascal a linked list type, with all common operations…

Hi Denis,
nice :)

I'm curious, why didn't you make it a class...wouldn't that have made it nicer/easier to use and program?

cheers,
Paul

Earlier message 22 lines
----- Original Message ----- From: "spir ☣" To: "FreePascal" Sent: Monday, May 03, 2010 4:25 AM Subject: [fpc-pascal] linked list to criticize Hello, Finally did it! Managed to write in Pascal a linked list type, with all common operations (put, change, remove, find & more). If anyone is nice enough to have a look, comment, and criticize: http://spir.wikidot.com/pascal-doubly-linked-list (There's an extensive intro text, and a test suite with output.) Denis ________________________________ vit esse estrany ☣ spir.wikidot.com

Reply by email

Juha Manninen
to Paul Nicholls: nice :) I'm curious, why didn't you make it a class...wouldn't that have made it…
I'm curious, why didn't you make it a class...wouldn't that have made it nicer/easier to use and program?

+1
It should be a class, yes. Now you have loads of global functions.

The integer data type doesn't make much sense but your comment says it will be changed to generics type. That sounds good.

Container classes are maybe the only place where generics really are useful. I often derive a list from TObjectList and redefine its "Items" property and "GetItem" + "SetItem" methods just to get a compiler type checking for correct type and to avoid type-casting later in code, and to benefit from Lazarus' hint and code completion features. Generics would do the same thing easier.

Once I had to maintain a Java program made without generics. The first thing I did, even before understanding the code much, was to define generics type for all containers by guessing it from type-casts. It helped me to understand the code better and to avoid errors later when changing it. I even spotter clear errors (wrong type-casts) in the original code. With Eclipse's hints and code completion it was a huge improvement.

Object Pascal generics containers + Lazarus would give the same benefits.

If someone is working on the generic containers this linked list effort should be added there (after making it a class and adding generics support). Actually it should be coordinated somehow because the containers could share some code. Delphi 2009 introduced generic containers which have some clever code. I think it needs effort to support native types like string and arrays. Supporting only class types (TObject->) would be easier.

Without generics support the linked list data type could be TObject which lets you forget the pointer syntax. Sometimes it causes overhead but it is very small compared to virtual machine overhead (Java, .NET etc.) while the syntax is about equally clear.

Regards,
Juha

Reply by email

Frank Church
to Felipe Monteiro de Carvalho: I think it would be good if you add a link to your package here:…

I don't wish to sound negative here, but I think something like doubly-linked lists is fundamental and doesn't need an implementation specially made for the FPC community, unless it needs to be adapted to the language implementation itself or the execution environment.

There must be dozens of implementations out there in textbooks and even in languages other than Pascal and it should be possible to check the code against that, just by inspection alone, not even by running tests.

If it has to fit into some preferred FPC libraries, or the FPC community's ways of working that's okay, but its correctness should never be in doubt before it goes in.

On 2 May 2010 21:20, Felipe Monteiro de Carvalho <

Show quoted text 14 lines
wrote: Hello, I think it would be good if you add a link to your package here: http://wiki.lazarus.freepascal.org/Components_and_Code_examples -- Felipe Monteiro de Carvalho _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

spir ☣
to Paul Nicholls: nice :) I'm curious, why didn't you make it a class...wouldn't that have made it…
Show quoted text 5 lines
Hi Denis, nice :) I'm curious, why didn't you make it a class...wouldn't that have made it nicer/easier to use and program?

Yes, for sure. Not beeing able to use the OO syntax is really a pain! (Even inside the LinkedList unit itself.) But it is all what we miss, I guess.

One point for me precisely was to write it from scratch, without any higher-level OO structure (object, class). Explore the techniques and issues of writing OO-like code in a language that would not come equipped with such a framework.

A side-question is also to avoid overloading code with a prebuilt OO layer, for efficiency (*). I take the opportunity to ask about the speed cost of using FreePascal's class-system vs plain Pascal. And the same about using objects vs plain records. I have no idea about this. Are there some data about this?

Denis

(*) Even more because in a while I intend to write a custom OO system (prototype-based). Building it on top FreePascal's class system makes a double OO layer, from client code(s point of view.

PS: I have slightly changed the online code:
* Replaced methods in correct order (for me), thank to one of you pointing me to "forward" :-)
* List_new now accepts directly an optional array of startup elements, not a pointer to it (I discovered arrays can have a 'nil' default.) ________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

spir ☣
to Felipe Monteiro de Carvalho: I think it would be good if you add a link to your package here:…
Show quoted text 6 lines
Hello, I think it would be good if you add a link to your package here: http://wiki.lazarus.freepascal.org/Components_and_Code_examples

Thanks, I'll add it there if some of you test it. (Self-testing one's own tool, from my experience, is not reliable for making it public.) The only builtin check is about index interval --but with Pascal type-checking I cannot find any other need. An issue is that it's not a generic container: items are typed at programming time (ints in original code for illustration). (But it's not such problematic for people using exclusively OO, I guess, since they can use a root class.)

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email