mapping.c

MudOSa4DGD/mudlib/dgd/lib/sys/efun/mapping.c · 1.4 KB

From the archive MudOSa4DGD-30.06.98.tar.gz

Download the archive   « Back to the archive contents

/*
 * mapping.c
 *
 * Some handy mapping functions
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */


/* create a mapping of two arrays */
static mapping mkmapping(mixed *indices, mixed *values) {
  mapping map;
  int i, sz;
  if ((sz=::sizeof(indices)) != ::sizeof(values))
    error("Unequal argument sizes in mkmapping()");
  map = ([ ]);
  for (i = 0; i < sz; i++)
    map[indices[i]] = values[i];
  return map;
}


#ifndef map_delete
/* return a mapping with an element deleted
   (NB! Original mapping also changed)         */
static mapping map_delete(mapping map, string elt) {
  return map[elt] = 0;
}
#endif


/* map the values of a mapping */
static varargs mapping
map_mapping(mapping map, mixed func, mixed ob, mixed args...) {
  mixed *indices, *values;
  mapping copy;
  object o;
  int i, sz;
  
  if (stringp(ob)) {
    if (!(o=find_object(ob)))
      ob = compile_object(ob);
    else
      ob = o;
  }
  
  indices = map_indices(map);
  values = map_values(map);
  copy = ([ ]);
  for (i = 0, sz = ::sizeof(indices); i < sz; i++)
    copy[indices[i]] = __call_other(ob, func, values[i], args...);
  return copy;
}


/* subtract two mappings (a -= b not defined in LPC) */
static void map_sub(mapping a, mapping b) {
  if (mapp(a) && mapp(b)) {
    int i;
    string *keyz;
    keyz = map_indices(b);
    for (i=::sizeof(keyz); --i >= 0; )
      a[keyz[i]] = 0;
  }
}

Site Stats

Snippets: 1791
Codebases: 571