[comp.lang.c] software ICs vs. libraries

peter@sugar.UUCP (10/29/87)

In article <1691@culdev1.UUCP>, drw@culdev1.UUCP (Dale Worley) writes:
> Another feature of "software ICs" comes from the fact that they are
> part of an object-oriented system.  One can actually write, say, a
> linked list manager that will work on objects of *any* type.  In most
> languages, this is impossible to do in a library routine.

You can do it in 'C'. In fact the standard 'C' library for the Amiga comes
with exactly this tool.

	Insert(ListHead, ListNode, PredNode)

	 - Insert a node in a list.

		AddHead(ListHead, ListNode)

		 - Insert at the head of the list:

			Insert(ListHead, ListNode, NULL);

		AddTail(ListHead, ListNode)

		 - Insert at the tail of the list:

			Insert(ListHead, ListNode, ListHead->lh_Tail);

	Remove(ListNode)

	 - Removes it from whatever List it's in.

		RemHead(ListHead)

		 - Removes and returns the head of a list.

			return_val = ListHead->lh_Head;
			Remove(return_val);
			return return_val;

		RemTail(ListHead)

		 - Removes and returns the tail of a list.
		   As Joe Isuzu says "well, you know..."

	Enqueue(ListHead, ListNode)

	 - Adds it on a priority basis. RemHead will return the highpri
	   node, RemTail the lowpri node. FIFO ordering if priorities match.

	FindName(ListHead, "Name")

	 - Finds the first node matching Name.

	FindName(ListNode, "Name")

	 - Finds the next node matching Name.

This deals with objects called "nodes". But, of course, you can pass any struct
to these functions so long as the first element of the struct is a list or a
node. This is one place 'C' outshines more modern and more heavily typed
languages like Modula.
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.

sommar@enea.UUCP (10/31/87)

peter@sugar.UUCP (Peter da Silva) writes:
>In article <1691@culdev1.UUCP>, drw@culdev1.UUCP (Dale Worley) writes:
>> Another feature of "software ICs" comes from the fact that they are
>> part of an object-oriented system.  One can actually write, say, a
>> linked list manager that will work on objects of *any* type.  In most
>> languages, this is impossible to do in a library routine.
>
>You can do it in 'C'. In fact the standard 'C' library for the Amiga comes
>with exactly this tool.
>   <description of interface deleted>
>This deals with objects called "nodes". But, of course, you can pass any 
>struct
>to these functions so long as the first element of the struct is a list or a
>node. This is one place 'C' outshines more modern and more heavily typed
>languages like Modula.

Of course you could do this in assembler too, n'est-ce pas? Just stack
an address to a block on the stack. The first word(s) in the block are 
the pointer to next block. And damn you if you forget them. The problem 
with C and assembler is that they leave the user to be reponsible for 
the correctness. If he forget the pointers in the struct, he may have 
a hard time to find out why the program crashes.
  Now, I don't speak Modula-2, so I can't speak for it. (But I can 
believe da Silva is right. My impression of Modula-2 has always been
that it is just another Pascal dialect :-)
  Finally some high-level languages in which you can write the 
linked-list manager *with* type checking are Simula and Ada. (But you 
don't write it in Simula, the manager is already there as a part of the 
language.)
-- 
Erland Sommarskog       
ENEA Data, Stockholm    
sommar@enea.UUCP        
                    It could have been worse; it could have been Pepsi.

drw@culdev1.UUCP (Dale Worley) (11/12/87)

I may be talking through my hat, but...

peter@sugar.UUCP (Peter da Silva) writes:
| In article <1691@culdev1.UUCP>, drw@culdev1.UUCP (Dale Worley) writes:
| > Another feature of "software ICs" comes from the fact that they are
| > part of an object-oriented system.  One can actually write, say, a
| > linked list manager that will work on objects of *any* type.  In most
| > languages, this is impossible to do in a library routine.
| 
| You can do it in 'C'. In fact the standard 'C' library for the Amiga comes
| with exactly this tool.
| 	Insert(ListHead, ListNode, PredNode)
|       [etc.]

Well, I'll bet not.  You can do a lot with macros, and if you're only
dealing with pointers to things, you can cast to (char *), but
consider a somewhat messier example where these two tricks don't work.
Say, a priority queue system, where the priorities aren't of a type
fixed in advance.

Of course you can *do* any of these in C by representing anything
messy as a (char *) pointing at it, but this is just implementing an
object-oriented system...

Dale
-- 
Dale Worley    Cullinet Software      ARPA: culdev1!drw@eddie.mit.edu
UUCP: ...!seismo!harvard!mit-eddie!culdev1!drw
If you get fed twice a day, how bad can life be?