Hello, all
My generic sorting routine accepts type-specific op-
erations as the following procedural variables:
TYPE
SwapProc = PROCEDURE( ADDRESS, INTEGER, INTEGER );
CompProc = PROCEDURE( ADDRESS, INTEGER, INTEGER ): INTEGER;
So that for an array of INTEGERs the user may imple-
ments them as follows:
PROCEDURE Compare( data: ADDRESS; i, j: INTEGER ):INTEGER;
VAR p: PArray;
BEGIN
p := data; (* How to CAST immediately? *)
RETURN p^[i] - p^[j];
END Compare;
PROCEDURE Swap( data: ADDRESS; i, j: INTEGER );
VAR temp: INTEGER; p: PArray;
BEGIN
p := data;
temp := p^[i];
p^[i] := p^[j];
p^[j] := temp;
END Swap;
I have misgivings about this syntax for accessing an
array by its address: will not the dereference oper-
ator cause the copying of the entire array to the
stack every time, and if it will, what are the al-
ternatives?
My generic sorting routine accepts type-specific
operations as the following procedural vari-
ables:
TYPE
SwapProc = PROCEDURE( ADDRESS, INTEGER, INTEGER );
CompProc = PROCEDURE( ADDRESS, INTEGER, INTEGER ): INTEGER;
So that for an array of INTEGERs the user may
implements them as follows:
PROCEDURE Compare( data: ADDRESS; i, j: INTEGER ):INTEGER;
VAR p: PArray;
BEGIN
p := data; (* How to CAST immediately? *)
RETURN p^[i] - p^[j];
END Compare;
PROCEDURE Swap( data: ADDRESS; i, j: INTEGER );
VAR temp: INTEGER; p: PArray;
BEGIN
p := data;
temp := p^[i];
p^[i] := p^[j];
p^[j] := temp;
END Swap;
I have misgivings about this syntax for access-
ing an array by its address: will not the deref-
erence operator cause the copying of the en-
tire array to the stack every time, and if it
will, what are the alternatives?
The user will be calling the procs like this?
Compare(ADR(myarray), i, j);
or
Compare(ADR(myarray[0]), i, j);
The compiler only knows the "data:" parameter as
an ADDRESS, so it probably will copy only one word
(or not copy at all, since "data:" is never as-
signed).
Note though that this approach in general is ex-
tremly unsafe since ADDRESS could point to any-
thing.
It relies on the caller for all parameter checking
and even if passed only arrays of INTEGER all ar-
ray overflow checking is lost.
It is probably a better idea to provide separate
safe functions for arrays of various commonly used
types like INTEGER, CARDINAL, REAL, maybe along
with DEF file exports of useful ones like
String20 = ARRAY[0..21] OF CHAR;
String80 = ARRAY[0..81] OF CHAR; , etc
PROCEDURE SortInt(VAR Array:ARRAY OF INTEGER);
PROCEDURE SortStr20(VAR Array:ARRAY OF String20);
etc.
And, for projects, various specialized ones for
RECORDS and POINTERS unique to that project.
Hello, all
My generic sorting routine accepts type-specific op-
erations as the following procedural variables:
TYPE
SwapProc = PROCEDURE( ADDRESS, INTEGER, INTEGER );
CompProc = PROCEDURE( ADDRESS, INTEGER, INTEGER ): INTEGER;
So that for an array of INTEGERs the user may imple-
ments them as follows:
PROCEDURE Compare( data: ADDRESS; i, j: INTEGER ):INTEGER;
VAR p: PArray;
BEGIN
p := data; (* How to CAST immediately? *)
RETURN p^[i] - p^[j];
END Compare;
PROCEDURE Swap( data: ADDRESS; i, j: INTEGER );
VAR temp: INTEGER; p: PArray;
BEGIN
p := data;
temp := p^[i];
p^[i] := p^[j];
p^[j] := temp;
END Swap;
I have misgivings about this syntax for accessing an
array by its address: will not the dereference oper-
ator cause the copying of the entire array to the
stack every time, and if it will, what are the al-
ternatives?
My generic sorting routine accepts type-specific
operations as the following procedural vari-
ables:
TYPE
SwapProc = PROCEDURE( ADDRESS, INTEGER, INTEGER );
CompProc = PROCEDURE( ADDRESS, INTEGER, INTEGER ): INTEGER;
So that for an array of INTEGERs the user may
implements them as follows:
PROCEDURE Compare( data: ADDRESS; i, j: INTEGER ):INTEGER;
VAR p: PArray;
BEGIN
p := data; (* How to CAST immediately? *)
RETURN p^[i] - p^[j];
END Compare;
p := CAST(PArray, data); ought to do it
It isn't very generic if it assumes that you can
treat the contents of PArray as being identical to
a 4 byte INTEGER in all respects.
The above comparison would fail badly half the
time with CARDINAL.
Martin Brown to Anton Shepelev:
PROCEDURE Compare( data: ADDRESS; i, j: INTEGER ):INTEGER;
VAR p: PArray;
BEGIN
p := data; (* How to CAST immediately? *)
RETURN p^[i] - p^[j];
END Compare;
p := CAST(PArray, data); ought to do it
That line works as it stands. By "immediately" I
meant without the intermediate variable. Since
pointer CAST is only a pseudoinstruction telling the
compiler to treat an ADDRESS variable as a pointer
to a specific type, that variable should not be re-
quired. I wanted to write:
CAST( PArray, data )^[i] - CAST( PArray, data )^[j];
It is possible in Borland Pascal and C.
It isn't very generic if it assumes that you can
treat the contents of PArray as being identical to
a 4 byte INTEGER in all respects.
I think you misunderstood me because I forgot to in-
clude the declaration of PArray:
TArray = ARRAY[0..Size - 1] OF INTEGER;
PArray = POINTER TO TArray;
What do you have against declaring the routines as
taking an open array of TYPE ?
Martin Brown to Anton Shepelev:
What do you have against declaring the routines as
taking an open array of TYPE ?
My original design to write a generic sortine mod-
ule, which can be used any kind of array-like data.
Why choose to use a type-safe language if you want
to bypass its type-safety capabilities?
I think that sometimes we may tolerate this in ex-
change for the *huge* benifit of generic code.
Although it is the right solution for some low-leven
programs with a fixed structure, such as data com-
pressors, there are many applications where this ap-
proach will cause massive and painful copy-pasting.
As I mentioned, the creators of ISO Modula-2 recognized
the problem and came up with an extension document
for generic programming.
Don't know whether it has ever been implemented.
Don't know whether it has ever been implemented.
I don't have any Macs, but p1 Modula-2 claims this:
http://modula2.awiedemann.de/
Object Oriented Modula-2 compiler
for Apple Macintosh supporting Mach-O as command line tool or from Xcode . Implementing ISO standards IS 10514-1,2,3
Last update: July, 17th 2017
And apparently "ISO/IEC 10514-2:1998" is "Part 2: Generics Modula-2".
I've never found, within plain M2, a safe way to write a truly generic
sort routine. I guess that is why ISO M2 added a "Generic M2" extension document. But I'm not sure anyone has ever implemented "Generic M2".
I haven't tried it myself, but according to the documentation ADW (ex-Stonybrook) Modula-2 supports generic modules:
Martin Brown to Anton Shepelev:
What do you have against declaring the routines as
taking an open array of TYPE ?
My original design to write a generic sortine mod-
ule, which can be used any kind of array-like data.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 468 |
Nodes: | 16 (2 / 14) |
Uptime: | 19:39:44 |
Calls: | 9,440 |
Calls today: | 3 |
Files: | 13,594 |
Messages: | 6,109,830 |