High level C structures

Programming, for all ages and all languages.
Post Reply
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

High level C structures

Post by Alboin »

In a program I've been working on recently I've used a lot of linked lists, hashes, and stacks and such, but they are somewhat lacking uniform and niceness. So, I've been thinking of writing a library to handle linked lists, stacks, hashes, etc. In it, I plan to include a data description language like:

Code: Select all

@hash test_hash
	$meow = "cat"        #This is a hash item.
	$complex = %ll       #This too, although the item is a linked list.
		1 "what" 2 "koo" 1 2 3 4 5
		( 123 321 "Moo" ) #This is a tuple in a linked list in a hash. 
	!
!
(Ignore some of the oddities in the example. I've only worked on the concept thus far.)

So you could create complex data structures without having mass blocks of ugly code. (Kind of like Glade.) Also, it would be easy to store. (Note, I would plan to have a normal function-type interface as well.)

What do you think? Is it worth creating, or is there something already like it? (If so, what?)

Thanks!
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I can think of an implementation in Haskell that would give you an hash given any sort of data, but it would require overloading and data types, which is something C doesn't have. C++ will help you somewhat, but it will still be nasty due to the variant-type constructs used...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

It will be nasty internally, but it's C, after all. How can using void * be pretty?

Quite frankly, I would probably be just better off using another language, but I like C. It's so minimalistic. It's like tea almost. Hmm...tea.....

I'm not really concerned with the implementation at this point, but more on the theory itself.

Comments, obvious errors, suggestions?
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Post by binutils »

User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

I love Perl. I really do. It's probably my favorite scripting language.

The problem is that I'm working on a project that plans to be somewhat extensive, and I just can't see using Perl for large programs. It goes with my paranoia. (I also can't use other people's code.....Even if it's PD. It's strange. It just doesn't feel right. I can write code for others, but people contributing code to my project? Not so much........)
C8H10N4O2 | #446691 | Trust the nodes.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I have no idea what hash tables are(checksums?) but an easy linked list system would be great, also something better than a huge byte struct would be nice.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

If it could translate the input into C code that handled linked lists appropriately, I'd almost be tempted to pay for it. I can never get linked lists to work...
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

hckr83 wrote:I have no idea what hash tables are(checksums?)
They're also known as associative arrays. (ie. $hash["cat"] = "meow"; )

My current plan is to implement a subset of Lisp like list functions, and then build several other data types on top of those......then the actual translation from data strings to actual data structures is easy.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

Alboin wrote:
hckr83 wrote:I have no idea what hash tables are(checksums?)
They're also known as associative arrays. (ie. $hash["cat"] = "meow"; )
Ofcourse hash tables are sometimes a good data-structure even if you don't need to use strings as keys...

Hash tables work by "hashing" a key into an array index, such that you don't need an array as large as your potential key-space. A trivial, although not necessarily good hash function for integers would be (key modulo table-size). Their advantage over something like linked-list is that they have the same complexities as arrays, at least in theory: as long as you can hash each key into a different value, you can access each element directly. If several key's hash to the same value, you have a collision, which can be handled by storing a (hopefully short) linked list in each array cell (instead of a single value) and then searching this list, by having a secondary hash function to hash again until no collision occurs, or by simply allocating a bigger table as soon as there's a collision (or some combination of the previous).

Simply allocating a new table has the disadvantage that in the worst case you could end up with almost as big array as with no hashing at all, so usually that's not a good strategy. The multiple-hashing strategy is good, if the secondary hash and the table sizes are well chosen, and the table is grown before it becomes too full, but removal needs to be done lazily and searching for a non-existing key becomes a bit more expensive. Finally the linked list version has the nice property, that in theory you never need to grow the table, your performance just drops if the chains grow too long.

There are some variations, but the basic idea is a data-structure which behaves like an array, but allows sparse key-space.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

pcmattman wrote:If it could translate the input into C code that handled linked lists appropriately, I'd almost be tempted to pay for it. I can never get linked lists to work...
Hey, I just got linked lists working! Really well too! :lol:
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

pcmattman wrote:
pcmattman wrote:If it could translate the input into C code that handled linked lists appropriately, I'd almost be tempted to pay for it. I can never get linked lists to work...
Hey, I just got linked lists working! Really well too! :lol:
They're actually easy to implement. It's just that in the event that one has many ll's (linked lists, as I call them.) it gets ugly implementing them all with varying interfaces.

Also, when one has multiple lls in a ll with hashes in between, or something like that, it gets very ugly very quickly. :wink:
C8H10N4O2 | #446691 | Trust the nodes.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Well I just never knew I needed to use malloc() to create each entry (scope). Now I have a full ll system that you just have to rename the types and it'll work for anyting. Stuff like insert, del, add etc... was trivial once I knew how to get it to work.
Post Reply