Skip to content

Commit

Permalink
Add Any.delete-key, Hash.delete-key, Bool.Numeric accelerators
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed May 29, 2011
1 parent 3cbbfcd commit ae3d3b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
21 changes: 0 additions & 21 deletions lib/CORE.setting
Expand Up @@ -308,7 +308,6 @@ my class Bool is EnumType {
method Stringy() { self.key }
method perl() { defined(self) ?? ~self !! self.typename }
method ACCEPTS(Mu $t) { defined(self) ?? self !! $t.^does(self) }
method Numeric() { self ?? 1 !! 0 }
our constant True = Q:CgOp { (box Bool (bool 1)) };
our constant False = Q:CgOp { (box Bool (bool 0)) };
method succ() { True }
Expand Down Expand Up @@ -830,20 +829,6 @@ my class Hash {
};
}

method delete-key($str) {
Q:CgOp {
(ternary (obj_is_defined (@ {self}))
(letn r (unbox varhash (@ {self}))
k (obj_getstr {$str})
old (ternary (varhash_contains_key (l r) (l k))
(varhash_getindex (l k) (l r))
{Any})
(varhash_delete_key (l r) (l k))
(l old))
{Any})
};
}
# Rakudo extensions compatibility - DO NOT USE
method delete($key) { self.{$key}:delete }
method exists($key) { self.{$key}:exists }
Expand All @@ -855,12 +840,6 @@ my class Hash {
}
}

augment class Any {
method delete-key($) {
defined(self) ?? die("Cannot use hash access on an object of type $.typename") !! Any
}
}

my class Enum is Cool {
has $.key;
has Mu $.value;
Expand Down
40 changes: 38 additions & 2 deletions lib/Kernel.cs
Expand Up @@ -1420,6 +1420,13 @@ class CtxBoolNativeDefined : ContextHandler<Variable> {
}
}

class CtxBoolRawNumeric : ContextHandler<double> {
public override double Get(Variable obj) {
P6any o = obj.Fetch();
return (o.IsDefined() && Kernel.UnboxAny<bool>(o)) ? 1 : 0;
}
}

class CtxNumSuccish : ContextHandler<P6any> {
double amt;
public CtxNumSuccish(double amt) { this.amt = amt; }
Expand Down Expand Up @@ -1637,6 +1644,18 @@ class IxCallMethod : IndexHandler {
}
}

class IxAnyDeleteKey : IndexHandler {
public override Variable Get(Variable obj, Variable key) {
if (key.islist) {
return Slice(obj, key);
}

P6any os = obj.Fetch();
if (!os.IsDefined())
return Kernel.NewROScalar(Kernel.AnyP);
throw new NieczaException("Cannot use hash access on an object of type " + os.mo.name);
}
}
class IxAnyExistsKey : IndexHandler {
public override Variable Get(Variable obj, Variable key) {
if (key.islist) {
Expand Down Expand Up @@ -1726,11 +1745,25 @@ class IxHashExistsKey : IndexHandler {
P6any os = obj.Fetch();
if (!os.IsDefined()) return Kernel.FalseV;
string ks = key.Fetch().mo.mro_raw_Str.Get(key);
VarHash h =
Kernel.UnboxAny<VarHash>(os);
VarHash h = Kernel.UnboxAny<VarHash>(os);
return h.ContainsKey(ks) ? Kernel.TrueV : Kernel.FalseV;
}
}
class IxHashDeleteKey : IndexHandler {
public override Variable Get(Variable obj, Variable key) {
P6any os = obj.Fetch();
if (!os.IsDefined()) return Kernel.NewROScalar(Kernel.AnyP);
string ks = key.Fetch().mo.mro_raw_Str.Get(key);
VarHash h = Kernel.UnboxAny<VarHash>(os);
Variable r;
if (h.TryGetValue(ks, out r)) {
h.Remove(ks);
return r;
} else {
return Kernel.NewROScalar(Kernel.AnyP);
}
}
}

class IxListAtPos : IndexHandler {
bool extend;
Expand Down Expand Up @@ -2975,6 +3008,7 @@ class LastFrameNode {
BoolMO = new STable("Bool");
Handler_Vonly(BoolMO, "Bool", new CtxReturnSelf(),
new CtxJustUnbox<bool>(false));
Handler_PandBoxInty(BoolMO, "Numeric", new CtxBoolRawNumeric());
BoolMO.FillProtoClass(new string[] { });
BoolMO.Invalidate();
TrueV = NewROScalar(BoxRaw<bool>(true, BoolMO));
Expand Down Expand Up @@ -3078,6 +3112,7 @@ class LastFrameNode {

HashMO = new STable("Hash");
WrapHandler1(HashMO, "exists-key", new IxHashExistsKey());
WrapHandler1(HashMO, "delete-key", new IxHashDeleteKey());
WrapHandler1(HashMO, "at-key", new IxHashAtKey());
Handler_PandBox(HashMO, "iterator", new CtxHashIterator(), IteratorMO);
Handler_PandBox(HashMO, "Bool", new CtxHashBool(), BoolMO);
Expand All @@ -3087,6 +3122,7 @@ class LastFrameNode {

AnyMO = new STable("Any");
WrapHandler1(AnyMO, "exists-key", new IxAnyExistsKey());
WrapHandler1(AnyMO, "delete-key", new IxAnyDeleteKey());
WrapHandler1(AnyMO, "at-key", new IxAnyAtKey());
WrapHandler1(AnyMO, "at-pos", new IxAnyAtPos());
Handler_Vonly(AnyMO, "list", new CtxAnyList(), null);
Expand Down
4 changes: 1 addition & 3 deletions perf/perf.TODO
Expand Up @@ -17,6 +17,4 @@ Improve isa, somehow.

Subquadratic nibbling.

81537 Mu.new attr: 14093 RxOp.zyg,

Missing accelerators: Bool.Numeric, Hash.delete-key, Any.delete-key
81537 Mu.new attr: 14093 RxOp.zyg,

0 comments on commit ae3d3b7

Please sign in to comment.