1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#pike __REAL_VERSION__
#pragma strict_types
//! Alias for @[m_delete()]
constant delete=m_delete;
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(protected mapping|ShadowedMapping parent)
{
protected mapping shadow = ([]);
protected mapping joined;
protected mapping parent_copy;
protected int(0..1) modify_parent;
//! @decl void create(mapping|ShadowedMapping parent, @
//! mapping|void shadow, int(0..1)|void modify_parent)
//!
//! @param parent
//! Mapping to be shadowed.
//! @param shadow
//! Initial shadow of @[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.
protected void create(mapping|void shadow, int(0..1)|void modify_parent)
{
if (shadow) this::shadow = shadow + ([]);
this::modify_parent = modify_parent;
}
// Updates the cached joined mapping if needed.
protected void update_joined()
{
if (!joined || !equal(parent, parent_copy)) {
joined = [mapping](parent + shadow);
parent_copy = [mapping](parent + ([]));
}
}
protected mixed `[](mixed ind)
{
mixed res = shadow[ind];
if (!undefinedp(res)) return res;
return parent[ind];
}
protected void `[]=(mixed ind, mixed val)
{
joined = 0;
if (modify_parent && !has_index(shadow, ind)) {
parent[ind] = val;
} else {
shadow[ind] = val;
}
}
protected mixed `->(string ind)
{
return `[](ind);
}
protected void `->=(string ind, mixed val)
{
`[]=(ind, val);
}
protected int(0..1) _equal(mixed other)
{
update_joined();
return equal(other, joined);
}
protected mixed _m_delete(mixed ind)
{
mixed res = m_delete(shadow, ind);
if (undefinedp(res))
res = m_delete(parent, ind);
else
joined = 0;
return res;
}
protected array(mixed) _indices()
{
update_joined();
return indices(joined);
}
protected array(mixed) _values()
{
update_joined();
return values(joined);
}
protected mixed `+(mixed ... args)
{
update_joined();
return predef::`+(joined, @args);
}
protected mixed ``+(mixed ... args)
{
update_joined();
return predef::`+(@args, joined);
}
protected mixed `-(mixed ... args)
{
update_joined();
return predef::`-(joined, @args);
}
protected mixed ``-(mixed ... args)
{
update_joined();
return predef::`-(@args, joined);
}
protected mixed `|(mixed ... args)
{
update_joined();
return predef::`|(joined, @args);
}
protected mixed ``|(mixed ... args)
{
update_joined();
return predef::`|(@args, joined);
}
protected mixed `&(mixed ... args)
{
update_joined();
return predef::`&(joined, @args);
}
protected mixed ``&(mixed ... args)
{
update_joined();
return predef::`&(@args, joined);
}
protected mixed `^(mixed ... args)
{
update_joined();
return predef::`^(joined, @args);
}
protected mixed ``^(mixed ... args)
{
update_joined();
return predef::`^(@args, joined);
}
protected mixed cast(string type)
{
update_joined();
switch(type) {
case "mapping": return joined + ([]);
case "array": return (array)joined;
}
return UNDEFINED;
}
protected mixed _search(mixed val)
{
update_joined();
return search(joined, val);
}
protected string _sprintf(int c)
{
update_joined();
return sprintf(sprintf("%%%c", c), joined);
}
}
|