Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] very simple specvecs
Home

[Freeciv-Dev] very simple specvecs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] very simple specvecs
From: Jason Dorje Short <jdorje@xxxxxxxxxxxx>
Date: Sat, 15 May 2004 02:13:50 -0400

It would be possible to make the specvec a simple array.

  struct {
    int a, b;
  } foo;

  #define SPECVEC_TAG foo
  #include "specvec.h"

  {
    foo_vector foo;

    foo_vector_init(&foo);
    foo_vector_reserve(&foo, 2);

    foo[0].a = 1;
    foo[0].b = 2;
    foo[1].a = 2;
    foo[1].b = 4;

    for (i = 0; i < foo_vector_size(&foo); i++) {
      printf("%d, %d\n", foo[i].a, foo[i].b);
    }

    foo_vector_free(&foo);
  }

However it would be _somewhat_ slower. Lookups would obviously be a fast O(1) operation. All other operations would still be O(1) but would be slightly slower.

Disregarding the potential ugliness of the implementation (no crazy memory tricks would be needed, don't worry Vasco), how useful would this be?

---

One implementation:

Obviously we now have a

  typedef struct foo *foo_vector;

As well we have a global hash of existing vector arrays (all vectors, not just foo_vectors). When the _init is done this vector is added to the hash, and the size and size_alloc are put into the value stored in the hash (a second malloc is needed here). Then these values are looked up in the hash (a slow O(1) operation) every time the vector's functions are accessed.

Perhaps this is too much of a reimplementation of malloc?

jason


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