pike.git / lib / modules / Mapping.pmod

version» Context lines:

pike.git/lib/modules/Mapping.pmod:7:   constant Iterator = __builtin.mapping_iterator;      //! 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; -  static int parent_generation = -1; +  static mapping parent_copy;    -  static int generation; -  static int dirty = 1; -  +     static int(0..1) modify_parent;    -  //! @param mapping|ShadowedMapping parent +  //! @decl void create(mapping|ShadowedMapping parent, @ +  //! mapping|void shadow, int(0..1)|void modify_parent) +  //! +  //! @param parent    //! Mapping to be shadowed. -  //! @param mapping|void shadow +  //! @param shadow    //! Initial shadow of @[parent]. -  //! @param int(0..1) modify_parent +  //! @param modify_parent    //! 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. -  +     static void create(mapping|void shadow, int(0..1)|void modify_parent)    {    if (shadow) this_program::shadow = shadow + ([]);    this_program::modify_parent = modify_parent;    }       // Updates the cached joined mapping if needed.    static void update_joined()    { -  if (!joined || (parent_generation != m_generation(parent))) { +  if (!joined || !equal(parent, parent_copy)) {    joined = [mapping](parent + shadow); -  parent_generation = m_generation(parent); -  dirty = 1; +  parent_copy = [mapping](parent + ([]));    }    }       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; -  dirty = 1; +     if (modify_parent && zero_type(shadow[ind])) {    parent[ind] = val;    } else {    shadow[ind] = val;    }    }       static mixed `->(string ind)    {    return `[](ind);    }       static void `->=(string ind, mixed val)    {    `[]=(ind, val);    }    -  static int _m_generation() +  static int(0..1) _equal(mixed other)    {    update_joined(); -  if (dirty) { -  generation++; -  dirty = 0; +  return equal(other, joined);    } -  return generation; -  } +        static mixed _m_delete(mixed ind)    {    mixed res = m_delete(shadow, ind);    if (zero_type(res)) {    res = m_delete(parent, ind);    }    if (!zero_type(res)) {    joined = 0; -  dirty = 1; +     }    return res;    }       static array(mixed) _indices()    {    update_joined();    return indices(joined);    }