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. class ShadowedMapping(static mapping|ShadowedMapping parent) { static mapping shadow = ([]); static mapping joined;
145f552008-06-27Henrik Grubbström (Grubba)  static mapping parent_copy;
83c5ec2008-06-24Henrik Grubbström (Grubba) 
3a6f652008-06-25Henrik Grubbström (Grubba)  static int(0..1) modify_parent;
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) 
3a6f652008-06-25Henrik Grubbström (Grubba)  static void create(mapping|void shadow, int(0..1)|void modify_parent) { 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. static void update_joined() {
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)  } } static mixed `[](mixed ind) { mixed res = shadow[ind]; if (!zero_type(res)) return res; return parent[ind]; } static void `[]=(mixed ind, mixed val) { joined = 0;
3a6f652008-06-25Henrik Grubbström (Grubba)  if (modify_parent && zero_type(shadow[ind])) { parent[ind] = val; } else { shadow[ind] = val; }
83c5ec2008-06-24Henrik Grubbström (Grubba)  } static mixed `->(string ind) { return `[](ind); } static void `->=(string ind, mixed val) { `[]=(ind, val); }
145f552008-06-27Henrik Grubbström (Grubba)  static 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)  } static mixed _m_delete(mixed ind) { mixed res = m_delete(shadow, ind); if (zero_type(res)) { res = m_delete(parent, ind); }
3a6f652008-06-25Henrik Grubbström (Grubba)  if (!zero_type(res)) { joined = 0; }
83c5ec2008-06-24Henrik Grubbström (Grubba)  return res; } static array(mixed) _indices() { update_joined(); return indices(joined); } static array(mixed) _values() { update_joined(); return values(joined); } static mixed `+(mixed ... args) { update_joined(); return predef::`+(joined, @args); } static mixed ``+(mixed ... args) { update_joined(); return predef::`+(@args, joined); } static mixed `-(mixed ... args) { update_joined(); return predef::`-(joined, @args); } static mixed ``-(mixed ... args) { update_joined(); return predef::`-(@args, joined); } static mixed `|(mixed ... args) { update_joined(); return predef::`|(joined, @args); } static mixed ``|(mixed ... args) { update_joined(); return predef::`|(@args, joined); } static mixed `&(mixed ... args) { update_joined(); return predef::`&(joined, @args); } static mixed ``&(mixed ... args) { update_joined(); return predef::`&(@args, joined); } static mixed `^(mixed ... args) { update_joined(); return predef::`^(joined, @args); } static mixed ``^(mixed ... args) { update_joined(); return predef::`^(@args, joined); } static mixed cast(string type) { update_joined(); switch(type) { case "mapping": return joined + ([]); case "array": return (array)joined; } return joined + ([]); }
c5979b2008-06-24Henrik Grubbström (Grubba) 
224bf72008-06-24Henrik Grubbström (Grubba)  static mixed _search(mixed val) { update_joined(); return search(joined, val); }
c5979b2008-06-24Henrik Grubbström (Grubba)  static string _sprintf(int c) { update_joined(); return sprintf(sprintf("%%%c", c), joined); }
83c5ec2008-06-24Henrik Grubbström (Grubba) }