Skip to content

Commit

Permalink
implemented Association'Drop()
Browse files Browse the repository at this point in the history
Task #131 - provide Association'Drop()
  • Loading branch information
grzegorzmazur committed Oct 5, 2015
1 parent 8185ce4 commit f5de44b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 31 deletions.
5 changes: 0 additions & 5 deletions JavaYacas/net/sf/yacas/ArrayClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ public ArrayClass(int aSize,LispObject aInitialItem)
iArray = new LispPtrArray(aSize,aInitialItem);
}
@Override
public String Send(LispArgList aArgList)
{
return null;
}
@Override
public String TypeName()
{
return "\"Array\"";
Expand Down
9 changes: 4 additions & 5 deletions JavaYacas/net/sf/yacas/AssociationClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public AssociationClass(LispEnvironment env) {
this._map = new TreeMap<>(new Cmp(env));
}

@Override
public String Send(LispArgList aArgList) {
return null;
}

@Override
public String TypeName() {
return "\"Association\"";
Expand All @@ -63,5 +58,9 @@ public void SetElement(LispObject k, LispObject v) throws Exception {
_map.put(new LispPtr(k), new LispPtr(v));
}

public boolean DropElement(LispObject k) throws Exception {
return _map.remove(new LispPtr(k)) != null;
}

Map<LispPtr, LispPtr> _map;
}
9 changes: 1 addition & 8 deletions JavaYacas/net/sf/yacas/GenericClass.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package net.sf.yacas;


/// Abstract class which can be put inside a LispGenericClass.
abstract class GenericClass
{
public GenericClass()
{
}
public abstract String Send(LispArgList aArgList);
abstract class GenericClass {
public abstract String TypeName();
}

26 changes: 26 additions & 0 deletions JavaYacas/net/sf/yacas/MathCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ public void AddCommands(LispEnvironment aEnvironment)
aEnvironment.CoreCommands().put(
"Association'Set",
new YacasEvaluator(new GenAssociationSet(),3, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"Association'Drop",
new YacasEvaluator(new GenAssociationDrop(), 2, YacasEvaluator.Fixed|YacasEvaluator.Function));
aEnvironment.CoreCommands().put(
"CustomEval",
new YacasEvaluator(new LispCustomEval(),4, YacasEvaluator.Fixed|YacasEvaluator.Macro));
Expand Down Expand Up @@ -3415,6 +3418,29 @@ public void Eval(LispEnvironment aEnvironment, int aStackTop) throws Exception {
}
}

class GenAssociationDrop extends YacasEvalCaller {

@Override
public void Eval(LispEnvironment env, int stack_top) throws Exception {
LispPtr evaluated = new LispPtr();
evaluated.Set(ARGUMENT(env, stack_top, 1).Get());

GenericClass gen = evaluated.Get().Generic();
LispError.CHK_ARG_CORE(env, stack_top, gen != null, 1);
LispError.CHK_ARG_CORE(env, stack_top, gen.TypeName().equals("\"Association\""), 1);

LispPtr k = new LispPtr();
k.Set(ARGUMENT(env, stack_top, 2).Get());

LispError.CHK_ARG_CORE(env, stack_top, k.Get() != null, 2);

if (((AssociationClass)gen).DropElement(k.Get()))
LispStandard.InternalTrue(env, RESULT(env, stack_top));
else
LispStandard.InternalFalse(env, RESULT(env, stack_top));
}
}

class LispCustomEval extends YacasEvalCaller
{
@Override
Expand Down
5 changes: 0 additions & 5 deletions JavaYacas/net/sf/yacas/PatternClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public boolean Matches(LispEnvironment aEnvironment, LispPtr[] aArguments) thro
}
//From GenericClass
@Override
public String Send(LispArgList aArgList)
{
return null;
}
@Override
public String TypeName()
{
return "\"Pattern\"";
Expand Down
16 changes: 8 additions & 8 deletions include/yacas/associationclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ class AssociationClass : public GenericClass
bool Contains(LispObject* k) const;
LispObject* GetElement(LispObject* k);
void SetElement(LispObject* k,LispObject* v);
void DropElement(LispObject* k);
bool DropElement(LispObject* k);

private:
struct Key {
class Key {
public:
Key(const LispEnvironment& env, LispObject* p):
env(env), p(p) {}

const LispEnvironment& env;
LispPtr p;

bool operator == (const Key& rhs) const
{
return InternalEquals(env, p, rhs.p);
Expand All @@ -60,6 +58,9 @@ class AssociationClass : public GenericClass
{
return InternalStrictTotalOrder(env, p, rhs.p);
}
private:
const LispEnvironment& env;
LispPtr p;
};

const LispEnvironment& _env;
Expand Down Expand Up @@ -106,10 +107,9 @@ void AssociationClass::SetElement(LispObject* k, LispObject* v)
}

inline
void AssociationClass::DropElement(LispObject* k)
bool AssociationClass::DropElement(LispObject* k)
{
_map.erase(Key(_env, LispPtr(k)));
return _map.erase(Key(_env, LispPtr(k)));
}

#endif /* ASSOCIATIONCLASS_H */

1 change: 1 addition & 0 deletions include/yacas/corefunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ CORE_KERNEL_FUNCTION("Association'Create",GenAssociationCreate,0,YacasEvaluator:
CORE_KERNEL_FUNCTION("Association'Size",GenAssociationSize,1,YacasEvaluator::Function | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("Association'Get",GenAssociationGet,2,YacasEvaluator::Function | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("Association'Set",GenAssociationSet,3,YacasEvaluator::Function | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("Association'Drop",GenAssociationDrop,2,YacasEvaluator::Function | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("CustomEval",LispCustomEval,4,YacasEvaluator::Macro | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("CustomEval'Expression",LispCustomEvalExpression,0,YacasEvaluator::Function | YacasEvaluator::Fixed)
CORE_KERNEL_FUNCTION("CustomEval'Result",LispCustomEvalResult,0,YacasEvaluator::Function | YacasEvaluator::Fixed)
Expand Down
13 changes: 13 additions & 0 deletions src/mathcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,19 @@ void GenAssociationSet(LispEnvironment& aEnvironment,LispInt aStackTop)
InternalTrue( aEnvironment, RESULT);
}

void GenAssociationDrop(LispEnvironment& aEnvironment,LispInt aStackTop)
{
LispPtr p(ARGUMENT(1));
GenericClass* gen = p->Generic();
AssociationClass* a = dynamic_cast<AssociationClass*>(gen);
CheckArg(a, 1, aEnvironment, aStackTop);

LispPtr k(ARGUMENT(2));
if (a->DropElement(k))
InternalTrue(aEnvironment,RESULT);
else
InternalFalse(aEnvironment,RESULT);
}

void LispCustomEval(LispEnvironment& aEnvironment,LispInt aStackTop)
{
Expand Down
3 changes: 3 additions & 0 deletions tests/association.yts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ NextTest("Association");
Verify(Association'Set(a, x, y), True);
Verify(Association'Size(a), 1);
Verify(Association'Get(a, x), y);
Verify(Association'Drop(a, x), True);
Verify(Association'Drop(a, x), False);
Verify(Association'Get(a, x), Undefined);
];

0 comments on commit f5de44b

Please sign in to comment.