Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] Re: Freeciv commit: vas: Added vector iterator macro, appe
Home

[Freeciv-Dev] Re: Freeciv commit: vas: Added vector iterator macro, appe

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv development list <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: Freeciv commit: vas: Added vector iterator macro, append and get the...
From: Raimar Falke <i-freeciv-lists@xxxxxxxxxxxxx>
Date: Wed, 19 May 2004 23:16:19 +0200

On Wed, May 19, 2004 at 01:20:44PM -0700, Raimar Falke wrote:
> -  if (index < 0 || index >= tthis->size) {
> -    assert(index >= 0 && index < tthis->size);
> -    return NULL;
> +  if (index == -1) {
> +    if (tthis->size > 0) {
> +      return tthis->p + tthis->size - 1;
> +    } else {
> +      return NULL;
> +    }
>    } else {
> -    return tthis->p + index;
> +    if (index < 0 || index >= tthis->size) {
> +      return NULL;
> +    } else {
> +      return tthis->p + index;
> +    }

What about this:

  if (index == -1 && tthis->size > 0) {
    return tthis->p + tthis->size - 1;
  } else if (index >= 0 && index < tthis->size) {
    return tthis->p + index;
  } else {
    return NULL;
  }

Less redundant, more compact and with a clean else case.

> +static inline void SPECVEC_FOO(_vector_append) (SPECVEC_VECTOR *tthis,
> +                                             SPECVEC_TYPE *pfoo)
> +{
> +  const size_t last = tthis->size;
> +
> +  SPECVEC_FOO(_vector_reserve) (tthis, last + 1);
> +  tthis->p[last] = *pfoo;
> +}

I think the variable is misnamed here. Because of the reserve call the
meaning changes from "size" to "index of last element".

What about the classical:

{
  SPECVEC_FOO(_vector_reserve) (tthis, tthis->size + 1);
  tthis->p[tthis->size - 1] = *pfoo;
}

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "Despite all the medical advances of the 20th century, the mortality 
  rate remains unchanged at 1 death per person."


[Prev in Thread] Current Thread [Next in Thread]