unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* struct-copy func
@ 2007-08-17  0:46 Kevin Ryde
  2007-08-20  8:35 ` Ludovic Courtès
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin Ryde @ 2007-08-17  0:46 UTC (permalink / raw)
  To: guile-devel

This is an idea I had for copying structures, mainly to make a
`record-copy' function (just an alias of struct-copy).  I've found it
pretty useful.

You can do a record copy with `record-constructor' and a map over
`record-accessor' for each field, but of course a block copy is heaps
more efficient.


SCM_DEFINE_PUBLIC (struct_copy, "struct-copy", 1, 0, 0,
            (SCM st),
	    "Return a copy of structure @var{st}.  This is a shallow copy,\n"
	    "there's no recursive copying of the objects in the fields.")
#define FUNC_NAME s_struct_copy
{
  scm_t_bits *vtable_data, *st_data, *new_st_data;
  size_t n_words;

  SCM_VALIDATE_STRUCT (SCM_ARG1, st);

  vtable_data = SCM_STRUCT_VTABLE_DATA (st);

  /* Only standard structs handled here, not goops entity or light forms.
     Believe that's enough for ordinary public uses of structures as created
     from `make-struct', `make-vtable', `make-vtable-vtable'.  */
  if (vtable_data[scm_struct_i_flags]
      & (SCM_STRUCTF_ENTITY | SCM_STRUCTF_LIGHT))
    SCM_WRONG_TYPE_ARG (SCM_ARG1, st);

  st_data = SCM_STRUCT_DATA (st);
  n_words = st_data[scm_struct_i_n_words];
  new_st_data = scm_alloc_struct (n_words,
                                  scm_struct_n_extra_words,
                                  "struct");

  memcpy (new_st_data, st_data, n_words * sizeof(scm_t_bits));
  scm_remember_upto_here_1 (st);

  return scm_double_cell ((scm_t_bits) vtable_data + scm_tc3_struct,
                          (scm_t_bits) new_st_data, 0, 0);
}
#undef FUNC_NAME


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-21  7:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-17  0:46 struct-copy func Kevin Ryde
2007-08-20  8:35 ` Ludovic Courtès
2007-08-20 23:27   ` Kevin Ryde
2007-08-21  7:49     ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).