Board index » delphi » Help needed for Delphi declaration of some Var...

Help needed for Delphi declaration of some Var...

Hi need some help to declaring the following :

type
  TCONTRIBTYPE = Record
    pixel : integer;
    weight: double;
  end;

  TCLIST = Record
    n : integer;
    p : (DON'T KNOW !!!!);   // it's a dynamic array of TContribType
  End;

Var
    contrib :(DON'T KNOW !!!!);   // it's a dynamic array of TCList

Please help...

Of if you know C++ Can you tell me what is this C code :

/* clamp the input to the specified range */
#define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)

Thanks alot....

 

Re:Help needed for Delphi declaration of some Var...


In article <356f73f1.42034...@news.total.net>, s...@total.net (The Boss)
writes:

Quote
>/* clamp the input to the specified range */
>#define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)

procedure ClampInt(var v : integer; l, h : integer);
begin
  if v < l then
    v := l
  else
    if v > h then
      v := h;
end;

 . . . and . . .

procedure ClampFloat(var v : double; l, h : double);
begin
  if v < l then
    v := l
  else
    if v > h then
      v := h;
end;

Alan Lloyd
alangll...@aol.com

Re:Help needed for Delphi declaration of some Var...


Quote
AlanGLLoyd wrote:

> In article <356f73f1.42034...@news.total.net>, s...@total.net (The Boss)
> writes:

> >/* clamp the input to the specified range */
> >#define CLAMP(v,l,h)    ((v)<(l) ? (l) : (v) > (h) ? (h) : v)

> procedure ClampInt(var v : integer; l, h : integer);
> begin
>   if v < l then
>     v := l
>   else
>     if v > h then
>       v := h;
> end;

>  . . . and . . .

> procedure ClampFloat(var v : double; l, h : double);
> begin
>   if v < l then
>     v := l
>   else
>     if v > h then
>       v := h;
> end;

> Alan Lloyd
> alangll...@aol.com

Just curious to know if there's a reason why this shouldn't be done as

 procedure ClampFloat(var v : double; l, h : double);
 begin
   if v < l then v := l
   if v > h then v := h;
 end;

Are two IF statements less efficient than a block IF construction?

Secondly, is there a disadvantage to using a function here instead of a
procedure?

Thanks

Re:Help needed for Delphi declaration of some Var...


In article <35702317....@fox.nstn.ca>, Gerry Forbes <gfor...@fox.nstn.ca>
writes:

Quote
>Just curious to know if there's a reason why this shouldn't be done as

> procedure ClampFloat(var v : double; l, h : double);
> begin
>   if v < l then v := l
>   if v > h then v := h;
> end;

I was mirroring the C code, I don't think it matters that much, because of
Delphi's typing you would have to have different procedures for integer and
floats.

Quote
>Are two IF statements less efficient than a block IF construction?

I don't know - shouldn't think so but you could measure it

Quote

>Secondly, is there a disadvantage to using a function here instead of a
>procedure?

Again I was mirroring the C code and I don't know. There may be some experts
(Dave Ullrich or Bob Lee etc) who would know, I guess that measurment would
show you.

Alan Lloyd
alangll...@aol.com

Re:Help needed for Delphi declaration of some Var...


In article <356f73f1.42034...@news.total.net>, The Boss <s...@total.net>
writes

Quote

>Hi need some help to declaring the following :

>type
>  TCONTRIBTYPE = Record
>    pixel : integer;
>    weight: double;
>  end;

>  TCLIST = Record
>    n : integer;
>    p : (DON'T KNOW !!!!);   // it's a dynamic array of TContribType
>  End;

>Var
>    contrib :(DON'T KNOW !!!!);   // it's a dynamic array of TCList

It appears that you haven't had a reply to the above. Maybe the
following will get you going (it must be compiled with range checking
disabled to avoid run-time exceptions due to the array[0..0]
constructs):

type
  TCONTRIBTYPE = Record
    pixel : integer;
    weight: double;
  end;

  { Array of TCONTRIBTYPE records }
  ACONTRIBTYPE = array[0..0] of TCONTRIBTYPE;

  { Pointer to array of TCONTRIBTYPE records }
  PACONTRIBTYPE = ^ACONTRIBTYPE;

  TCLIST = Record
    n : integer;
    p : PACONTRIBTYPE;

    { The number of members referenced by "p" (the size of the array) }
    SizeP: Integer;
  End;

  { Array of TCLIST records }
  ATCLIST = array[0..0] of TCLIST;

  { Pointer to array of TCLIST records }
  PATCLIST = ^ATCLIST;
Var
    contrib: PATCLIST;
    Sizecontrib: Integer;
    Count: Integer;

{ Allocate an array of two TCLIST records }
Sizecontrib := 2;
GetMem(contrib, SizeOf(TCLIST) * Sizecontrib);

{ Allocate an array of three TCONTRIBTYPE records for each
  individual TCLIST record }
for Count := 0 to Sizecontrib - 1 do
    with contrib^[Count] do
begin
    Sizep := 3;
    GetMem(p, SizeOf(TCONTRIBTYPE) * Sizep);
end; { end with }
.
.
.
Do something with the arrays ...
.
.
.
{ Free the array of TCONTRIBTYPE records for each
  individual TCLIST record }
for Count := 0 to Sizecontrib - 1 do
    with contrib^[Count] do
begin
    FreeMem(p, SizeOf(TCONTRIBTYPE) * Sizep);
end; { end with }

{ Free the array of TCLIST records }
FreeMem(contrib, SizeOf(TCLIST) * Sizecontrib);

The trick is to add two new type definitions for each record - one to
define an array of records, and the other to define a pointer to that
array. You can then declare a pointer variable and use GetMem to
allocate sufficient space for your array of records.

Note that I only use Delphi V1, so the syntax may differ for D2 or D3.

Hope the above helps.
--
Steve Turner
Leeds, England

Other Threads