a745102001-02-22Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
29a20a2003-04-27Martin Nilsson #pragma strict_types
a745102001-02-22Fredrik Hübinette (Hubbe) 
a8c8b82003-07-24Henrik Grubbström (Grubba) //! Alias for @[m_delete()]
a745102001-02-22Fredrik Hübinette (Hubbe) constant delete=m_delete;
a8c8b82003-07-24Henrik Grubbström (Grubba) 
a745102001-02-22Fredrik Hübinette (Hubbe) constant Iterator = __builtin.mapping_iterator;
83c5ec2008-06-24Henrik Grubbström (Grubba) //! A mapping look-alike that overrides (ie shadows) another @[parent] mapping. //! //! The class implements most of the usual mapping operations.
9eaf1d2008-06-28Martin Nilsson class ShadowedMapping(protected mapping|ShadowedMapping parent)
83c5ec2008-06-24Henrik Grubbström (Grubba) {
9eaf1d2008-06-28Martin Nilsson  protected mapping shadow = ([]);
83c5ec2008-06-24Henrik Grubbström (Grubba) 
9eaf1d2008-06-28Martin Nilsson  protected mapping joined; protected mapping parent_copy;
83c5ec2008-06-24Henrik Grubbström (Grubba) 
9eaf1d2008-06-28Martin Nilsson  protected int(0..1) modify_parent;
3a6f652008-06-25Henrik Grubbström (Grubba) 
145f552008-06-27Henrik Grubbström (Grubba)  //! @decl void create(mapping|ShadowedMapping parent, @ //! mapping|void shadow, int(0..1)|void modify_parent) //! //! @param parent
3a6f652008-06-25Henrik Grubbström (Grubba)  //! Mapping to be shadowed.
145f552008-06-27Henrik Grubbström (Grubba)  //! @param shadow
3a6f652008-06-25Henrik Grubbström (Grubba)  //! Initial shadow of @[parent].
145f552008-06-27Henrik Grubbström (Grubba)  //! @param modify_parent
3a6f652008-06-25Henrik Grubbström (Grubba)  //! Modifications should be done to @[parent] rather than //! to @[shadow]. If this is set, only entries that are //! already present in @[shadow] can be modified by later //! operations.
145f552008-06-27Henrik Grubbström (Grubba) 
9eaf1d2008-06-28Martin Nilsson  protected void create(mapping|void shadow, int(0..1)|void modify_parent)
3a6f652008-06-25Henrik Grubbström (Grubba)  { if (shadow) this_program::shadow = shadow + ([]); this_program::modify_parent = modify_parent; }
83c5ec2008-06-24Henrik Grubbström (Grubba)  // Updates the cached joined mapping if needed.
9eaf1d2008-06-28Martin Nilsson  protected void update_joined()
83c5ec2008-06-24Henrik Grubbström (Grubba)  {
145f552008-06-27Henrik Grubbström (Grubba)  if (!joined || !equal(parent, parent_copy)) {
83c5ec2008-06-24Henrik Grubbström (Grubba)  joined = [mapping](parent + shadow);
145f552008-06-27Henrik Grubbström (Grubba)  parent_copy = [mapping](parent + ([]));
83c5ec2008-06-24Henrik Grubbström (Grubba)  } }
9eaf1d2008-06-28Martin Nilsson  protected mixed `[](mixed ind)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { mixed res = shadow[ind];
65340d2014-08-15Martin Nilsson  if (!undefinedp(res)) return res;
83c5ec2008-06-24Henrik Grubbström (Grubba)  return parent[ind]; }
9eaf1d2008-06-28Martin Nilsson  protected void `[]=(mixed ind, mixed val)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { joined = 0;
65340d2014-08-15Martin Nilsson  if (modify_parent && !has_index(shadow, ind)) {
3a6f652008-06-25Henrik Grubbström (Grubba)  parent[ind] = val; } else { shadow[ind] = val; }
83c5ec2008-06-24Henrik Grubbström (Grubba)  }
9eaf1d2008-06-28Martin Nilsson  protected mixed `->(string ind)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { return `[](ind); }
9eaf1d2008-06-28Martin Nilsson  protected void `->=(string ind, mixed val)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { `[]=(ind, val); }
9eaf1d2008-06-28Martin Nilsson  protected int(0..1) _equal(mixed other)
83c5ec2008-06-24Henrik Grubbström (Grubba)  {
3a6f652008-06-25Henrik Grubbström (Grubba)  update_joined();
145f552008-06-27Henrik Grubbström (Grubba)  return equal(other, joined);
83c5ec2008-06-24Henrik Grubbström (Grubba)  }
9eaf1d2008-06-28Martin Nilsson  protected mixed _m_delete(mixed ind)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { mixed res = m_delete(shadow, ind);
65340d2014-08-15Martin Nilsson  if (undefinedp(res))
83c5ec2008-06-24Henrik Grubbström (Grubba)  res = m_delete(parent, ind);
65340d2014-08-15Martin Nilsson  else
3a6f652008-06-25Henrik Grubbström (Grubba)  joined = 0;
65340d2014-08-15Martin Nilsson 
83c5ec2008-06-24Henrik Grubbström (Grubba)  return res; }
9eaf1d2008-06-28Martin Nilsson  protected array(mixed) _indices()
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return indices(joined); }
9eaf1d2008-06-28Martin Nilsson  protected array(mixed) _values()
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return values(joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed `+(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`+(joined, @args); }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``+(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`+(@args, joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed `-(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`-(joined, @args); }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``-(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`-(@args, joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed `|(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`|(joined, @args); }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``|(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`|(@args, joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed `&(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`&(joined, @args); }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``&(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`&(@args, joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed `^(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`^(joined, @args); }
9eaf1d2008-06-28Martin Nilsson  protected mixed ``^(mixed ... args)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return predef::`^(@args, joined); }
9eaf1d2008-06-28Martin Nilsson  protected mixed cast(string type)
83c5ec2008-06-24Henrik Grubbström (Grubba)  { update_joined(); switch(type) { case "mapping": return joined + ([]); case "array": return (array)joined; } return joined + ([]); }
c5979b2008-06-24Henrik Grubbström (Grubba) 
9eaf1d2008-06-28Martin Nilsson  protected mixed _search(mixed val)
224bf72008-06-24Henrik Grubbström (Grubba)  { update_joined(); return search(joined, val); }
9eaf1d2008-06-28Martin Nilsson  protected string _sprintf(int c)
c5979b2008-06-24Henrik Grubbström (Grubba)  { update_joined(); return sprintf(sprintf("%%%c", c), joined); }
83c5ec2008-06-24Henrik Grubbström (Grubba) }