English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Sparse structure
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-07-07 (19:27)
From: Chris King <colanderman@g...>
Subject: Sparse structure
I'm trying to create a sparse structure; i.e. a large structure which
doesn't allocate storage for "unused" fields.  Given a structure:

type foo = { a: int option; b: float option }

the best solution I can come up with, short of using Obj.magic, is to
use a hash table like this:

type foo_key = A_key | B_key
type foo_value = A_value of int | B_value of float
type sparse = (foo_key, foo_value) Hashtbl.t

which works, but the extra variant type required means more CPU time
and more keystrokes.  Is there a better solution than this?

The structure will have hundreds of fields, each with a different
type.  Most of the fields will be unused, but usage will be determined
at runtime so using objects is not an option.


- Chris K