Article View: comp.lang.modula2
Article #1079Re: Implementing Abstract Lists
From: Juergen Boerstler
Date: Wed, 15 Aug 1990 13:11
Date: Wed, 15 Aug 1990 13:11
85 lines
3889 bytes
3889 bytes
Peter.M..Perchansky@f101.n273.z1.fidonet.org (Peter M. Perchansky) writes:
>Hello:
> In a few days I will post my generic dequeue module to show how Modula-2 can handle generic lists.
Thank you. I got the deques-module last week. In reading the code I observed
that you too did not solve the byte/word-copying-problem in Modula-2. As most
other programmers do (including myself) some non Modula-2 call is used to do
this work (here: '_Move').
Nevertheless you can do something similar to:
PROCEDURE ByteCopy( Destination, Source: ADDRESS; Length: CARDINAL);
(* copies 'Length' bytes from 'Source^' to 'Destination^', using *)
(* the possibitlity to copy word per word, which is "standard"- *)
(* Modula-2 (i.e. by Wirth in his 3rd Ed. of the language report)*)
(* PreCondition: Your must know how much bytes per word you have *)
(* (maybe your 'SYSTEM' module supports something *)
(* like 'BYTESPERWORD' which adds to portability) *)
(* CAUTION: Operation temporarily violates storage contents *)
VAR
Dest, Src, TmpAddress: ADDRESS;
TmpWord: WORD;
Count, Rest: CARDINAL;
BEGIN
Dest := Destination;
Src := Source;
Rest := Length MOD BYTESPERWORD;
(* for 'Rest <> 0' or 'Length < BYTESPERWORD' some special cases *)
(* are needed *)
IF Length > BYTESPERWORD THEN
FOR Count := 1 TO Length DIV BYTESPERWORD DO
(* copy whole words *)
Dest^ := Src^;
INC( Dest, BYTESPERWORD); INC( Src, BYTESPERWORD)
END;
(* special case for additional bytes: *)
IF Rest > 0 THEN
(* readjust start address for last word copy *)
DEC( Dest, BYTESPERWORD - Rest); DEC( Src, BYTESPERWORD - Rest);
(* copy last word *)
Dest^ := Src^
END
ELSE
(* special case for 'Length < BYTESPERWORD': *)
TmpAddress := Dest + Rest;
TmpWord := TmpAddress^;
(* write whole word, and maybe overwrite some bytes *)
Dest^ := Src^;
(* restore the overwrited bytes *)
TmpAddress^ := TmpWord
END
END ByteCopy;
(* this version is not checked very well, because I couldn't copy *)
(* the source directly, so I may have introduced some errors *)
In addition there is a problem in comparing the contents of two list elements.
In general it is not very time efficient to perform a byte per byte test for
("user") types like:
UserTypeForListElements = RECORD
Key: SomeType;
(*
a lot of additional fields
*)
END;
where the relations '=', '<', '>', ... can be computed very efficiently by
user supported procedures.
Therefore generic ADTs should allow such procedures as parameters.
In addition I recommend the reading of:
C. Lins: The Modula-2 Software Component Library, Springer-Verlag.
SIGPlan Notices
Journal of Ada, Pascal, & Modula-2
jubo
**************************************************************************
* Juergen Boerstler * e-mail: jubo@rwthi3.uucp *
* Lehrstuhl fuer Informatik III * phone: +49/ 241/ 80-7216 *
* Ahornstrasse 55 * *
* D-5100 Aachen * *
* West Germany * *
**************************************************************************
Technical Details
Message-ID:
<3328@rwthinf.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!mcsun!unido!rwthinf!juboReferences:
<402.26C179B6@puddle.fidonet.org>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!mcsun!unido!rwthinf!jubo From: jubo@rwthinf.UUCP (Juergen Boerstler) Newsgroups: comp.lang.modula2 Subject: Re: Implementing Abstract Lists Message-ID: <3328@rwthinf.UUCP> Date: 15 Aug 90 13:11:21 GMT References: <402.26C179B6@puddle.fidonet.org> Organization: RBI - RWTH Aachen Lines: 85