From: <ag...@us...> - 2011-07-17 00:54:36
|
Revision: 2401 http://zoolib.svn.sourceforge.net/zoolib/?rev=2401&view=rev Author: agreen Date: 2011-07-17 00:54:30 +0000 (Sun, 17 Jul 2011) Log Message: ----------- Massively opaque, and no docs as yet. Added Paths: ----------- trunk/zoolib/source/cxx/zoolib/ZCog.h Added: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h (rev 0) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-07-17 00:54:30 UTC (rev 2401) @@ -0,0 +1,355 @@ +/* ------------------------------------------------------------------------------------------------- +Copyright (c) 2011 Andrew Green +http://www.zoolib.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software +and associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES +OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF +OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------------------------- */ + +#ifndef __ZCog__ +#define __ZCog__ 1 + +#include "zoolib/ZCallable.h" +#include "zoolib/ZCallable_Bind.h" +#include "zoolib/ZCallable_Function.h" + +namespace ZooLib { + +// ================================================================================================= +#pragma mark - +#pragma mark * ZCog + +template <class Param_p> +class ZCog +: public ZRef<ZCallable<ZCog<Param_p>(const ZCog<Param_p>&,Param_p)> > + { +public: + typedef Param_p Param; + typedef ZCog(Signature)(const ZCog& iSelf, Param iParam); + typedef ZCallable<Signature> Callable; + typedef ZRef<Callable> inherited; + +//-- + + ZCog() + {} + + ZCog(const ZCog& iOther) + : inherited(iOther) + {} + + ~ZCog() + {} + + ZCog& operator=(const ZCog& iOther) + { + inherited::operator=(iOther); + return *this; + } + +//-- + + ZCog(const inherited& iOther) + : inherited(iOther) + {} + + ZCog& operator=(const inherited& iOther) + { + inherited::operator=(iOther); + return *this; + } + +//-- + + ZCog(Callable* iCallable) + : inherited(iCallable) + {} + +//-- + + ZCog(const null_t&) + {} + }; + +// ================================================================================================= +#pragma mark - +#pragma mark * sCog + +const struct + { + template <class Param> + ZCog<Param> operator() + (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const + { return ZCog<Param>(iCallable); } + + template <class Param> + ZCog<Param> operator() + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const + { return ZCog<Param>(iCallable); } + + template <class Param> + ZCog<Param> operator() + (const ZCog<Param>& iCog) const + { return iCog; } + + template <class Param> + ZCog<Param> operator& + (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const + { return ZCog<Param>(iCallable); } + + template <class Param> + ZCog<Param> operator& + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const + { return ZCog<Param>(iCallable); } + + template <class Param> + ZCog<Param> operator& + (const ZCog<Param>& iCog) const + { return iCog; } + + } sCog = {}; + +// ================================================================================================= +#pragma mark - +#pragma mark * sAny + +template <class Param> +ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1); + +template <class Param> +ZCog<Param> sAny(const ZCog<Param>& i0, const ZCog<Param>& i1) + { + if (i0) + { + if (i1) + return sBindR(sCallable(sAny_Fun<Param>), i0, i1); + return i0; + } + return i1; + } + +template <class Param> +ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1) + { + if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + { + if (i1 && not sAssignIfUnequal(i1, i1->Call(i1, iParam))) + return iSelf; + } + else if (i1) + { + i1 = i1->Call(i1, iParam); + } + + return sAny(i0, i1); + } + +template <class Param> +ZCog<Param> operator&(const ZCog<Param>& i0, const ZCog<Param>& i1) + { return sAny(i0, i1); } + +template <class Param> +ZCog<Param>& operator&=(ZCog<Param>& i0, const ZCog<Param>& i1) + { return i0 = sAny(i0, i1); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sAll + +template <class Param> +ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1); + +template <class Param> +ZCog<Param> sAll(const ZCog<Param>& i0, const ZCog<Param>& i1) + { + if (i0 && i1) + return sBindR(sCallable(sAll_Fun<Param>), i0, i1); + return null; + } + +template <class Param> +ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1) + { + if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + { + if (i1 && not sAssignIfUnequal(i1, i1->Call(i1, iParam))) + return iSelf; + } + else if (i0 && i1) + { + i1 = i1->Call(i1, iParam); + } + + return sAll(i0, i1); + } + +template <class Param> +ZCog<Param> operator*(const ZCog<Param>& i0, const ZCog<Param>& i1) + { return sAll(i0, i1); } + +template <class Param> +ZCog<Param>& operator*=(ZCog<Param>& i0, const ZCog<Param>& i1) + { return i0 = sAll(i0, i1); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sSeq + +template <class Param> +ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1); + +template <class Param> +ZCog<Param> sSeq(const ZCog<Param>& i0, const ZCog<Param>& i1) + { + if (i0) + { + if (i1) + return sBindR(sCallable(sSeq_Fun<Param>), i0, i1); + return i0; + } + return i1; + } + +template <class Param> +ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> i0, ZCog<Param> i1) + { + if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + return iSelf; + + if (!i0 && i1) + i1 = i1->Call(i1, iParam); + + return sSeq(i0, i1); + } + +template <class Param> +ZCog<Param> operator|(const ZCog<Param>& i0, const ZCog<Param>& i1) + { return sSeq(i0, i1); } + +template <class Param> +ZCog<Param>& operator|=(ZCog<Param>& i0, const ZCog<Param>& i1) + { return i0 = sSeq(i0, i1); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sRepeat + +template <class Param> +ZCog<Param> sRepeat_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> iChild_Start, ZCog<Param> iChild) + { + if (not iChild || not sAssignIfUnequal(iChild, iChild->Call(iChild, iParam))) + return iSelf; + + if (iChild) + return sBindR(sCallable(sRepeat_Fun<Param>), iChild_Start, iChild); + + return sBindR(sCallable(sRepeat_Fun<Param>), iChild_Start, iChild_Start); + } + +template <class Param> +ZCog<Param> sRepeat(ZCog<Param> iChild) + { return sBindR(sCallable(sRepeat_Fun<Param>), iChild, iChild); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sRepeat (count) + +template <class Param> +ZCog<Param> sRepeat_Count_Fun(const ZCog<Param>& iSelf, Param iParam, + size_t iCount, ZCog<Param> iChild_Start, ZCog<Param> iChild) + { + if (not iCount) + return null; + + if (iChild && not sAssignIfUnequal(iChild, iChild->Call(iChild, iParam))) + return iSelf; + + if (iChild) + return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild_Start, iChild); + + if (--iCount) + return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild_Start, iChild_Start); + + return null; + } + +template <class Param> +ZCog<Param> sRepeat(size_t iCount, ZCog<Param> iChild) + { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild, iChild); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sDelayUntil + +template <class Param> +ZCog<Param> sDelayUntil_Fun(const ZCog<Param>& iSelf, Param iParam, + ZTime iSystemTime) + { + if (ZTime::sSystem() > iSystemTime) + return null; + return iSelf; + } + +template <class Param> +ZCog<Param> sDelayUntil(ZTime iSystemTime) + { return sBindR(sCallable(sDelayUntil_Fun<Param>), iSystemTime); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sDelayFor + +template <class Param> +ZCog<Param> sDelayFor_Fun(const ZCog<Param>& iSelf, Param iParam, + double iDelay) + { + if (ZCog<Param> theZCog = sDelayUntil<Param>(ZTime::sSystem() + iDelay)) + return theZCog->Call(theZCog, iParam); + return null; + } + +template <class Param> +ZCog<Param> sDelayFor(double iDelay) + { return sBindR(sCallable(sDelayFor_Fun<Param>), iDelay); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sStartAt, sStopAt, sStartAfter, sStopAfter + +template <class Param> +ZCog<Param> sStartAt(ZTime iSystemTime, ZCog<Param> iChild) + { return sDelayUntil<Param>(iSystemTime) | iChild; } + +template <class Param> +ZCog<Param> sStopAt(ZTime iSystemTime, ZCog<Param> iChild) + { return sDelayUntil<Param>(iSystemTime) * iChild; } + +template <class Param> +ZCog<Param> sStartAfter(double iDelay, ZCog<Param> iChild) + { return sDelayFor<Param>(iDelay) | iChild; } + +template <class Param> +ZCog<Param> sStopAfter(double iDelay, ZCog<Param> iChild) + { return sDelayFor<Param>(iDelay) * iChild; } + +} // namespace ZooLib + +#endif // __ZCog__ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-07-17 01:36:55
|
Revision: 2407 http://zoolib.svn.sourceforge.net/zoolib/?rev=2407&view=rev Author: agreen Date: 2011-07-17 01:36:49 +0000 (Sun, 17 Jul 2011) Log Message: ----------- Cleanup. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-07-17 01:36:27 UTC (rev 2406) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-07-17 01:36:49 UTC (rev 2407) @@ -77,10 +77,22 @@ : inherited(iCallable) {} + ZCog& operator=(Callable* iCallable) + { + inherited::operator=(iCallable); + return *this; + } + //-- ZCog(const null_t&) {} + + ZCog& operator=(const null_t& iNull) + { + inherited::operator=(iNull); + return *this; + } }; // ================================================================================================= @@ -127,44 +139,44 @@ template <class Param> ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1); + ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sAny(const ZCog<Param>& i0, const ZCog<Param>& i1) +ZCog<Param> sAny(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - if (i0) + if (iCog0) { - if (i1) - return sBindR(sCallable(sAny_Fun<Param>), i0, i1); - return i0; + if (iCog1) + return sBindR(sCallable(sAny_Fun<Param>), iCog0, iCog1); + return iCog0; } - return i1; + return iCog1; } template <class Param> ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1) + ZCog<Param> iCog0, ZCog<Param> iCog1) { - if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) { - if (i1 && not sAssignIfUnequal(i1, i1->Call(i1, iParam))) + if (iCog1 && not sCompareAndSet(iCog1, iCog1->Call(iCog1, iParam))) return iSelf; } - else if (i1) + else if (iCog1) { - i1 = i1->Call(i1, iParam); + iCog1 = iCog1->Call(iCog1, iParam); } - return sAny(i0, i1); + return sAny(iCog0, iCog1); } template <class Param> -ZCog<Param> operator&(const ZCog<Param>& i0, const ZCog<Param>& i1) - { return sAny(i0, i1); } +ZCog<Param> operator&(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sAny(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator&=(ZCog<Param>& i0, const ZCog<Param>& i1) - { return i0 = sAny(i0, i1); } +ZCog<Param>& operator&=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return iCog0 = sAny(iCog0, iCog1); } // ================================================================================================= #pragma mark - @@ -172,40 +184,40 @@ template <class Param> ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1); + ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sAll(const ZCog<Param>& i0, const ZCog<Param>& i1) +ZCog<Param> sAll(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - if (i0 && i1) - return sBindR(sCallable(sAll_Fun<Param>), i0, i1); + if (iCog0 && iCog1) + return sBindR(sCallable(sAll_Fun<Param>), iCog0, iCog1); return null; } template <class Param> ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1) + ZCog<Param> iCog0, ZCog<Param> iCog1) { - if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) { - if (i1 && not sAssignIfUnequal(i1, i1->Call(i1, iParam))) + if (iCog1 && not sCompareAndSet(iCog1, iCog1->Call(iCog1, iParam))) return iSelf; } - else if (i0 && i1) + else if (iCog0 && iCog1) { - i1 = i1->Call(i1, iParam); + iCog1 = iCog1->Call(iCog1, iParam); } - return sAll(i0, i1); + return sAll(iCog0, iCog1); } template <class Param> -ZCog<Param> operator*(const ZCog<Param>& i0, const ZCog<Param>& i1) - { return sAll(i0, i1); } +ZCog<Param> operator*(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sAll(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator*=(ZCog<Param>& i0, const ZCog<Param>& i1) - { return i0 = sAll(i0, i1); } +ZCog<Param>& operator*=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return iCog0 = sAll(iCog0, iCog1); } // ================================================================================================= #pragma mark - @@ -213,40 +225,40 @@ template <class Param> ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1); + ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sSeq(const ZCog<Param>& i0, const ZCog<Param>& i1) +ZCog<Param> sSeq(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - if (i0) + if (iCog0) { - if (i1) - return sBindR(sCallable(sSeq_Fun<Param>), i0, i1); - return i0; + if (iCog1) + return sBindR(sCallable(sSeq_Fun<Param>), iCog0, iCog1); + return iCog0; } - return i1; + return iCog1; } template <class Param> ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> i0, ZCog<Param> i1) + ZCog<Param> iCog0, ZCog<Param> iCog1) { - if (i0 && not sAssignIfUnequal(i0, i0->Call(i0, iParam))) + if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) return iSelf; - if (!i0 && i1) - i1 = i1->Call(i1, iParam); + if (!iCog0 && iCog1) + iCog1 = iCog1->Call(iCog1, iParam); - return sSeq(i0, i1); + return sSeq(iCog0, iCog1); } template <class Param> -ZCog<Param> operator|(const ZCog<Param>& i0, const ZCog<Param>& i1) - { return sSeq(i0, i1); } +ZCog<Param> operator|(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sSeq(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator|=(ZCog<Param>& i0, const ZCog<Param>& i1) - { return i0 = sSeq(i0, i1); } +ZCog<Param>& operator|=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return iCog0 = sSeq(iCog0, iCog1); } // ================================================================================================= #pragma mark - @@ -254,20 +266,20 @@ template <class Param> ZCog<Param> sRepeat_Fun(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iChild_Start, ZCog<Param> iChild) + ZCog<Param> iCog_Init, ZCog<Param> iCog) { - if (not iChild || not sAssignIfUnequal(iChild, iChild->Call(iChild, iParam))) + if (not iCog || not sCompareAndSet(iCog, iCog->Call(iCog, iParam))) return iSelf; - if (iChild) - return sBindR(sCallable(sRepeat_Fun<Param>), iChild_Start, iChild); + if (iCog) + return sBindR(sCallable(sRepeat_Fun<Param>), iCog_Init, iCog); - return sBindR(sCallable(sRepeat_Fun<Param>), iChild_Start, iChild_Start); + return sBindR(sCallable(sRepeat_Fun<Param>), iCog_Init, iCog_Init); } template <class Param> -ZCog<Param> sRepeat(ZCog<Param> iChild) - { return sBindR(sCallable(sRepeat_Fun<Param>), iChild, iChild); } +ZCog<Param> sRepeat(ZCog<Param> iCog) + { return sBindR(sCallable(sRepeat_Fun<Param>), iCog, iCog); } // ================================================================================================= #pragma mark - @@ -275,26 +287,26 @@ template <class Param> ZCog<Param> sRepeat_Count_Fun(const ZCog<Param>& iSelf, Param iParam, - size_t iCount, ZCog<Param> iChild_Start, ZCog<Param> iChild) + size_t iCount, ZCog<Param> iCog_Init, ZCog<Param> iCog) { if (not iCount) return null; - if (iChild && not sAssignIfUnequal(iChild, iChild->Call(iChild, iParam))) + if (iCog && not sCompareAndSet(iCog, iCog->Call(iCog, iParam))) return iSelf; - if (iChild) - return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild_Start, iChild); + if (iCog) + return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog_Init, iCog); if (--iCount) - return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild_Start, iChild_Start); + return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog_Init, iCog_Init); return null; } template <class Param> -ZCog<Param> sRepeat(size_t iCount, ZCog<Param> iChild) - { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iChild, iChild); } +ZCog<Param> sRepeat(size_t iCount, ZCog<Param> iCog) + { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog, iCog); } // ================================================================================================= #pragma mark - @@ -321,8 +333,8 @@ ZCog<Param> sDelayFor_Fun(const ZCog<Param>& iSelf, Param iParam, double iDelay) { - if (ZCog<Param> theZCog = sDelayUntil<Param>(ZTime::sSystem() + iDelay)) - return theZCog->Call(theZCog, iParam); + if (ZCog<Param> theCog = sDelayUntil<Param>(ZTime::sSystem() + iDelay)) + return theCog->Call(theCog, iParam); return null; } @@ -335,20 +347,20 @@ #pragma mark * sStartAt, sStopAt, sStartAfter, sStopAfter template <class Param> -ZCog<Param> sStartAt(ZTime iSystemTime, ZCog<Param> iChild) - { return sDelayUntil<Param>(iSystemTime) | iChild; } +ZCog<Param> sStartAt(ZTime iSystemTime, ZCog<Param> iCog) + { return sDelayUntil<Param>(iSystemTime) | iCog; } template <class Param> -ZCog<Param> sStopAt(ZTime iSystemTime, ZCog<Param> iChild) - { return sDelayUntil<Param>(iSystemTime) * iChild; } +ZCog<Param> sStopAt(ZTime iSystemTime, ZCog<Param> iCog) + { return sDelayUntil<Param>(iSystemTime) * iCog; } template <class Param> -ZCog<Param> sStartAfter(double iDelay, ZCog<Param> iChild) - { return sDelayFor<Param>(iDelay) | iChild; } +ZCog<Param> sStartAfter(double iDelay, ZCog<Param> iCog) + { return sDelayFor<Param>(iDelay) | iCog; } template <class Param> -ZCog<Param> sStopAfter(double iDelay, ZCog<Param> iChild) - { return sDelayFor<Param>(iDelay) * iChild; } +ZCog<Param> sStopAfter(double iDelay, ZCog<Param> iCog) + { return sDelayFor<Param>(iDelay) * iCog; } } // namespace ZooLib This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-07-28 20:29:24
|
Revision: 2434 http://zoolib.svn.sourceforge.net/zoolib/?rev=2434&view=rev Author: agreen Date: 2011-07-28 20:29:18 +0000 (Thu, 28 Jul 2011) Log Message: ----------- Add sOnce. Some cleanup. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-07-28 20:28:16 UTC (rev 2433) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-07-28 20:29:18 UTC (rev 2434) @@ -61,25 +61,25 @@ //-- - ZCog(const inherited& iOther) - : inherited(iOther) + ZCog(Callable* iCallable) + : inherited(iCallable) {} - - ZCog& operator=(const inherited& iOther) + + ZCog& operator=(Callable* iCallable) { - inherited::operator=(iOther); + inherited::operator=(iCallable); return *this; } //-- - ZCog(Callable* iCallable) - : inherited(iCallable) + ZCog(const ZRef<Callable>& iOther) + : inherited(iOther) {} - - ZCog& operator=(Callable* iCallable) + + ZCog& operator=(const ZRef<Callable>& iOther) { - inherited::operator=(iCallable); + inherited::operator=(iOther); return *this; } @@ -262,6 +262,23 @@ // ================================================================================================= #pragma mark - +#pragma mark * sOnce + +template <class Param> +ZCog<Param> sOnce_Fun(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> iCog) + { + if (iCog) + iCog->Call(iCog, iParam); + return null; + } + +template <class Param> +ZCog<Param> sOnce(ZCog<Param> iCog) + { return sBindR(sCallable(sOnce_Fun<Param>), iCog); } + +// ================================================================================================= +#pragma mark - #pragma mark * sRepeat template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-08-02 16:00:45
|
Revision: 2444 http://zoolib.svn.sourceforge.net/zoolib/?rev=2444&view=rev Author: agreen Date: 2011-08-02 16:00:40 +0000 (Tue, 02 Aug 2011) Log Message: ----------- Don't need to be explicit about the return type. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-08-02 15:58:51 UTC (rev 2443) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-08-02 16:00:40 UTC (rev 2444) @@ -104,12 +104,12 @@ template <class Param> ZCog<Param> operator() (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const - { return ZCog<Param>(iCallable); } + { return iCallable; } template <class Param> ZCog<Param> operator() (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const - { return ZCog<Param>(iCallable); } + { return iCallable; } template <class Param> ZCog<Param> operator() @@ -119,12 +119,12 @@ template <class Param> ZCog<Param> operator& (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const - { return ZCog<Param>(iCallable); } + { return iCallable; } template <class Param> ZCog<Param> operator& (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const - { return ZCog<Param>(iCallable); } + { return iCallable; } template <class Param> ZCog<Param> operator& This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-08-23 12:05:32
|
Revision: 2452 http://zoolib.svn.sourceforge.net/zoolib/?rev=2452&view=rev Author: agreen Date: 2011-08-23 12:05:26 +0000 (Tue, 23 Aug 2011) Log Message: ----------- Some addl/replacement names. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-08-23 12:04:33 UTC (rev 2451) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-08-23 12:05:26 UTC (rev 2452) @@ -97,6 +97,14 @@ // ================================================================================================= #pragma mark - +#pragma mark * sCallCog + +template <class Cog> +Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) + { return sCall(iCog, iCog, iParam); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCog const struct @@ -135,7 +143,7 @@ // ================================================================================================= #pragma mark - -#pragma mark * sAny +#pragma mark * sAny/sEither template <class Param> ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, @@ -154,6 +162,10 @@ } template <class Param> +ZCog<Param> sEither(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sAny(iCog0, iCog1); } + +template <class Param> ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { @@ -171,16 +183,16 @@ } template <class Param> -ZCog<Param> operator&(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> operator+(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { return sAny(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator&=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return iCog0 = sAny(iCog0, iCog1); } +ZCog<Param>& operator+=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) + { return ioCog0 = sAny(ioCog0, iCog1); } // ================================================================================================= #pragma mark - -#pragma mark * sAll +#pragma mark * sAll/sBoth template <class Param> ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, @@ -195,6 +207,10 @@ } template <class Param> +ZCog<Param> sBoth(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sAll(iCog0, iCog1); } + +template <class Param> ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { @@ -216,12 +232,12 @@ { return sAll(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator*=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return iCog0 = sAll(iCog0, iCog1); } +ZCog<Param>& operator*=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) + { return ioCog0 = sAll(ioCog0, iCog1); } // ================================================================================================= #pragma mark - -#pragma mark * sSeq +#pragma mark * sSeq/sEach template <class Param> ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, @@ -240,6 +256,10 @@ } template <class Param> +ZCog<Param> sEach(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { return sSeq(iCog0, iCog1); } + +template <class Param> ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { @@ -257,8 +277,8 @@ { return sSeq(iCog0, iCog1); } template <class Param> -ZCog<Param>& operator|=(ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return iCog0 = sSeq(iCog0, iCog1); } +ZCog<Param>& operator|=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) + { return ioCog0 = sSeq(ioCog0, iCog1); } // ================================================================================================= #pragma mark - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-01 17:04:33
|
Revision: 2459 http://zoolib.svn.sourceforge.net/zoolib/?rev=2459&view=rev Author: agreen Date: 2011-09-01 17:04:26 +0000 (Thu, 01 Sep 2011) Log Message: ----------- Make more functions able to take a ZRef<ZCallable... rather than an actual ZCog, so there are fewer situations where we have to use sCog. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-01 16:50:49 UTC (rev 2458) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-01 17:04:26 UTC (rev 2459) @@ -103,6 +103,17 @@ Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) { return sCall(iCog, iCog, iParam); } +template <class Param> +ZCog<Param> sCallCog + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable, Param iParam) + { return sCall(iCallable, iCallable, iParam); } + +template <class Param> +ZCog<const Param&> sCallCog + (const ZRef<ZCallable<ZCog<const Param&>(const ZCog<const Param&>&,const Param&)> >& iCallable, + const Param& iParam) + { return sCall(iCallable, iCallable, iParam); } + // ================================================================================================= #pragma mark - #pragma mark * sCog @@ -120,7 +131,7 @@ { return iCallable; } template <class Param> - ZCog<Param> operator() + const ZCog<Param>& operator() (const ZCog<Param>& iCog) const { return iCog; } @@ -135,7 +146,7 @@ { return iCallable; } template <class Param> - ZCog<Param> operator& + const ZCog<Param>& operator& (const ZCog<Param>& iCog) const { return iCog; } @@ -143,30 +154,26 @@ // ================================================================================================= #pragma mark - -#pragma mark * sAny/sEither +#pragma mark * sEither template <class Param> -ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sEither_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sAny(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sEither(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0) { if (iCog1) - return sBindR(sCallable(sAny_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sEither_Fun<Param>), iCog0, iCog1); return iCog0; } return iCog1; } template <class Param> -ZCog<Param> sEither(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sAny(iCog0, iCog1); } - -template <class Param> -ZCog<Param> sAny_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sEither_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -179,39 +186,39 @@ iCog1 = iCog1->Call(iCog1, iParam); } - return sAny(iCog0, iCog1); + return sEither(iCog0, iCog1); } template <class Param> -ZCog<Param> operator+(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sAny(iCog0, iCog1); } +ZCog<Param> operator+ + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sEither<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator+=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) - { return ioCog0 = sAny(ioCog0, iCog1); } +ZCog<Param>& operator+= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sEither<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sAll/sBoth +#pragma mark * sBoth template <class Param> -ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sBoth_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sAll(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sBoth(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0 && iCog1) - return sBindR(sCallable(sAll_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sBoth_Fun<Param>), iCog0, iCog1); return null; } template <class Param> -ZCog<Param> sBoth(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sAll(iCog0, iCog1); } - -template <class Param> -ZCog<Param> sAll_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sBoth_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -224,43 +231,43 @@ iCog1 = iCog1->Call(iCog1, iParam); } - return sAll(iCog0, iCog1); + return sBoth(iCog0, iCog1); } template <class Param> -ZCog<Param> operator*(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sAll(iCog0, iCog1); } +ZCog<Param> operator* + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sBoth<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator*=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) - { return ioCog0 = sAll(ioCog0, iCog1); } +ZCog<Param>& operator*= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sBoth<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sSeq/sEach +#pragma mark * sEach template <class Param> -ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sEach_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sSeq(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sEach(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0) { if (iCog1) - return sBindR(sCallable(sSeq_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sEach_Fun<Param>), iCog0, iCog1); return iCog0; } return iCog1; } template <class Param> -ZCog<Param> sEach(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sSeq(iCog0, iCog1); } - -template <class Param> -ZCog<Param> sSeq_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sEach_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -269,16 +276,20 @@ if (!iCog0 && iCog1) iCog1 = iCog1->Call(iCog1, iParam); - return sSeq(iCog0, iCog1); + return sEach(iCog0, iCog1); } template <class Param> -ZCog<Param> operator|(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { return sSeq(iCog0, iCog1); } +ZCog<Param> operator| + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sEach<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator|=(ZCog<Param>& ioCog0, const ZCog<Param>& iCog1) - { return ioCog0 = sSeq(ioCog0, iCog1); } +ZCog<Param>& operator|= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sEach<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - @@ -288,14 +299,13 @@ ZCog<Param> sOnce_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog) { - if (iCog) - iCog->Call(iCog, iParam); + sCallCog(iCog, iParam); return null; } template <class Param> -ZCog<Param> sOnce(ZCog<Param> iCog) - { return sBindR(sCallable(sOnce_Fun<Param>), iCog); } +ZCog<Param> sOnce(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sBindR(sCallable(sOnce_Fun<Param>), iCallable); } // ================================================================================================= #pragma mark - @@ -315,8 +325,8 @@ } template <class Param> -ZCog<Param> sRepeat(ZCog<Param> iCog) - { return sBindR(sCallable(sRepeat_Fun<Param>), iCog, iCog); } +ZCog<Param> sRepeat(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sBindR(sCallable(sRepeat_Fun<Param>), iCallable, iCallable); } // ================================================================================================= #pragma mark - @@ -342,8 +352,9 @@ } template <class Param> -ZCog<Param> sRepeat(size_t iCount, ZCog<Param> iCog) - { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog, iCog); } +ZCog<Param> sRepeat(size_t iCount, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCallable, iCallable); } // ================================================================================================= #pragma mark - @@ -369,11 +380,7 @@ template <class Param> ZCog<Param> sDelayFor_Fun(const ZCog<Param>& iSelf, Param iParam, double iDelay) - { - if (ZCog<Param> theCog = sDelayUntil<Param>(ZTime::sSystem() + iDelay)) - return theCog->Call(theCog, iParam); - return null; - } + { return sCallCog(sDelayUntil<Param>(ZTime::sSystem() + iDelay), iParam); } template <class Param> ZCog<Param> sDelayFor(double iDelay) @@ -384,19 +391,23 @@ #pragma mark * sStartAt, sStopAt, sStartAfter, sStopAfter template <class Param> -ZCog<Param> sStartAt(ZTime iSystemTime, ZCog<Param> iCog) +ZCog<Param> sStartAt(ZTime iSystemTime, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) { return sDelayUntil<Param>(iSystemTime) | iCog; } template <class Param> -ZCog<Param> sStopAt(ZTime iSystemTime, ZCog<Param> iCog) +ZCog<Param> sStopAt(ZTime iSystemTime, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) { return sDelayUntil<Param>(iSystemTime) * iCog; } template <class Param> -ZCog<Param> sStartAfter(double iDelay, ZCog<Param> iCog) +ZCog<Param> sStartAfter(double iDelay, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) { return sDelayFor<Param>(iDelay) | iCog; } template <class Param> -ZCog<Param> sStopAfter(double iDelay, ZCog<Param> iCog) +ZCog<Param> sStopAfter(double iDelay, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) { return sDelayFor<Param>(iDelay) * iCog; } } // namespace ZooLib This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-06 17:30:17
|
Revision: 2471 http://zoolib.svn.sourceforge.net/zoolib/?rev=2471&view=rev Author: agreen Date: 2011-09-06 17:30:07 +0000 (Tue, 06 Sep 2011) Log Message: ----------- Put sCog prefix on cog-creating template functions. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-06 17:16:16 UTC (rev 2470) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-06 17:30:07 UTC (rev 2471) @@ -157,23 +157,23 @@ #pragma mark * sEither template <class Param> -ZCog<Param> sEither_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Either_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sEither(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Either(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0) { if (iCog1) - return sBindR(sCallable(sEither_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sCog_Either_Fun<Param>), iCog0, iCog1); return iCog0; } return iCog1; } template <class Param> -ZCog<Param> sEither_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Either_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -186,39 +186,39 @@ iCog1 = iCog1->Call(iCog1, iParam); } - return sEither(iCog0, iCog1); + return sCog_Either(iCog0, iCog1); } template <class Param> ZCog<Param> operator+ (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sEither<Param>(iCallable0, iCallable1); } + { return sCog_Either<Param>(iCallable0, iCallable1); } template <class Param> ZCog<Param>& operator+= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sEither<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_Either<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sBoth +#pragma mark * sCog_Both template <class Param> -ZCog<Param> sBoth_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Both_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sBoth(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Both(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0 && iCog1) - return sBindR(sCallable(sBoth_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sCog_Both_Fun<Param>), iCog0, iCog1); return null; } template <class Param> -ZCog<Param> sBoth_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Both_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -231,43 +231,43 @@ iCog1 = iCog1->Call(iCog1, iParam); } - return sBoth(iCog0, iCog1); + return sCog_Both(iCog0, iCog1); } template <class Param> ZCog<Param> operator* (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sBoth<Param>(iCallable0, iCallable1); } + { return sCog_Both<Param>(iCallable0, iCallable1); } template <class Param> ZCog<Param>& operator*= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sBoth<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_Both<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sEach +#pragma mark * sCog_Each template <class Param> -ZCog<Param> sEach_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Each_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sEach(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Each(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { if (iCog0) { if (iCog1) - return sBindR(sCallable(sEach_Fun<Param>), iCog0, iCog1); + return sBindR(sCallable(sCog_Each_Fun<Param>), iCog0, iCog1); return iCog0; } return iCog1; } template <class Param> -ZCog<Param> sEach_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Each_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1) { if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) @@ -276,27 +276,27 @@ if (!iCog0 && iCog1) iCog1 = iCog1->Call(iCog1, iParam); - return sEach(iCog0, iCog1); + return sCog_Each(iCog0, iCog1); } template <class Param> ZCog<Param> operator| (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sEach<Param>(iCallable0, iCallable1); } + { return sCog_Each<Param>(iCallable0, iCallable1); } template <class Param> ZCog<Param>& operator|= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sEach<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_Each<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sOnce +#pragma mark * sCog_Once template <class Param> -ZCog<Param> sOnce_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Once_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog) { sCallCog(iCog, iParam); @@ -304,36 +304,36 @@ } template <class Param> -ZCog<Param> sOnce(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sOnce_Fun<Param>), iCallable); } +ZCog<Param> sCog_Once(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sBindR(sCallable(sCog_Once_Fun<Param>), iCallable); } // ================================================================================================= #pragma mark - -#pragma mark * sRepeat +#pragma mark * sCog_Repeat template <class Param> -ZCog<Param> sRepeat_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Repeat_Fun(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog_Init, ZCog<Param> iCog) { if (not iCog || not sCompareAndSet(iCog, iCog->Call(iCog, iParam))) return iSelf; if (iCog) - return sBindR(sCallable(sRepeat_Fun<Param>), iCog_Init, iCog); + return sBindR(sCallable(sCog_Repeat_Fun<Param>), iCog_Init, iCog); - return sBindR(sCallable(sRepeat_Fun<Param>), iCog_Init, iCog_Init); + return sBindR(sCallable(sCog_Repeat_Fun<Param>), iCog_Init, iCog_Init); } template <class Param> -ZCog<Param> sRepeat(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sRepeat_Fun<Param>), iCallable, iCallable); } +ZCog<Param> sCog_Repeat(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sBindR(sCallable(sCog_Repeat_Fun<Param>), iCallable, iCallable); } // ================================================================================================= #pragma mark - -#pragma mark * sRepeat (count) +#pragma mark * sCog_Repeat (count) template <class Param> -ZCog<Param> sRepeat_Count_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_Repeat_Count_Fun(const ZCog<Param>& iSelf, Param iParam, size_t iCount, ZCog<Param> iCog_Init, ZCog<Param> iCog) { if (not iCount) @@ -343,25 +343,25 @@ return iSelf; if (iCog) - return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog_Init, iCog); + return sBindR(sCallable(sCog_Repeat_Count_Fun<Param>), iCount, iCog_Init, iCog); if (--iCount) - return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCog_Init, iCog_Init); + return sBindR(sCallable(sCog_Repeat_Count_Fun<Param>), iCount, iCog_Init, iCog_Init); return null; } template <class Param> -ZCog<Param> sRepeat(size_t iCount, +ZCog<Param> sCog_Repeat(size_t iCount, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sRepeat_Count_Fun<Param>), iCount, iCallable, iCallable); } + { return sBindR(sCallable(sCog_Repeat_Count_Fun<Param>), iCount, iCallable, iCallable); } // ================================================================================================= #pragma mark - -#pragma mark * sDelayUntil +#pragma mark * sCog_DelayUntil template <class Param> -ZCog<Param> sDelayUntil_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_DelayUntil_Fun(const ZCog<Param>& iSelf, Param iParam, ZTime iSystemTime) { if (ZTime::sSystem() > iSystemTime) @@ -370,45 +370,45 @@ } template <class Param> -ZCog<Param> sDelayUntil(ZTime iSystemTime) - { return sBindR(sCallable(sDelayUntil_Fun<Param>), iSystemTime); } +ZCog<Param> sCog_DelayUntil(ZTime iSystemTime) + { return sBindR(sCallable(sCog_DelayUntil_Fun<Param>), iSystemTime); } // ================================================================================================= #pragma mark - -#pragma mark * sDelayFor +#pragma mark * sCog_DelayFor template <class Param> -ZCog<Param> sDelayFor_Fun(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> sCog_DelayFor_Fun(const ZCog<Param>& iSelf, Param iParam, double iDelay) - { return sCallCog(sDelayUntil<Param>(ZTime::sSystem() + iDelay), iParam); } + { return sCallCog(sCog_DelayUntil<Param>(ZTime::sSystem() + iDelay), iParam); } template <class Param> -ZCog<Param> sDelayFor(double iDelay) - { return sBindR(sCallable(sDelayFor_Fun<Param>), iDelay); } +ZCog<Param> sCog_DelayFor(double iDelay) + { return sBindR(sCallable(sCog_DelayFor_Fun<Param>), iDelay); } // ================================================================================================= #pragma mark - -#pragma mark * sStartAt, sStopAt, sStartAfter, sStopAfter +#pragma mark * sCog_StartAt, sCog_StopAt, sCog_StartAfter, sCog_StopAfter template <class Param> -ZCog<Param> sStartAt(ZTime iSystemTime, +ZCog<Param> sCog_StartAt(ZTime iSystemTime, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sDelayUntil<Param>(iSystemTime) | iCog; } + { return sCog_DelayUntil<Param>(iSystemTime) | iCog; } template <class Param> -ZCog<Param> sStopAt(ZTime iSystemTime, +ZCog<Param> sCog_StopAt(ZTime iSystemTime, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sDelayUntil<Param>(iSystemTime) * iCog; } + { return sCog_DelayUntil<Param>(iSystemTime) * iCog; } template <class Param> -ZCog<Param> sStartAfter(double iDelay, +ZCog<Param> sCog_StartAfter(double iDelay, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sDelayFor<Param>(iDelay) | iCog; } + { return sCog_DelayFor<Param>(iDelay) | iCog; } template <class Param> -ZCog<Param> sStopAfter(double iDelay, +ZCog<Param> sCog_StopAfter(double iDelay, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sDelayFor<Param>(iDelay) * iCog; } + { return sCog_DelayFor<Param>(iDelay) * iCog; } } // namespace ZooLib This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-15 20:21:54
|
Revision: 2477 http://zoolib.svn.sourceforge.net/zoolib/?rev=2477&view=rev Author: agreen Date: 2011-09-15 20:21:47 +0000 (Thu, 15 Sep 2011) Log Message: ----------- More methods taking a callable rather than an actual cog, making them easier to use in conjunction with sBind et al. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-15 20:21:09 UTC (rev 2476) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-15 20:21:47 UTC (rev 2477) @@ -161,15 +161,17 @@ ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sCog_Either(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Either + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCog0) + if (iCallable0) { - if (iCog1) - return sBindR(sCallable(sCogFun_Either<Param>), iCog0, iCog1); - return iCog0; + if (iCallable1) + return sBindR(sCallable(sCogFun_Either<Param>), iCallable0, iCallable1); + return iCallable0; } - return iCog1; + return iCallable1; } template <class Param> @@ -210,10 +212,12 @@ ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sCog_Both(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Both + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCog0 && iCog1) - return sBindR(sCallable(sCogFun_Both<Param>), iCog0, iCog1); + if (iCallable0 && iCallable1) + return sBindR(sCallable(sCogFun_Both<Param>), iCallable0, iCallable1); return null; } @@ -255,15 +259,17 @@ ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> -ZCog<Param> sCog_Each(const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Each + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCog0) + if (iCallable0) { - if (iCog1) - return sBindR(sCallable(sCogFun_Each<Param>), iCog0, iCog1); - return iCog0; + if (iCallable1) + return sBindR(sCallable(sCogFun_Each<Param>), iCallable0, iCallable1); + return iCallable0; } - return iCog1; + return iCallable1; } template <class Param> @@ -392,23 +398,23 @@ template <class Param> ZCog<Param> sCog_StartAt(ZTime iSystemTime, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sCog_DelayUntil<Param>(iSystemTime) | iCog; } + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_DelayUntil<Param>(iSystemTime) | iCallable; } template <class Param> ZCog<Param> sCog_StopAt(ZTime iSystemTime, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sCog_DelayUntil<Param>(iSystemTime) * iCog; } + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_DelayUntil<Param>(iSystemTime) * iCallable; } template <class Param> ZCog<Param> sCog_StartAfter(double iDelay, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sCog_DelayFor<Param>(iDelay) | iCog; } + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >iCallable) + { return sCog_DelayFor<Param>(iDelay) | iCallable; } template <class Param> ZCog<Param> sCog_StopAfter(double iDelay, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCog) - { return sCog_DelayFor<Param>(iDelay) * iCog; } + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >iCallable) + { return sCog_DelayFor<Param>(iDelay) * iCallable; } } // namespace ZooLib This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-23 19:30:35
|
Revision: 2486 http://zoolib.svn.sourceforge.net/zoolib/?rev=2486&view=rev Author: agreen Date: 2011-09-23 19:30:29 +0000 (Fri, 23 Sep 2011) Log Message: ----------- Add sCallCogChanged and sCallCogUnchanged. Experimental prefix change -- 'l' for input parameters that are changed locally. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-19 20:37:51 UTC (rev 2485) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-23 19:30:29 UTC (rev 2486) @@ -97,7 +97,7 @@ // ================================================================================================= #pragma mark - -#pragma mark * sCallCog +#pragma mark * sCallCog variants template <class Cog> Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) @@ -114,6 +114,25 @@ const Param& iParam) { return sCall(iCallable, iCallable, iParam); } +template <class Cog> +bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) + { + if (ioCog) + { + Cog result = ioCog->Call(ioCog, iParam); + if (ioCog != result) + { + ioCog = result; + return true; + } + } + return false; + } + +template <class Cog> +bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) + { return not sCallCogChanged(ioCog, iParam); } + // ================================================================================================= #pragma mark - #pragma mark * sCog @@ -158,7 +177,7 @@ template <class Param> ZCog<Param> sCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1); + ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> ZCog<Param> sCog_Either @@ -176,19 +195,19 @@ template <class Param> ZCog<Param> sCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1) + ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) + if (sCallCogUnchanged(lCog0, iParam)) { - if (iCog1 && not sCompareAndSet(iCog1, iCog1->Call(iCog1, iParam))) + if (sCallCogUnchanged(lCog1, iParam)) return iSelf; } - else if (iCog1) + else if (lCog1) { - iCog1 = iCog1->Call(iCog1, iParam); + lCog1 = lCog1->Call(lCog1, iParam); } - return sCog_Either(iCog0, iCog1); + return sCog_Either(lCog0, lCog1); } template <class Param> @@ -209,7 +228,7 @@ template <class Param> ZCog<Param> sCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1); + ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> ZCog<Param> sCog_Both @@ -223,19 +242,19 @@ template <class Param> ZCog<Param> sCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1) + ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) + if (sCallCogUnchanged(lCog0, iParam)) { - if (iCog1 && not sCompareAndSet(iCog1, iCog1->Call(iCog1, iParam))) + if (sCallCogUnchanged(lCog1, iParam)) return iSelf; } - else if (iCog0 && iCog1) + else if (lCog0 && lCog1) { - iCog1 = iCog1->Call(iCog1, iParam); + lCog1 = lCog1->Call(lCog1, iParam); } - return sCog_Both(iCog0, iCog1); + return sCog_Both(lCog0, lCog1); } template <class Param> @@ -256,7 +275,7 @@ template <class Param> ZCog<Param> sCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1); + ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> ZCog<Param> sCog_Each @@ -274,15 +293,15 @@ template <class Param> ZCog<Param> sCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1) + ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (iCog0 && not sCompareAndSet(iCog0, iCog0->Call(iCog0, iParam))) + if (lCog0 && not sCompareAndSet(lCog0, lCog0->Call(lCog0, iParam))) return iSelf; - if (!iCog0 && iCog1) - iCog1 = iCog1->Call(iCog1, iParam); + if (not lCog0 && lCog1) + lCog1 = lCog1->Call(lCog1, iParam); - return sCog_Each(iCog0, iCog1); + return sCog_Each(lCog0, lCog1); } template <class Param> @@ -319,13 +338,13 @@ template <class Param> ZCog<Param> sCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog_Init, ZCog<Param> iCog) + ZCog<Param> iCog_Init, ZCog<Param> lCog) { - if (not iCog || not sCompareAndSet(iCog, iCog->Call(iCog, iParam))) + if (sCallCogUnchanged(lCog, iParam)) return iSelf; - if (iCog) - return sBindR(sCallable(sCogFun_Repeat<Param>), iCog_Init, iCog); + if (lCog) + return sBindR(sCallable(sCogFun_Repeat<Param>), iCog_Init, lCog); return sBindR(sCallable(sCogFun_Repeat<Param>), iCog_Init, iCog_Init); } @@ -340,16 +359,16 @@ template <class Param> ZCog<Param> sCogFun_Repeat_Count(const ZCog<Param>& iSelf, Param iParam, - size_t iCount, ZCog<Param> iCog_Init, ZCog<Param> iCog) + size_t iCount, ZCog<Param> iCog_Init, ZCog<Param> lCog) { if (not iCount) return null; - if (iCog && not sCompareAndSet(iCog, iCog->Call(iCog, iParam))) + if (sCallCogUnchanged(lCog, iParam)) return iSelf; - if (iCog) - return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCog_Init, iCog); + if (lCog) + return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCog_Init, lCog); if (--iCount) return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCog_Init, iCog_Init); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-26 02:38:03
|
Revision: 2494 http://zoolib.svn.sourceforge.net/zoolib/?rev=2494&view=rev Author: agreen Date: 2011-09-26 02:37:57 +0000 (Mon, 26 Sep 2011) Log Message: ----------- Simplify sCallCogChanged. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-26 02:37:06 UTC (rev 2493) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-26 02:37:57 UTC (rev 2494) @@ -116,26 +116,15 @@ template <class Cog> bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) - { - if (ioCog) - { - Cog result = ioCog->Call(ioCog, iParam); - if (ioCog != result) - { - ioCog = result; - return true; - } - } - return false; - } + { return ioCog && sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } template <class Cog> bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) - { return not sCallCogChanged(ioCog, iParam); } + { return not ioCog || not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } // ================================================================================================= #pragma mark - -#pragma mark * sCog +#pragma mark * sCog function and pseudo operator const struct { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-09-30 20:14:35
|
Revision: 2521 http://zoolib.svn.sourceforge.net/zoolib/?rev=2521&view=rev Author: agreen Date: 2011-09-30 20:14:29 +0000 (Fri, 30 Sep 2011) Log Message: ----------- Add sCog_WhileUnchanged. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-29 17:49:56 UTC (rev 2520) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-09-30 20:14:29 UTC (rev 2521) @@ -307,6 +307,51 @@ // ================================================================================================= #pragma mark - +#pragma mark * sCog_WhileUnchanged + +// Call second cog so long as the calling the first cog is unchanged. + +template <class Param> +ZCog<Param> sCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> iCog0, ZCog<Param> iCog1); + +template <class Param> +ZCog<Param> sCog_WhileUnchanged + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + if (iCallable0 && iCallable1) + return sBindR(sCallable(sCogFun_WhileUnchanged<Param>), iCallable0, iCallable1); + return null; + } + +template <class Param> +ZCog<Param> sCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, ZCog<Param> lCog1) + { + if (sCallCogChanged(lCog0, iParam)) + return lCog0; + + if (sCallCogChanged(lCog1, iParam)) + return sCog_WhileUnchanged(lCog0, lCog1); + + return iSelf; + } + +template <class Param> +ZCog<Param> operator% + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sCog_WhileUnchanged<Param>(iCallable0, iCallable1); } + +template <class Param> +ZCog<Param>& operator%= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sCog_WhileUnchanged<Param>(ioCog0, iCallable1); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCog_Once template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-07 17:47:41
|
Revision: 2535 http://zoolib.svn.sourceforge.net/zoolib/?rev=2535&view=rev Author: agreen Date: 2011-10-07 17:47:35 +0000 (Fri, 07 Oct 2011) Log Message: ----------- Add the notion of a terminal cog, one which returns null when called, but is itself distinguishable and thus usable as a marker. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-07 17:46:46 UTC (rev 2534) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-07 17:47:35 UTC (rev 2535) @@ -21,6 +21,8 @@ #ifndef __ZCog__ #define __ZCog__ 1 +#include "zoolib/ZLog.h" + #include "zoolib/ZCallable.h" #include "zoolib/ZCallable_Bind.h" #include "zoolib/ZCallable_Function.h" @@ -115,12 +117,20 @@ { return sCall(iCallable, iCallable, iParam); } template <class Cog> +bool sCallValidCogChanged(Cog& ioCog, const typename Cog::Param iParam) + { return sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +template <class Cog> bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) - { return ioCog && sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + { return ioCog && sCallValidCogChanged(ioCog, iParam); } template <class Cog> +bool sCallValidCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) + { return not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +template <class Cog> bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) - { return not ioCog || not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + { return not ioCog || sCallValidCogUnchanged(ioCog, iParam); } // ================================================================================================= #pragma mark - @@ -162,13 +172,51 @@ // ================================================================================================= #pragma mark - -#pragma mark * sEither +#pragma mark * sCog_Term template <class Param> -ZCog<Param> sCogFun_Either(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Term(const ZCog<Param>& iSelf, Param iParam) + { + // Shouldn't ever call this. + ZLOGTRACE(eDebug); + return null; + } + +template <class Param> +const ZCog<Param>& sCog_Term() + { + static const ZCog<Param> spCog = sCallable(spCogFun_Term<Param>); + return spCog; + } + +template <class Param> +const ZCog<Param>& sCog_Term + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Term<Param>(); } + +template <class Param> +bool sIsTerm(const ZCog<Param>& iCog) + { return sCog_Term<Param>() == iCog; } + +// ================================================================================================= +#pragma mark - +#pragma mark * sCog_Either + +template <class Param> +ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> +ZCog<Param> spCog_Either + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Either<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> ZCog<Param> sCog_Either (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) @@ -176,27 +224,51 @@ if (iCallable0) { if (iCallable1) - return sBindR(sCallable(sCogFun_Either<Param>), iCallable0, iCallable1); + return spCog_Either(iCallable0, iCallable1); return iCallable0; } return iCallable1; } template <class Param> -ZCog<Param> sCogFun_Either(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (sCallCogUnchanged(lCog0, iParam)) + ZAssert(lCog0 && lCog1); + + if (sIsTerm(lCog0)) { - if (sCallCogUnchanged(lCog1, iParam)) + if (sIsTerm(lCog1)) + return lCog0; + if ((lCog1 = lCog1->Call(lCog1, iParam))) + return lCog1; + return lCog0; + } + else if (sCallValidCogUnchanged(lCog0, iParam)) + { + if (sIsTerm(lCog1)) + return lCog0; + if (sCallValidCogUnchanged(lCog1, iParam)) return iSelf; + if (not lCog1 || sIsTerm(lCog1)) + return lCog0; + return spCog_Either(lCog0, lCog1); } - else if (lCog1) + else if (not lCog0) { - lCog1 = lCog1->Call(lCog1, iParam); + if (sIsTerm(lCog1)) + return lCog1; + + return lCog1->Call(lCog1, iParam); } - - return sCog_Either(lCog0, lCog1); + else if (sIsTerm(lCog1) || (lCog1 = lCog1->Call(lCog1, iParam))) + { + return spCog_Either(lCog0, lCog1); + } + else + { + return lCog0; + } } template <class Param> @@ -216,34 +288,79 @@ #pragma mark * sCog_Both template <class Param> -ZCog<Param> sCogFun_Both(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> +ZCog<Param> spCog_Both + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Both<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> ZCog<Param> sCog_Both (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { if (iCallable0 && iCallable1) - return sBindR(sCallable(sCogFun_Both<Param>), iCallable0, iCallable1); + return spCog_Both(iCallable0, iCallable1); return null; } template <class Param> -ZCog<Param> sCogFun_Both(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (sCallCogUnchanged(lCog0, iParam)) + ZAssert(lCog0 && lCog1); + + if (sIsTerm(lCog0)) { - if (sCallCogUnchanged(lCog1, iParam)) + if (not sIsTerm(lCog1) && not lCog1->Call(lCog1, iParam)) + return null; + return lCog0; + } + else if (sCallValidCogUnchanged(lCog0, iParam)) + { + if (sIsTerm(lCog1)) + return lCog1; + if (sCallValidCogUnchanged(lCog1, iParam)) return iSelf; + if (not lCog1) + return null; + if (sIsTerm(lCog1)) + return lCog1; + return spCog_Both(lCog0, lCog1); } - else if (lCog0 && lCog1) + else if (not lCog0) { - lCog1 = lCog1->Call(lCog1, iParam); + return null; } - - return sCog_Both(lCog0, lCog1); + else if (sIsTerm(lCog0)) + { + if (sIsTerm(lCog1)) + return lCog1; + if ((lCog1 = lCog1->Call(lCog1, iParam)) && sIsTerm(lCog1)) + return lCog1; + return null; + } + else if (sIsTerm(lCog1)) + { + return lCog1; + } + else if ((lCog1 = lCog1->Call(lCog1, iParam))) + { + if (sIsTerm(lCog1)) + return lCog1; + return spCog_Both(lCog0, lCog1); + } + else + { + return null; + } } template <class Param> @@ -263,10 +380,20 @@ #pragma mark * sCog_Each template <class Param> -ZCog<Param> sCogFun_Each(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1); template <class Param> +ZCog<Param> spCog_Each + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Each<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> ZCog<Param> sCog_Each (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) @@ -274,23 +401,50 @@ if (iCallable0) { if (iCallable1) - return sBindR(sCallable(sCogFun_Each<Param>), iCallable0, iCallable1); + return spCog_Each(iCallable0, iCallable1); return iCallable0; } return iCallable1; } template <class Param> -ZCog<Param> sCogFun_Each(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (lCog0 && not sCompareAndSet(lCog0, lCog0->Call(lCog0, iParam))) - return iSelf; + ZAssert(lCog0 && lCog1); - if (not lCog0 && lCog1) - lCog1 = lCog1->Call(lCog1, iParam); - - return sCog_Each(lCog0, lCog1); + if (sIsTerm(lCog0)) + { + if (sIsTerm(lCog1)) + return lCog1; + return lCog1->Call(lCog1, iParam); + } + else if (sCallValidCogUnchanged(lCog0, iParam)) + { + if (sIsTerm(lCog1) || sCallValidCogUnchanged(lCog1, iParam)) + return iSelf; + if (not lCog1) + return lCog0; + return spCog_Each(lCog0, lCog1); + } + else if (not lCog0) + { + if (sIsTerm(lCog1)) + return lCog1; + return lCog1->Call(lCog1, iParam); + } + else if (sIsTerm(lCog0)) + { + if (sIsTerm(lCog1)) + return lCog1; + if ((lCog1 = lCog1->Call(lCog1, iParam))) + return spCog_Each(lCog0, lCog1); + return lCog0; + } + else + { + return spCog_Each(lCog0, lCog1); + } } template <class Param> @@ -307,34 +461,155 @@ // ================================================================================================= #pragma mark - +#pragma mark * sCog_FollowedBy + +template <class Param> +ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1); + +template <class Param> +ZCog<Param> spCog_FollowedBy_First + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_FollowedBy_First<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> +ZCog<Param> sCog_FollowedBy_First + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + if (iCallable0) + { + if (iCallable1) + return spCog_FollowedBy_First(iCallable0, iCallable1); + return iCallable0; + } + return null; + } + +template <class Param> +ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1); + +template <class Param> +ZCog<Param> spCog_FollowedBy_Subsequent + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_FollowedBy_Subsequent<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> +ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1) + { + ZAssert(lCog0 && iCog1); + if (sCallValidCogUnchanged(lCog0, iParam)) + return iSelf; + + if (lCog0 && not sIsTerm(lCog0)) + return spCog_FollowedBy_Subsequent(lCog0, iCog1); + + if (not sIsTerm(iCog1)) + return iCog1->Call(iCog1, iParam); + + return iCog1; + } + +template <class Param> +ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1) + { + ZAssert(lCog0 && iCog1); + if (sIsTerm(lCog0)) + { + if (sIsTerm(iCog1)) + return iCog1; + return iCog1->Call(iCog1, iParam); + } + else if ((lCog0 = lCog0->Call(lCog0, iParam))) + { + if (sIsTerm(lCog0)) + { + if (sIsTerm(iCog1)) + return iCog1; + return iCog1->Call(iCog1, iParam); + } + return spCog_FollowedBy_Subsequent(lCog0, iCog1); + } + else + { + return null; + } + } + +template <class Param> +ZCog<Param> operator/ + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sCog_FollowedBy_First<Param>(iCallable0, iCallable1); } + +template <class Param> +ZCog<Param>& operator/= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sCog_FollowedBy_First<Param>(ioCog0, iCallable1); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCog_WhileUnchanged // Call second cog so long as the calling the first cog is unchanged. template <class Param> -ZCog<Param> sCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> iCog0, ZCog<Param> iCog1); template <class Param> +ZCog<Param> spCog_WhileUnchanged + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_WhileUnchanged<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> ZCog<Param> sCog_WhileUnchanged (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { if (iCallable0 && iCallable1) - return sBindR(sCallable(sCogFun_WhileUnchanged<Param>), iCallable0, iCallable1); + return spCog_WhileUnchanged(iCallable0, iCallable1); return null; } template <class Param> -ZCog<Param> sCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, ZCog<Param> lCog0, ZCog<Param> lCog1) { - if (sCallCogChanged(lCog0, iParam)) + ZAssert(lCog0 && lCog1); + + if (sIsTerm(lCog0)) return lCog0; - if (sCallCogChanged(lCog1, iParam)) - return sCog_WhileUnchanged(lCog0, lCog1); + if (sCallValidCogChanged(lCog0, iParam)) + return lCog0; + if (sCallValidCogChanged(lCog1, iParam)) + { + if (lCog1 && not sIsTerm(lCog1)) + return spCog_WhileUnchanged(lCog0, lCog1); + return lCog0; + } + return iSelf; } @@ -355,45 +630,67 @@ #pragma mark * sCog_Once template <class Param> -ZCog<Param> sCogFun_Once(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog) - { - sCallCog(iCog, iParam); - return null; - } - -template <class Param> ZCog<Param> sCog_Once(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sCogFun_Once<Param>), iCallable); } + { return iCallable * sCog_Term<Param>(); } // ================================================================================================= #pragma mark - -#pragma mark * sCog_Repeat +#pragma mark * sCog_While template <class Param> -ZCog<Param> sCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog_Init, ZCog<Param> lCog) +ZCog<Param> sCog_While(const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); + +template <class Param> +ZCog<Param> spCogFun_While(const ZCog<Param>& iSelf, Param iParam, + const ZCog<Param>& iCog_Init, ZCog<Param> lCog) { - if (sCallCogUnchanged(lCog, iParam)) + ZAssert(iCog_Init && lCog); + + if (sCallValidCogUnchanged(lCog, iParam)) return iSelf; - if (lCog) - return sBindR(sCallable(sCogFun_Repeat<Param>), iCog_Init, lCog); + if (not lCog) + return null; - return sBindR(sCallable(sCogFun_Repeat<Param>), iCog_Init, iCog_Init); + if (sIsTerm(lCog)) + return sCog_While(iCog_Init, iCog_Init); + else + return sCog_While(iCog_Init, lCog); } template <class Param> +ZCog<Param> sCog_While(const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) + { + static ZMACRO_auto(spCallable, sCallable(spCogFun_While<Param>)); + return sBindR(spCallable, iCog_Init, iCog); + } + +template <class Param> +ZCog<Param> sCog_While(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_While<Param>(iCallable, iCallable); } + +template <class Param> +ZCog<Param> operator*(const ZCog<Param>& iCog) + { return sCog_While<Param>(iCog); } + +// ================================================================================================= +#pragma mark - +#pragma mark * sCog_Repeat + +template <class Param> ZCog<Param> sCog_Repeat(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sCogFun_Repeat<Param>), iCallable, iCallable); } + { return sCog_While(iCallable | sCog_Term(iCallable)); } // ================================================================================================= #pragma mark - #pragma mark * sCog_Repeat (count) template <class Param> -ZCog<Param> sCogFun_Repeat_Count(const ZCog<Param>& iSelf, Param iParam, - size_t iCount, ZCog<Param> iCog_Init, ZCog<Param> lCog) +ZCog<Param> sCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); + +template <class Param> +ZCog<Param> spCogFun_Repeat_Count(const ZCog<Param>& iSelf, Param iParam, + size_t iCount, const ZCog<Param>& iCog_Init, ZCog<Param> lCog) { if (not iCount) return null; @@ -401,26 +698,33 @@ if (sCallCogUnchanged(lCog, iParam)) return iSelf; - if (lCog) - return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCog_Init, lCog); + if (lCog && not sIsTerm(lCog)) + return sCog_Repeat(iCount, iCog_Init, lCog); if (--iCount) - return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCog_Init, iCog_Init); + return sCog_Repeat(iCount, iCog_Init, iCog_Init); return null; } template <class Param> +ZCog<Param> sCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) + { + static ZMACRO_auto(spCallable, sCallable(spCogFun_Repeat_Count<Param>)); + return sBindR(spCallable, iCount, iCog_Init, iCog); + } + +template <class Param> ZCog<Param> sCog_Repeat(size_t iCount, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sBindR(sCallable(sCogFun_Repeat_Count<Param>), iCount, iCallable, iCallable); } + { return sCog_Repeat<Param>(iCount, iCallable, iCallable); } // ================================================================================================= #pragma mark - #pragma mark * sCog_DelayUntil template <class Param> -ZCog<Param> sCogFun_DelayUntil(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_DelayUntil(const ZCog<Param>& iSelf, Param iParam, ZTime iSystemTime) { if (ZTime::sSystem() > iSystemTime) @@ -430,20 +734,26 @@ template <class Param> ZCog<Param> sCog_DelayUntil(ZTime iSystemTime) - { return sBindR(sCallable(sCogFun_DelayUntil<Param>), iSystemTime); } + { + static ZMACRO_auto(spCallable, sCallable(spCogFun_DelayUntil<Param>)); + return sBindR(spCallable, iSystemTime); + } // ================================================================================================= #pragma mark - #pragma mark * sCog_DelayFor template <class Param> -ZCog<Param> sCogFun_DelayFor(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_DelayFor(const ZCog<Param>& iSelf, Param iParam, double iDelay) { return sCallCog(sCog_DelayUntil<Param>(ZTime::sSystem() + iDelay), iParam); } template <class Param> ZCog<Param> sCog_DelayFor(double iDelay) - { return sBindR(sCallable(sCogFun_DelayFor<Param>), iDelay); } + { + static ZMACRO_auto(spCallable, sCallable(spCogFun_DelayFor<Param>)); + return sBindR(spCallable, iDelay); + } // ================================================================================================= #pragma mark - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-08 21:09:51
|
Revision: 2538 http://zoolib.svn.sourceforge.net/zoolib/?rev=2538&view=rev Author: agreen Date: 2011-10-08 21:09:45 +0000 (Sat, 08 Oct 2011) Log Message: ----------- Fix each, and do possibly dangerous chained call of next cog in repeat. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-07 21:13:26 UTC (rev 2537) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-08 21:09:45 UTC (rev 2538) @@ -421,10 +421,10 @@ } else if (sCallValidCogUnchanged(lCog0, iParam)) { - if (sIsTerm(lCog1) || sCallValidCogUnchanged(lCog1, iParam)) +// if (sIsTerm(lCog1) || sCallValidCogUnchanged(lCog1, iParam)) return iSelf; - if (not lCog1) - return lCog0; +// if (not lCog1) +// return lCog0; return spCog_Each(lCog0, lCog1); } else if (not lCog0) @@ -653,7 +653,7 @@ return null; if (sIsTerm(lCog)) - return sCog_While(iCog_Init, iCog_Init); + return sCallCog(sCog_While(iCog_Init, iCog_Init), iParam); //## Dangerous? else return sCog_While(iCog_Init, lCog); } @@ -702,7 +702,7 @@ return sCog_Repeat(iCount, iCog_Init, lCog); if (--iCount) - return sCog_Repeat(iCount, iCog_Init, iCog_Init); + return sCallCog(sCog_Repeat(iCount, iCog_Init, iCog_Init), iParam); //## Dangerous? return null; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-10 18:54:58
|
Revision: 2542 http://zoolib.svn.sourceforge.net/zoolib/?rev=2542&view=rev Author: agreen Date: 2011-10-10 18:54:52 +0000 (Mon, 10 Oct 2011) Log Message: ----------- Make it safe to call sCallCog with a term. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-10 18:54:20 UTC (rev 2541) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-10 18:54:52 UTC (rev 2542) @@ -21,8 +21,6 @@ #ifndef __ZCog__ #define __ZCog__ 1 -#include "zoolib/ZLog.h" - #include "zoolib/ZCallable.h" #include "zoolib/ZCallable_Bind.h" #include "zoolib/ZCallable_Function.h" @@ -99,41 +97,6 @@ // ================================================================================================= #pragma mark - -#pragma mark * sCallCog variants - -template <class Cog> -Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) - { return sCall(iCog, iCog, iParam); } - -template <class Param> -ZCog<Param> sCallCog - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable, Param iParam) - { return sCall(iCallable, iCallable, iParam); } - -template <class Param> -ZCog<const Param&> sCallCog - (const ZRef<ZCallable<ZCog<const Param&>(const ZCog<const Param&>&,const Param&)> >& iCallable, - const Param& iParam) - { return sCall(iCallable, iCallable, iParam); } - -template <class Cog> -bool sCallValidCogChanged(Cog& ioCog, const typename Cog::Param iParam) - { return sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } - -template <class Cog> -bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) - { return ioCog && sCallValidCogChanged(ioCog, iParam); } - -template <class Cog> -bool sCallValidCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) - { return not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } - -template <class Cog> -bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) - { return not ioCog || sCallValidCogUnchanged(ioCog, iParam); } - -// ================================================================================================= -#pragma mark - #pragma mark * sCog function and pseudo operator const struct @@ -178,7 +141,7 @@ ZCog<Param> spCogFun_Term(const ZCog<Param>& iSelf, Param iParam) { // Shouldn't ever call this. - ZLOGTRACE(eDebug); + ZUnimplemented(); return null; } @@ -195,11 +158,58 @@ { return sCog_Term<Param>(); } template <class Param> -bool sIsTerm(const ZCog<Param>& iCog) - { return sCog_Term<Param>() == iCog; } +bool sIsTerm(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Term<Param>() == iCallable; } // ================================================================================================= #pragma mark - +#pragma mark * sCallCog variants + +template <class Cog> +Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) + { + if (not iCog || sIsTerm(iCog)) + return iCog; + return sCall(iCog, iCog, iParam); + } + +template <class Param> +ZCog<Param> sCallCog + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable, Param iParam) + { + if (not iCallable || sIsTerm(iCallable)) + return iCallable; + return sCall(iCallable, iCallable, iParam); + } + +template <class Param> +ZCog<const Param&> sCallCog + (const ZRef<ZCallable<ZCog<const Param&>(const ZCog<const Param&>&,const Param&)> >& iCallable, + const Param& iParam) + { + if (not iCallable || sIsTerm(iCallable)) + return iCallable; + return sCall(iCallable, iCallable, iParam); + } + +template <class Cog> +bool sCallValidCogChanged(Cog& ioCog, const typename Cog::Param iParam) + { return sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +template <class Cog> +bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) + { return ioCog && not sIsTerm(ioCog) && sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +template <class Cog> +bool sCallValidCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) + { return not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +template <class Cog> +bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) + { return not ioCog || sIsTerm(ioCog) || not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCog_Either template <class Param> @@ -421,11 +431,7 @@ } else if (sCallValidCogUnchanged(lCog0, iParam)) { -// if (sIsTerm(lCog1) || sCallValidCogUnchanged(lCog1, iParam)) - return iSelf; -// if (not lCog1) -// return lCog0; - return spCog_Each(lCog0, lCog1); + return iSelf; } else if (not lCog0) { @@ -510,6 +516,7 @@ ZCog<Param> lCog0, const ZCog<Param>& iCog1) { ZAssert(lCog0 && iCog1); + if (sCallValidCogUnchanged(lCog0, iParam)) return iSelf; @@ -527,6 +534,7 @@ ZCog<Param> lCog0, const ZCog<Param>& iCog1) { ZAssert(lCog0 && iCog1); + if (sIsTerm(lCog0)) { if (sIsTerm(iCog1)) @@ -728,7 +736,8 @@ ZTime iSystemTime) { if (ZTime::sSystem() > iSystemTime) - return null; + return sCog_Term(iSelf); +// return null; return iSelf; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-13 16:53:03
|
Revision: 2550 http://zoolib.svn.sourceforge.net/zoolib/?rev=2550&view=rev Author: agreen Date: 2011-10-13 16:52:57 +0000 (Thu, 13 Oct 2011) Log Message: ----------- Better behavior for repeating cogs. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-12 00:21:14 UTC (rev 2549) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-13 16:52:57 UTC (rev 2550) @@ -605,12 +605,9 @@ { ZAssert(lCog0 && lCog1); - if (sIsTerm(lCog0)) + if (sIsTerm(lCog0) || sCallValidCogChanged(lCog0, iParam)) return lCog0; - if (sCallValidCogChanged(lCog0, iParam)) - return lCog0; - if (sCallValidCogChanged(lCog1, iParam)) { if (lCog1 && not sIsTerm(lCog1)) @@ -654,14 +651,14 @@ { ZAssert(iCog_Init && lCog); - if (sCallValidCogUnchanged(lCog, iParam)) + if (sIsTerm(lCog) || sCallValidCogUnchanged(lCog, iParam)) return iSelf; if (not lCog) return null; if (sIsTerm(lCog)) - return sCallCog(sCog_While(iCog_Init, iCog_Init), iParam); //## Dangerous? + return sCallCog(sCog_While(iCog_Init, iCog_Init), iParam); //## Dangerous else return sCog_While(iCog_Init, lCog); } @@ -675,7 +672,15 @@ template <class Param> ZCog<Param> sCog_While(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sCog_While<Param>(iCallable, iCallable); } + { + if (iCallable) + { + if (sIsTerm(iCallable)) + return iCallable; + return sCog_While<Param>(iCallable, iCallable); + } + return null; + } template <class Param> ZCog<Param> operator*(const ZCog<Param>& iCog) @@ -683,49 +688,44 @@ // ================================================================================================= #pragma mark - -#pragma mark * sCog_Repeat - -template <class Param> -ZCog<Param> sCog_Repeat(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sCog_While(iCallable | sCog_Term(iCallable)); } - -// ================================================================================================= -#pragma mark - #pragma mark * sCog_Repeat (count) template <class Param> -ZCog<Param> sCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); +ZCog<Param> spCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); template <class Param> -ZCog<Param> spCogFun_Repeat_Count(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, size_t iCount, const ZCog<Param>& iCog_Init, ZCog<Param> lCog) { - if (not iCount) - return null; - if (sCallCogUnchanged(lCog, iParam)) return iSelf; - if (lCog && not sIsTerm(lCog)) - return sCog_Repeat(iCount, iCog_Init, lCog); + if (not lCog) + return null; - if (--iCount) - return sCallCog(sCog_Repeat(iCount, iCog_Init, iCog_Init), iParam); //## Dangerous? - - return null; + if (sIsTerm(lCog)) + return sCallCog(spCog_Repeat(iCount - 1, iCog_Init, iCog_Init), iParam); + else + return spCog_Repeat(iCount, iCog_Init, lCog); } template <class Param> -ZCog<Param> sCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) +ZCog<Param> spCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) { - static ZMACRO_auto(spCallable, sCallable(spCogFun_Repeat_Count<Param>)); - return sBindR(spCallable, iCount, iCog_Init, iCog); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Repeat<Param>)); + if (iCount && iCog_Init) + { + if (sIsTerm(iCog_Init)) + return iCog_Init; + return sBindR(spCallable, iCount, iCog_Init, iCog); + } + return null; } template <class Param> ZCog<Param> sCog_Repeat(size_t iCount, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sCog_Repeat<Param>(iCount, iCallable, iCallable); } + { return spCog_Repeat<Param>(iCount, iCallable, iCallable); } // ================================================================================================= #pragma mark - @@ -736,8 +736,7 @@ ZTime iSystemTime) { if (ZTime::sSystem() > iSystemTime) - return sCog_Term(iSelf); -// return null; + return sCog_Term<Param>(); return iSelf; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-17 13:11:20
|
Revision: 2567 http://zoolib.svn.sourceforge.net/zoolib/?rev=2567&view=rev Author: agreen Date: 2011-10-17 13:11:14 +0000 (Mon, 17 Oct 2011) Log Message: ----------- Add ZCogCombiner variants, for use with ZAccumulator_T. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-17 13:10:37 UTC (rev 2566) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-17 13:11:14 UTC (rev 2567) @@ -293,6 +293,18 @@ const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { return ioCog0 = sCog_Either<Param>(ioCog0, iCallable1); } +template <class Param> +struct ZCogCombiner_Either + { + void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const + { + if (io0) + io0 = sCog_Either(io0, i1); + else + io0 = i1; + } + }; + // ================================================================================================= #pragma mark - #pragma mark * sCog_Both @@ -385,6 +397,18 @@ const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { return ioCog0 = sCog_Both<Param>(ioCog0, iCallable1); } +template <class Param> +struct ZCogCombiner_Both + { + void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const + { + if (io0) + io0 = sCog_Both(io0, i1); + else + io0 = i1; + } + }; + // ================================================================================================= #pragma mark - #pragma mark * sCog_Each @@ -465,6 +489,18 @@ const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { return ioCog0 = sCog_Each<Param>(ioCog0, iCallable1); } +template <class Param> +struct ZCogCombiner_Each + { + void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const + { + if (io0) + io0 = sCog_Each(io0, i1); + else + io0 = i1; + } + }; + // ================================================================================================= #pragma mark - #pragma mark * sCog_FollowedBy This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-18 16:15:16
|
Revision: 2574 http://zoolib.svn.sourceforge.net/zoolib/?rev=2574&view=rev Author: agreen Date: 2011-10-18 16:15:07 +0000 (Tue, 18 Oct 2011) Log Message: ----------- Wasn't assigning to io0. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-18 16:14:43 UTC (rev 2573) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-18 16:15:07 UTC (rev 2574) @@ -297,7 +297,7 @@ struct ZCogAccumulatorCombiner_Either { void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const - { sCog_Either(io0, i1); } + { io0 = sCog_Either(io0, i1); } }; // ================================================================================================= This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-21 02:50:04
|
Revision: 2582 http://zoolib.svn.sourceforge.net/zoolib/?rev=2582&view=rev Author: agreen Date: 2011-10-21 02:49:58 +0000 (Fri, 21 Oct 2011) Log Message: ----------- It is cheaper to pass const& of ZRefs, then we only retain/release if we actually need to. sCog_Both doesn't do this yet, as it's a bit more fiddly than the others. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-20 21:47:08 UTC (rev 2581) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-21 02:49:58 UTC (rev 2582) @@ -214,7 +214,7 @@ template <class Param> ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_Either @@ -242,43 +242,53 @@ template <class Param> ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && lCog1); + ZAssert(iCog0 && iCog1); - if (sIsTerm(lCog0)) + if (sIsTerm(iCog0)) { - if (sIsTerm(lCog1)) - return lCog0; - if ((lCog1 = lCog1->Call(lCog1, iParam))) - return lCog1; - return lCog0; + if (sIsTerm(iCog1)) + return iCog0; + if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) + return newCog1; + return iCog0; } - else if (sCallValidCogUnchanged(lCog0, iParam)) + else { - if (sIsTerm(lCog1)) - return lCog0; - if (sCallValidCogUnchanged(lCog1, iParam)) - return iSelf; - if (not lCog1 || sIsTerm(lCog1)) - return lCog0; - return spCog_Either(lCog0, lCog1); - } - else if (not lCog0) - { - if (sIsTerm(lCog1)) - return lCog1; + ZCog<Param> newCog0 = iCog0; + if (sCallValidCogUnchanged(newCog0, iParam)) + { + if (sIsTerm(iCog1)) + return newCog0; + + ZCog<Param> newCog1 = iCog1; + if (sCallValidCogUnchanged(newCog1, iParam)) + return iSelf; + if (not newCog1 || sIsTerm(newCog1)) + return newCog0; + return spCog_Either(newCog0, newCog1); + } + else if (not newCog0) + { + if (sIsTerm(iCog1)) + return iCog1; - return lCog1->Call(lCog1, iParam); + return iCog1->Call(iCog1, iParam); + } + else if (sIsTerm(iCog1)) + { + return spCog_Either(newCog0, iCog1); + } + else if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) + { + return spCog_Either(newCog0, newCog1); + } + else + { + return newCog0; + } } - else if (sIsTerm(lCog1) || (lCog1 = lCog1->Call(lCog1, iParam))) - { - return spCog_Either(lCog0, lCog1); - } - else - { - return lCog0; - } } template <class Param> @@ -306,7 +316,7 @@ template <class Param> ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_Both @@ -330,9 +340,11 @@ template <class Param> ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && lCog1); + ZAssert(iCog0 && iCog1); + ZCog<Param> lCog0 = iCog0; + ZCog<Param> lCog1 = iCog1; if (sIsTerm(lCog0)) { @@ -410,7 +422,7 @@ template <class Param> ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_Each @@ -438,37 +450,41 @@ template <class Param> ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && lCog1); + ZAssert(iCog0 && iCog1); - if (sIsTerm(lCog0)) + if (sIsTerm(iCog0)) { - if (sIsTerm(lCog1)) - return lCog1; - return lCog1->Call(lCog1, iParam); + if (sIsTerm(iCog1)) + return iCog1; + return iCog1->Call(iCog1, iParam); } - else if (sCallValidCogUnchanged(lCog0, iParam)) - { - return iSelf; - } - else if (not lCog0) - { - if (sIsTerm(lCog1)) - return lCog1; - return lCog1->Call(lCog1, iParam); - } - else if (sIsTerm(lCog0)) - { - if (sIsTerm(lCog1)) - return lCog1; - if ((lCog1 = lCog1->Call(lCog1, iParam))) - return spCog_Each(lCog0, lCog1); - return lCog0; - } else { - return spCog_Each(lCog0, lCog1); + ZCog<Param> newCog0 = iCog0; + if (sCallValidCogUnchanged(newCog0, iParam)) + { + return iSelf; + } + else if (not newCog0) + { + if (sIsTerm(iCog1)) + return iCog1; + return iCog1->Call(iCog1, iParam); + } + else if (sIsTerm(newCog0)) + { + if (sIsTerm(iCog1)) + return iCog1; + if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) + return spCog_Each(newCog0, newCog1); + return newCog0; + } + else + { + return spCog_Each(newCog0, iCog1); + } } } @@ -497,7 +513,7 @@ template <class Param> ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, const ZCog<Param>& iCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_FollowedBy_First @@ -525,7 +541,7 @@ template <class Param> ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, const ZCog<Param>& iCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_FollowedBy_Subsequent @@ -539,15 +555,16 @@ template <class Param> ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, const ZCog<Param>& iCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && iCog1); + ZAssert(iCog0 && iCog1); - if (sCallValidCogUnchanged(lCog0, iParam)) + ZCog<Param> newCog0 = iCog0; + if (sCallValidCogUnchanged(newCog0, iParam)) return iSelf; - if (lCog0 && not sIsTerm(lCog0)) - return spCog_FollowedBy_Subsequent(lCog0, iCog1); + if (newCog0 && not sIsTerm(newCog0)) + return spCog_FollowedBy_Subsequent(newCog0, iCog1); if (not sIsTerm(iCog1)) return iCog1->Call(iCog1, iParam); @@ -557,25 +574,25 @@ template <class Param> ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, const ZCog<Param>& iCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && iCog1); + ZAssert(iCog0 && iCog1); - if (sIsTerm(lCog0)) + if (sIsTerm(iCog0)) { if (sIsTerm(iCog1)) return iCog1; return iCog1->Call(iCog1, iParam); } - else if ((lCog0 = lCog0->Call(lCog0, iParam))) + else if (const ZCog<Param> newCog0 = iCog0->Call(iCog0, iParam)) { - if (sIsTerm(lCog0)) + if (sIsTerm(newCog0)) { if (sIsTerm(iCog1)) return iCog1; return iCog1->Call(iCog1, iParam); } - return spCog_FollowedBy_Subsequent(lCog0, iCog1); + return spCog_FollowedBy_Subsequent(newCog0, iCog1); } else { @@ -603,7 +620,7 @@ template <class Param> ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> iCog0, ZCog<Param> iCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_WhileUnchanged @@ -627,18 +644,23 @@ template <class Param> ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, - ZCog<Param> lCog0, ZCog<Param> lCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(lCog0 && lCog1); + ZAssert(iCog0 && iCog1); - if (sIsTerm(lCog0) || sCallValidCogChanged(lCog0, iParam)) - return lCog0; + if (sIsTerm(iCog0)) + return iCog0; - if (sCallValidCogChanged(lCog1, iParam)) + ZCog<Param> newCog0 = iCog0; + if (sCallValidCogChanged(newCog0, iParam)) + return newCog0; + + ZCog<Param> newCog1 = iCog1; + if (sCallValidCogChanged(newCog1, iParam)) { - if (lCog1 && not sIsTerm(lCog1)) - return spCog_WhileUnchanged(lCog0, lCog1); - return lCog0; + if (newCog1 && not sIsTerm(newCog1)) + return spCog_WhileUnchanged(newCog0, newCog1); + return newCog0; } return iSelf; @@ -673,20 +695,24 @@ template <class Param> ZCog<Param> spCogFun_While(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog_Init, ZCog<Param> lCog) + const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) { - ZAssert(iCog_Init && lCog); + ZAssert(iCog_Init && iCog); - if (sIsTerm(lCog) || sCallValidCogUnchanged(lCog, iParam)) + if (sIsTerm(iCog)) return iSelf; - if (not lCog) + ZCog<Param> newCog = iCog; + if (sCallValidCogUnchanged(newCog, iParam)) + return iSelf; + + if (not newCog) return null; - if (sIsTerm(lCog)) + if (sIsTerm(newCog)) return sCallCog(sCog_While(iCog_Init, iCog_Init), iParam); //## Dangerous else - return sCog_While(iCog_Init, lCog); + return sCog_While(iCog_Init, newCog); } template <class Param> @@ -721,18 +747,18 @@ template <class Param> ZCog<Param> spCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, - size_t iCount, const ZCog<Param>& iCog_Init, ZCog<Param> lCog) + size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) { - if (sCallCogUnchanged(lCog, iParam)) + if (sCallCogUnchanged(iCog, iParam)) return iSelf; - if (not lCog) + if (not iCog) return null; - if (sIsTerm(lCog)) + if (sIsTerm(iCog)) return sCallCog(spCog_Repeat(iCount - 1, iCog_Init, iCog_Init), iParam); else - return spCog_Repeat(iCount, iCog_Init, lCog); + return spCog_Repeat(iCount, iCog_Init, iCog); } template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-30 02:56:17
|
Revision: 2599 http://zoolib.svn.sourceforge.net/zoolib/?rev=2599&view=rev Author: agreen Date: 2011-10-30 02:56:11 +0000 (Sun, 30 Oct 2011) Log Message: ----------- Comment. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-30 02:55:40 UTC (rev 2598) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-30 02:56:11 UTC (rev 2599) @@ -99,6 +99,8 @@ #pragma mark - #pragma mark * sCog function and pseudo operator +#if 0 + const struct { template <class Param> @@ -133,6 +135,8 @@ } sCog = {}; +#endif + // ================================================================================================= #pragma mark - #pragma mark * sCog_Term @@ -616,7 +620,7 @@ #pragma mark - #pragma mark * sCog_WhileUnchanged -// Call second cog so long as the calling the first cog is unchanged. +// Call cog1 so long as cog0 is unchanged. template <class Param> ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-10-30 23:05:42
|
Revision: 2600 http://zoolib.svn.sourceforge.net/zoolib/?rev=2600&view=rev Author: agreen Date: 2011-10-30 23:05:36 +0000 (Sun, 30 Oct 2011) Log Message: ----------- WhileUnchanged returns iCallable0 if iCallable1 is null. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-30 02:56:11 UTC (rev 2599) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-10-30 23:05:36 UTC (rev 2600) @@ -641,8 +641,12 @@ (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0 && iCallable1) - return spCog_WhileUnchanged(iCallable0, iCallable1); + if (iCallable0) + { + if (iCallable1) + return spCog_WhileUnchanged(iCallable0, iCallable1); + return iCallable0; + } return null; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-11-17 14:43:44
|
Revision: 2606 http://zoolib.svn.sourceforge.net/zoolib/?rev=2606&view=rev Author: agreen Date: 2011-11-17 14:43:37 +0000 (Thu, 17 Nov 2011) Log Message: ----------- Remove sCog. Add sCog_Not. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-14 15:06:44 UTC (rev 2605) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-17 14:43:37 UTC (rev 2606) @@ -97,48 +97,6 @@ // ================================================================================================= #pragma mark - -#pragma mark * sCog function and pseudo operator - -#if 0 - -const struct - { - template <class Param> - ZCog<Param> operator() - (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const - { return iCallable; } - - template <class Param> - ZCog<Param> operator() - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const - { return iCallable; } - - template <class Param> - const ZCog<Param>& operator() - (const ZCog<Param>& iCog) const - { return iCog; } - - template <class Param> - ZCog<Param> operator& - (ZCallable<ZCog<Param>(const ZCog<Param>&,Param)>* iCallable) const - { return iCallable; } - - template <class Param> - ZCog<Param> operator& - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) const - { return iCallable; } - - template <class Param> - const ZCog<Param>& operator& - (const ZCog<Param>& iCog) const - { return iCog; } - - } sCog = {}; - -#endif - -// ================================================================================================= -#pragma mark - #pragma mark * sCog_Term template <class Param> @@ -214,6 +172,36 @@ // ================================================================================================= #pragma mark - +#pragma mark * sCog_Not + +template <class Param> +ZCog<Param> spCogFun_Not(const ZCog<Param>& iSelf, Param iParam, + const ZCog<Param>& iCog) + { + if (iCog && not sIsTerm(iCog)) + { + const ZCog<Param> newCog = iCog->Call(iCog, iParam); + if (not newCog || sIsTerm(newCog)) + return iCog; + } + return iSelf; + } + +template <class Param> +const ZCog<Param>& sCog_Not + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + static ZMACRO_auto(spCallable, sCallable(spCogFun_Not<Param>)); + return sBindR(spCallable, iCallable); + } + +template <class Param> +ZCog<Param> operator~ + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Not<Param>(iCallable); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCog_Either template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-11-18 20:13:24
|
Revision: 2617 http://zoolib.svn.sourceforge.net/zoolib/?rev=2617&view=rev Author: agreen Date: 2011-11-18 20:13:18 +0000 (Fri, 18 Nov 2011) Log Message: ----------- Add sCog_Applied. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-18 20:12:57 UTC (rev 2616) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-18 20:13:18 UTC (rev 2617) @@ -501,6 +501,111 @@ // ================================================================================================= #pragma mark - +#pragma mark * sCog_Applied + +template <class Param> +ZCog<Param> spCogFun_Applied(const ZCog<Param>& iSelf, Param iParam, + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); + +template <class Param> +ZCog<Param> spCog_Applied + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + ZAssert(iCallable0 && iCallable1); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Applied<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); + } + +template <class Param> +ZCog<Param> sCog_Applied + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { + if (iCallable0) + { + if (iCallable1) + return spCog_Applied(iCallable0, iCallable1); + return iCallable0; + } + return null; + } + +template <class Param> +ZCog<Param> spCogFun_Applied(const ZCog<Param>& iSelf, Param iParam, + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + { + ZAssert(iCog0 && iCog1); + ZCog<Param> lCog0 = iCog0; + ZCog<Param> lCog1 = iCog1; + + if (sIsTerm(lCog0)) + { + if (not sIsTerm(lCog1) && not lCog1->Call(lCog1, iParam)) + return null; + return lCog0; + } + else if (sCallValidCogUnchanged(lCog0, iParam)) + { + if (sIsTerm(lCog1)) + return lCog0; + if (sCallValidCogUnchanged(lCog1, iParam)) + return iSelf; + if (not lCog1) + return lCog0; + if (sIsTerm(lCog1)) + return lCog0; + return spCog_Both(lCog0, lCog1); + } + else if (not lCog0) + { + return null; + } + else if (sIsTerm(lCog0)) + { + if (sIsTerm(lCog1)) + return lCog1; + if ((lCog1 = lCog1->Call(lCog1, iParam)) && sIsTerm(lCog1)) + return lCog1; + return null; + } + else if (sIsTerm(lCog1)) + { + return lCog0; + } + else if ((lCog1 = lCog1->Call(lCog1, iParam))) + { + if (sIsTerm(lCog1)) + return lCog0; + return spCog_Both(lCog0, lCog1); + } + else + { + return null; + } + } + +template <class Param> +ZCog<Param> operator^ + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sCog_Applied<Param>(iCallable0, iCallable1); } + +template <class Param> +ZCog<Param>& operator^= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = sCog_Applied<Param>(ioCog0, iCallable1); } + +template <class Param> +struct ZCogAccumulatorCombiner_Applied + { + void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const + { io0 = sCog_Applied(io0, i1); } + }; + +// ================================================================================================= +#pragma mark - #pragma mark * sCog_FollowedBy template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2011-11-25 20:46:23
|
Revision: 2622 http://zoolib.svn.sourceforge.net/zoolib/?rev=2622&view=rev Author: agreen Date: 2011-11-25 20:46:17 +0000 (Fri, 25 Nov 2011) Log Message: ----------- Fix sCog_Not. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-25 20:39:09 UTC (rev 2621) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2011-11-25 20:46:17 UTC (rev 2622) @@ -178,17 +178,18 @@ ZCog<Param> spCogFun_Not(const ZCog<Param>& iSelf, Param iParam, const ZCog<Param>& iCog) { - if (iCog && not sIsTerm(iCog)) - { - const ZCog<Param> newCog = iCog->Call(iCog, iParam); - if (not newCog || sIsTerm(newCog)) - return iCog; - } - return iSelf; + if (not iCog || sIsTerm(iCog)) + return iSelf; + + const ZCog<Param> newCog = iCog->Call(iCog, iParam); + if (not newCog || sIsTerm(newCog)) + return iSelf; + + return null; } template <class Param> -const ZCog<Param>& sCog_Not +ZCog<Param> sCog_Not (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) { static ZMACRO_auto(spCallable, sCallable(spCogFun_Not<Param>)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2012-01-05 21:45:45
|
Revision: 2657 http://zoolib.svn.sourceforge.net/zoolib/?rev=2657&view=rev Author: agreen Date: 2012-01-05 21:45:39 +0000 (Thu, 05 Jan 2012) Log Message: ----------- Finish sCog_Applied implementation (was just a clone of Both before). Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2012-01-05 21:45:12 UTC (rev 2656) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2012-01-05 21:45:39 UTC (rev 2657) @@ -506,16 +506,17 @@ template <class Param> ZCog<Param> spCogFun_Applied(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1_Init, const ZCog<Param>& iCog1); template <class Param> ZCog<Param> spCog_Applied (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1_Init, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCallable0 && iCallable1); + ZAssert(iCallable0 && iCallable1_Init && iCallable1); static ZMACRO_auto(spCallable, sCallable(spCogFun_Applied<Param>)); - return sBindR(spCallable, iCallable0, iCallable1); + return sBindR(spCallable, iCallable0, iCallable1_Init, iCallable1); } template <class Param> @@ -525,8 +526,8 @@ { if (iCallable0) { - if (iCallable1) - return spCog_Applied(iCallable0, iCallable1); + if (iCallable1 && not sIsTerm(iCallable1)) + return spCog_Applied(iCallable0, iCallable1, iCallable1); return iCallable0; } return null; @@ -534,29 +535,32 @@ template <class Param> ZCog<Param> spCogFun_Applied(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) + const ZCog<Param>& iCog0, const ZCog<Param>& iCog1_Init, const ZCog<Param>& iCog1) { ZAssert(iCog0 && iCog1); + ZAssert(not sIsTerm(iCog1)); + ZCog<Param> lCog0 = iCog0; ZCog<Param> lCog1 = iCog1; if (sIsTerm(lCog0)) { - if (not sIsTerm(lCog1) && not lCog1->Call(lCog1, iParam)) + if (not lCog1->Call(lCog1, iParam)) return null; return lCog0; } else if (sCallValidCogUnchanged(lCog0, iParam)) { - if (sIsTerm(lCog1)) - return lCog0; if (sCallValidCogUnchanged(lCog1, iParam)) return iSelf; + if (not lCog1) return lCog0; + if (sIsTerm(lCog1)) - return lCog0; - return spCog_Both(lCog0, lCog1); + return spCog_Applied(lCog0, iCog1_Init, iCog1_Init); + + return spCog_Applied(lCog0, iCog1_Init, lCog1); } else if (not lCog0) { @@ -564,26 +568,21 @@ } else if (sIsTerm(lCog0)) { - if (sIsTerm(lCog1)) - return lCog1; - if ((lCog1 = lCog1->Call(lCog1, iParam)) && sIsTerm(lCog1)) - return lCog1; - return null; - } - else if (sIsTerm(lCog1)) - { + lCog1->Call(lCog1, iParam); return lCog0; } - else if ((lCog1 = lCog1->Call(lCog1, iParam))) + else { - if (sIsTerm(lCog1)) + lCog1 = lCog1->Call(lCog1, iParam); + + if (not lCog1) return lCog0; - return spCog_Both(lCog0, lCog1); + + if (sIsTerm(lCog1)) + return spCog_Applied(lCog0, iCog1_Init, iCog1_Init); + + return spCog_Applied(lCog0, iCog1_Init, lCog1); } - else - { - return null; - } } template <class Param> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ag...@us...> - 2012-01-09 20:05:44
|
Revision: 2662 http://zoolib.svn.sourceforge.net/zoolib/?rev=2662&view=rev Author: agreen Date: 2012-01-09 20:05:33 +0000 (Mon, 09 Jan 2012) Log Message: ----------- A fairly big rework, to conform more closely to the conventions of ternary logic. Modified Paths: -------------- trunk/zoolib/source/cxx/zoolib/ZCog.h Modified: trunk/zoolib/source/cxx/zoolib/ZCog.h =================================================================== --- trunk/zoolib/source/cxx/zoolib/ZCog.h 2012-01-06 04:11:50 UTC (rev 2661) +++ trunk/zoolib/source/cxx/zoolib/ZCog.h 2012-01-09 20:05:33 UTC (rev 2662) @@ -93,14 +93,29 @@ inherited::operator=(iNull); return *this; } + +//-- + + ZCog(const bool iBool) + : inherited(iBool ? sTrue() : null) + {} + + ZCog& operator=(const bool iBool) + { + inherited::operator=(iBool ? sTrue() : null); + return *this; + } + +//-- + + static ZCog sFalse() + { return null; } + + static const ZCog& sTrue(); }; -// ================================================================================================= -#pragma mark - -#pragma mark * sCog_Term - template <class Param> -ZCog<Param> spCogFun_Term(const ZCog<Param>& iSelf, Param iParam) +ZCog<Param> spCogFun_True(const ZCog<Param>& iSelf, Param iParam) { // Shouldn't ever call this. ZUnimplemented(); @@ -108,40 +123,66 @@ } template <class Param> -const ZCog<Param>& sCog_Term() +const ZCog<Param>& ZCog<Param>::sTrue() { - static const ZCog<Param> spCog = sCallable(spCogFun_Term<Param>); + static const ZCog<Param> spCog = sCallable(spCogFun_True<Param>); return spCog; } +// ================================================================================================= +#pragma mark - +#pragma mark * Nullary, sCog_Unknown + template <class Param> -const ZCog<Param>& sCog_Term - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sCog_Term<Param>(); } +ZCog<Param> spCogFun_Unknown(const ZCog<Param>& iSelf, Param iParam) + { return iSelf; } template <class Param> -bool sIsTerm(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return sCog_Term<Param>() == iCallable; } +const ZCog<Param>& sCog_Unknown() + { + static const ZCog<Param> spCog = sCallable(spCogFun_Unknown<Param>); + return spCog; + } // ================================================================================================= #pragma mark - +#pragma mark * Cog classification + +template <class Param> +bool sIsTrue(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return ZCog<Param>::sTrue() == iCallable; } + +template <class Param> +bool sIsFalse(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return not iCallable; } + +template <class Param> +bool sIsFinished(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return not iCallable || sIsTrue(iCallable); } + +template <class Param> +bool sIsPending(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return iCallable && not sIsTrue(iCallable); } + +// ================================================================================================= +#pragma mark - #pragma mark * sCallCog variants template <class Cog> Cog sCallCog(const Cog& iCog, const typename Cog::Param iParam) { - if (not iCog || sIsTerm(iCog)) + if (sIsFinished(iCog)) return iCog; - return sCall(iCog, iCog, iParam); + return iCog->Call(iCog, iParam); } template <class Param> ZCog<Param> sCallCog (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable, Param iParam) { - if (not iCallable || sIsTerm(iCallable)) + if (sIsFinished(iCallable)) return iCallable; - return sCall(iCallable, iCallable, iParam); + return iCallable->Call(iCallable, iParam); } template <class Param> @@ -149,551 +190,586 @@ (const ZRef<ZCallable<ZCog<const Param&>(const ZCog<const Param&>&,const Param&)> >& iCallable, const Param& iParam) { - if (not iCallable || sIsTerm(iCallable)) + if (sIsFinished(iCallable)) return iCallable; - return sCall(iCallable, iCallable, iParam); + return iCallable->Call(iCallable, iParam); } template <class Cog> -bool sCallValidCogChanged(Cog& ioCog, const typename Cog::Param iParam) +bool sCallPendingCog_Changed(Cog& ioCog, const typename Cog::Param iParam) { return sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } template <class Cog> -bool sCallCogChanged(Cog& ioCog, const typename Cog::Param iParam) - { return ioCog && not sIsTerm(ioCog) && sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } +bool sCallCog_Changed(Cog& ioCog, const typename Cog::Param iParam) + { return sIsPending(ioCog) && sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } template <class Cog> -bool sCallValidCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) +bool sCallPendingCog_Unchanged(Cog& ioCog, const typename Cog::Param iParam) { return not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } template <class Cog> -bool sCallCogUnchanged(Cog& ioCog, const typename Cog::Param iParam) - { return not ioCog || sIsTerm(ioCog) || not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } +bool sCallCog_Unchanged(Cog& ioCog, const typename Cog::Param iParam) + { return sIsFinished(ioCog) || not sCompareAndSet(ioCog, ioCog->Call(ioCog, iParam)); } // ================================================================================================= #pragma mark - -#pragma mark * sCog_Not +#pragma mark * Unary, sCog_Not template <class Param> +ZCog<Param> spCog_Not + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable); + +template <class Param> ZCog<Param> spCogFun_Not(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog) + ZCog<Param> lCog) { - if (not iCog || sIsTerm(iCog)) + if (sCallPendingCog_Unchanged(lCog, iParam)) return iSelf; - const ZCog<Param> newCog = iCog->Call(iCog, iParam); - if (not newCog || sIsTerm(newCog)) - return iSelf; + if (not lCog) + return true; - return null; + if (sIsTrue(lCog)) + return false; + + return spCog_Not(lCog); } template <class Param> -ZCog<Param> sCog_Not +ZCog<Param> spCog_Not (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) { + ZAssert(iCallable && not sIsTrue(iCallable)); static ZMACRO_auto(spCallable, sCallable(spCogFun_Not<Param>)); - return sBindR(spCallable, iCallable); + return sBindR(spCallable, iCallable); } template <class Param> +ZCog<Param> sCog_Not + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + if (not iCallable) + return true; + + if (sIsTrue(iCallable)) + return false; + + return spCog_Not(iCallable); + } + +template <class Param> ZCog<Param> operator~ (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) { return sCog_Not<Param>(iCallable); } // ================================================================================================= #pragma mark - -#pragma mark * sCog_Either +#pragma mark * Unary, sCog_Tautology (TrueIfFinished) template <class Param> -ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); +ZCog<Param> spCog_Tautology + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable); template <class Param> -ZCog<Param> spCog_Either - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) +ZCog<Param> spCogFun_Tautology(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog) { - ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_Either<Param>)); - return sBindR(spCallable, iCallable0, iCallable1); + if (sCallPendingCog_Unchanged(lCog, iParam)) + return iSelf; + + if (not lCog || sIsTrue(lCog)) + return true; + + return spCog_Tautology(lCog); } template <class Param> -ZCog<Param> sCog_Either - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) +ZCog<Param> spCog_Tautology + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) { - if (iCallable0) - { - if (iCallable1) - return spCog_Either(iCallable0, iCallable1); - return iCallable0; - } - return iCallable1; + ZAssert(iCallable && not sIsTrue(iCallable)); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Tautology<Param>)); + return sBindR(spCallable, iCallable); } template <class Param> -ZCog<Param> spCogFun_Either(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> sCog_Tautology + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) { - ZAssert(iCog0 && iCog1); - - if (sIsTerm(iCog0)) - { - if (sIsTerm(iCog1)) - return iCog0; - if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) - return newCog1; - return iCog0; - } - else - { - ZCog<Param> newCog0 = iCog0; - if (sCallValidCogUnchanged(newCog0, iParam)) - { - if (sIsTerm(iCog1)) - return newCog0; - - ZCog<Param> newCog1 = iCog1; - if (sCallValidCogUnchanged(newCog1, iParam)) - return iSelf; - if (not newCog1 || sIsTerm(newCog1)) - return newCog0; - return spCog_Either(newCog0, newCog1); - } - else if (not newCog0) - { - if (sIsTerm(iCog1)) - return iCog1; + if (not iCallable || sIsTrue(iCallable)) + return true; - return iCog1->Call(iCog1, iParam); - } - else if (sIsTerm(iCog1)) - { - return spCog_Either(newCog0, iCog1); - } - else if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) - { - return spCog_Either(newCog0, newCog1); - } - else - { - return newCog0; - } - } + return spCog_Tautology(iCallable); } template <class Param> -ZCog<Param> operator+ - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_Either<Param>(iCallable0, iCallable1); } +ZCog<Param> operator+(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Tautology<Param>(iCallable); } +// ================================================================================================= +#pragma mark - +#pragma mark * Unary, sCog_Contradiction (FalseIfFinished) + template <class Param> -ZCog<Param>& operator+= - (ZCog<Param>& ioCog0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_Either<Param>(ioCog0, iCallable1); } +ZCog<Param> spCog_Contradiction + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable); template <class Param> -struct ZCogAccumulatorCombiner_Either +ZCog<Param> spCogFun_Contradiction(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog) { - void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const - { io0 = sCog_Either(io0, i1); } - }; + if (sCallPendingCog_Unchanged(lCog, iParam)) + return iSelf; + if (not lCog || sIsTrue(lCog)) + return false; + + return spCog_Contradiction(lCog); + } + +template <class Param> +ZCog<Param> spCog_Contradiction + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + ZAssert(iCallable && not sIsTrue(iCallable)); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Contradiction<Param>)); + return sBindR(spCallable, iCallable); + } + +template <class Param> +ZCog<Param> sCog_Contradiction + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + if (not iCallable || sIsTrue(iCallable)) + return false; + + return spCog_Contradiction(iCallable); + } + +template <class Param> +ZCog<Param> operator-(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Contradiction<Param>(iCallable); } + // ================================================================================================= #pragma mark - -#pragma mark * sCog_Both +#pragma mark * Unary, sCog_Repeat template <class Param> -ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); +ZCog<Param> spCog_Repeat + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable_Init, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable); template <class Param> -ZCog<Param> spCog_Both +ZCog<Param> spCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, + const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) + { + ZCog<Param> lCog = iCog; + + if (sCallPendingCog_Unchanged(lCog, iParam)) + return iSelf; + + if (not lCog) + return false; + + if (not sIsTrue(lCog)) + return spCog_Repeat(iCog_Init, lCog); + + // To get unbroken repetition on hitting a term we need to call the new cog, + // don't make the call if we'd just be calling an iSelf-equivalent. + if (iCog_Init == iCog) + return iSelf; + else + return sCallCog(spCog_Repeat(iCog_Init, iCog_Init), iParam); + } + +template <class Param> +ZCog<Param> spCog_Repeat + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable_Init, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + ZAssert(iCallable_Init && iCallable); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Repeat<Param>)); + return sBindR(spCallable, iCallable_Init, iCallable); + } + +template <class Param> +ZCog<Param> sCog_Repeat + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { + if (not iCallable) + return false; + + if (sIsTrue(iCallable)) + return sCog_Unknown<Param>(); + + return spCog_Repeat(iCallable, iCallable); + } + +template <class Param> +ZCog<Param> operator*(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) + { return sCog_Repeat<Param>(iCallable); } + +// ================================================================================================= +#pragma mark - +#pragma mark * Binary sequential, sCog_Then + +// Call cog0 till it finishes, if true then call cog1 + +template <class Param> +ZCog<Param> spCog_Then (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1); + +template <class Param> +ZCog<Param> spCogFun_Then(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog, const ZCog<Param>& iCog1) { - ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_Both<Param>)); - return sBindR(spCallable, iCallable0, iCallable1); + if (sCallPendingCog_Unchanged(lCog, iParam)) + return iSelf; + + if (sIsFalse(lCog)) + return false; + + if (sIsTrue(lCog)) + { + if (iCog1 && not sIsTrue(iCog1)) + return iCog1->Call(iCog1, iParam); + return iCog1; + } + + return spCog_Then(lCog, iCog1); } template <class Param> -ZCog<Param> sCog_Both +ZCog<Param> sCog_Then (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0 && iCallable1) - return spCog_Both(iCallable0, iCallable1); - return null; + if (sIsFalse(iCallable0)) + return false; + + if (sIsTrue(iCallable0)) + return iCallable1; + + return spCog_Then(iCallable0, iCallable1); } template <class Param> -ZCog<Param> spCogFun_Both(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> spCog_Then + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCog0 && iCog1); - ZCog<Param> lCog0 = iCog0; - ZCog<Param> lCog1 = iCog1; + if (sIsFalse(iCallable0)) + return false; - if (sIsTerm(lCog0)) - { - if (not sIsTerm(lCog1) && not lCog1->Call(lCog1, iParam)) - return null; - return lCog0; - } - else if (sCallValidCogUnchanged(lCog0, iParam)) - { - if (sIsTerm(lCog1)) - return lCog1; - if (sCallValidCogUnchanged(lCog1, iParam)) - return iSelf; - if (not lCog1) - return null; - if (sIsTerm(lCog1)) - return lCog1; - return spCog_Both(lCog0, lCog1); - } - else if (not lCog0) - { - return null; - } - else if (sIsTerm(lCog0)) - { - if (sIsTerm(lCog1)) - return lCog1; - if ((lCog1 = lCog1->Call(lCog1, iParam)) && sIsTerm(lCog1)) - return lCog1; - return null; - } - else if (sIsTerm(lCog1)) - { - return lCog1; - } - else if ((lCog1 = lCog1->Call(lCog1, iParam))) - { - if (sIsTerm(lCog1)) - return lCog1; - return spCog_Both(lCog0, lCog1); - } - else - { - return null; - } + if (sIsTrue(iCallable0)) + return iCallable1; + + static ZMACRO_auto(spCallable, sCallable(spCogFun_Then<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); } template <class Param> -ZCog<Param> operator* +ZCog<Param> operator>> (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_Both<Param>(iCallable0, iCallable1); } + { return sCog_Then<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator*= +ZCog<Param>& operator>>= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_Both<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_Then<Param>(ioCog0, iCallable1); } -template <class Param> -struct ZCogAccumulatorCombiner_Both - { - void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const - { - if (io0) - io0 = sCog_Both(io0, i1); - else - io0 = i1; - } - }; - // ================================================================================================= #pragma mark - -#pragma mark * sCog_Each +#pragma mark * Binary parallel, sCog_And template <class Param> -ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); +ZCog<Param> spCogFun_And(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1); template <class Param> -ZCog<Param> spCog_Each +ZCog<Param> spCog_And (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_Each<Param>)); + ZAssert(sIsPending(iCallable0) && not sIsTrue(iCallable1)); + static ZMACRO_auto(spCallable, sCallable(spCogFun_And<Param>)); return sBindR(spCallable, iCallable0, iCallable1); } template <class Param> -ZCog<Param> sCog_Each +ZCog<Param> sCog_And (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0) - { - if (iCallable1) - return spCog_Each(iCallable0, iCallable1); + if (sIsFalse(iCallable0)) + return false; + + if (sIsTrue(iCallable0)) + return iCallable1; + + if (sIsTrue(iCallable1)) return iCallable0; - } - return iCallable1; + + return spCog_And(iCallable0, iCallable1); } template <class Param> -ZCog<Param> spCogFun_Each(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) +ZCog<Param> spCogFun_And(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1) { - ZAssert(iCog0 && iCog1); + ZAssert(sIsPending(lCog0) && not sIsTrue(iCog1)); - if (sIsTerm(iCog0)) + if (sCallPendingCog_Unchanged(lCog0, iParam)) { - if (sIsTerm(iCog1)) - return iCog1; + if (not iCog1) + return false; + + ZCog<Param> lCog1 = iCog1; + if (sCallPendingCog_Unchanged(lCog1, iParam)) + return iSelf; + + if (not lCog1) + return false; + + if (sIsTrue(lCog1)) + return lCog0; + + return spCog_And(lCog0, lCog1); + } + else if (not lCog0) + { + return false; + } + else if (sIsTrue(lCog0)) + { + if (not iCog1) + return false; + + if (sIsTrue(iCog1)) + return true; + return iCog1->Call(iCog1, iParam); } else { - ZCog<Param> newCog0 = iCog0; - if (sCallValidCogUnchanged(newCog0, iParam)) + if (not iCog1) + return false; + + if (sIsTrue(iCog1)) + return lCog0; + + if (ZCog<Param> lCog1 = iCog1->Call(iCog1, iParam)) { - return iSelf; + if (sIsTrue(lCog1)) + return lCog0; + return spCog_And(lCog0, lCog1); } - else if (not newCog0) - { - if (sIsTerm(iCog1)) - return iCog1; - return iCog1->Call(iCog1, iParam); - } - else if (sIsTerm(newCog0)) - { - if (sIsTerm(iCog1)) - return iCog1; - if (const ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam)) - return spCog_Each(newCog0, newCog1); - return newCog0; - } - else - { - return spCog_Each(newCog0, iCog1); - } + + return false; } } template <class Param> -ZCog<Param> operator| +ZCog<Param> operator& (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_Each<Param>(iCallable0, iCallable1); } + { return sCog_And<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator|= +ZCog<Param>& operator&= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_Each<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_And<Param>(ioCog0, iCallable1); } template <class Param> -struct ZCogAccumulatorCombiner_Each +struct ZCogAccumulatorCombiner_And { void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const - { io0 = sCog_Each(io0, i1); } + { + if (io0) + io0 = sCog_And(io0, i1); + else + io0 = i1; + } }; // ================================================================================================= #pragma mark - -#pragma mark * sCog_With +#pragma mark * Binary parallel, sCog_Or template <class Param> -ZCog<Param> spCogFun_With(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1_Init, const ZCog<Param>& iCog1); +ZCog<Param> spCogFun_Or(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1); template <class Param> -ZCog<Param> spCog_With +ZCog<Param> spCog_Or (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1_Init, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCallable0 && iCallable1_Init && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_With<Param>)); - return sBindR(spCallable, iCallable0, iCallable1_Init, iCallable1); + ZAssert(sIsPending(iCallable0) && sIsPending(iCallable1)); + static ZMACRO_auto(spCallable, sCallable(spCogFun_Or<Param>)); + return sBindR(spCallable, iCallable0, iCallable1); } template <class Param> -ZCog<Param> sCog_With +ZCog<Param> sCog_Or (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0) - { - if (iCallable1 && not sIsTerm(iCallable1)) - return spCog_With(iCallable0, iCallable1, iCallable1); + if (sIsFalse(iCallable0)) + return iCallable1; + + if (sIsTrue(iCallable0)) + return true; + + if (sIsFalse(iCallable1)) return iCallable0; - } - return null; + + return spCog_Or(iCallable0, iCallable1); } template <class Param> -ZCog<Param> spCogFun_With(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1_Init, const ZCog<Param>& iCog1) +ZCog<Param> spCogFun_Or(const ZCog<Param>& iSelf, Param iParam, + ZCog<Param> lCog0, const ZCog<Param>& iCog1) { - ZAssert(iCog0 && iCog1); - ZAssert(not sIsTerm(iCog1)); + ZAssert(sIsPending(lCog0) && iCog1); - ZCog<Param> lCog0 = iCog0; - ZCog<Param> lCog1 = iCog1; + if (sCallPendingCog_Unchanged(lCog0, iParam)) + { + if (sIsTrue(iCog1)) + return true; - if (sIsTerm(lCog0)) - { - if (not lCog1->Call(lCog1, iParam)) - return null; - return lCog0; - } - else if (sCallValidCogUnchanged(lCog0, iParam)) - { - if (sCallValidCogUnchanged(lCog1, iParam)) + ZCog<Param> lCog1 = iCog1; + if (sCallPendingCog_Unchanged(lCog1, iParam)) return iSelf; if (not lCog1) return lCog0; + + if (sIsTrue(lCog1)) + return true; - if (sIsTerm(lCog1)) - return spCog_With(lCog0, iCog1_Init, iCog1_Init); - - return spCog_With(lCog0, iCog1_Init, lCog1); + return spCog_Or(lCog0, lCog1); } else if (not lCog0) { - return null; + if (sIsTrue(iCog1)) + return true; + + return iCog1->Call(iCog1, iParam); } - else if (sIsTerm(lCog0)) + else if (sIsTrue(lCog0)) { - lCog1->Call(lCog1, iParam); - return lCog0; + return true; } else { - lCog1 = lCog1->Call(lCog1, iParam); + if (sIsTrue(iCog1)) + return true; - if (not lCog1) - return lCog0; + if (ZCog<Param> lCog1 = iCog1->Call(iCog1, iParam)) + { + if (sIsTrue(lCog1)) + return true; + return spCog_Or(lCog0, lCog1); + } - if (sIsTerm(lCog1)) - return spCog_With(lCog0, iCog1_Init, iCog1_Init); - - return spCog_With(lCog0, iCog1_Init, lCog1); + return lCog0; } } template <class Param> -ZCog<Param> operator^ +ZCog<Param> operator| (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_With<Param>(iCallable0, iCallable1); } + { return sCog_Or<Param>(iCallable0, iCallable1); } template <class Param> -ZCog<Param>& operator^= +ZCog<Param>& operator|= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_With<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_Or<Param>(ioCog0, iCallable1); } template <class Param> -struct ZCogAccumulatorCombiner_With +struct ZCogAccumulatorCombiner_Or { void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const - { io0 = sCog_With(io0, i1); } + { + if (io0) + { + if (i1) + io0 = sCog_Or(io0, i1); + } + else + { + io0 = i1; + } + } }; // ================================================================================================= #pragma mark - -#pragma mark * sCog_FollowedBy +#pragma mark * sCog_With +// Call cog1 so long as cog0 is pending. + template <class Param> -ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_With(const ZCog<Param>& iSelf, Param iParam, const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> -ZCog<Param> spCog_FollowedBy_First +ZCog<Param> spCog_With (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_FollowedBy_First<Param>)); + ZAssert(sIsPending(iCallable0) && sIsPending(iCallable1)); + + static ZMACRO_auto(spCallable, sCallable(spCogFun_With<Param>)); return sBindR(spCallable, iCallable0, iCallable1); } template <class Param> -ZCog<Param> sCog_FollowedBy_First +ZCog<Param> sCog_With (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0) - { - if (iCallable1) - return spCog_FollowedBy_First(iCallable0, iCallable1); - return iCallable0; - } - return null; + if (sIsPending(iCallable0) && sIsPending(iCallable1)) + return spCog_With(iCallable0, iCallable1); + return iCallable0; } template <class Param> -ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); - -template <class Param> -ZCog<Param> spCog_FollowedBy_Subsequent - (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { - ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_FollowedBy_Subsequent<Param>)); - return sBindR(spCallable, iCallable0, iCallable1); - } - -template <class Param> -ZCog<Param> spCogFun_FollowedBy_Subsequent(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_With(const ZCog<Param>& iSelf, Param iParam, const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(iCog0 && iCog1); + ZAssert(sIsPending(iCog0) && sIsPending(iCog1)); ZCog<Param> newCog0 = iCog0; - if (sCallValidCogUnchanged(newCog0, iParam)) - return iSelf; + if (sCallPendingCog_Unchanged(newCog0, iParam)) + { + ZCog<Param> newCog1 = iCog1; + if (sCallPendingCog_Unchanged(newCog1, iParam)) + return iSelf; - if (newCog0 && not sIsTerm(newCog0)) - return spCog_FollowedBy_Subsequent(newCog0, iCog1); - - if (not sIsTerm(iCog1)) - return iCog1->Call(iCog1, iParam); + if (sIsFinished(newCog1)) + return newCog0; - return iCog1; - } - -template <class Param> -ZCog<Param> spCogFun_FollowedBy_First(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) - { - ZAssert(iCog0 && iCog1); - - if (sIsTerm(iCog0)) - { - if (sIsTerm(iCog1)) - return iCog1; - return iCog1->Call(iCog1, iParam); + return spCog_With(newCog0, newCog1); } - else if (const ZCog<Param> newCog0 = iCog0->Call(iCog0, iParam)) + else if (sIsPending(newCog0)) { - if (sIsTerm(newCog0)) - { - if (sIsTerm(iCog1)) - return iCog1; - return iCog1->Call(iCog1, iParam); - } - return spCog_FollowedBy_Subsequent(newCog0, iCog1); + ZCog<Param> newCog1 = iCog1->Call(iCog1, iParam); + if (sIsPending(newCog1)) + return spCog_With(newCog0, newCog1); + + return newCog0; } else { - return null; + return newCog0; } } @@ -701,66 +777,59 @@ ZCog<Param> operator/ (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_FollowedBy_First<Param>(iCallable0, iCallable1); } + { return sCog_With<Param>(iCallable0, iCallable1); } template <class Param> ZCog<Param>& operator/= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_FollowedBy_First<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_With<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sCog_WhileUnchanged +#pragma mark * sCog_WithUnchanged -// Call cog1 so long as cog0 is unchanged. +// Call cog1 so long as cog0 is pending and unchanged. template <class Param> -ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_WithUnchanged(const ZCog<Param>& iSelf, Param iParam, const ZCog<Param>& iCog0, const ZCog<Param>& iCog1); template <class Param> -ZCog<Param> spCog_WhileUnchanged +ZCog<Param> spCog_WithUnchanged (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { ZAssert(iCallable0 && iCallable1); - static ZMACRO_auto(spCallable, sCallable(spCogFun_WhileUnchanged<Param>)); + static ZMACRO_auto(spCallable, sCallable(spCogFun_WithUnchanged<Param>)); return sBindR(spCallable, iCallable0, iCallable1); } template <class Param> -ZCog<Param> sCog_WhileUnchanged +ZCog<Param> sCog_WithUnchanged (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - if (iCallable0) - { - if (iCallable1) - return spCog_WhileUnchanged(iCallable0, iCallable1); - return iCallable0; - } - return null; + if (sIsPending(iCallable0) && sIsPending(iCallable1)) + return spCog_WithUnchanged(iCallable0, iCallable1); + return iCallable0; } template <class Param> -ZCog<Param> spCogFun_WhileUnchanged(const ZCog<Param>& iSelf, Param iParam, +ZCog<Param> spCogFun_WithUnchanged(const ZCog<Param>& iSelf, Param iParam, const ZCog<Param>& iCog0, const ZCog<Param>& iCog1) { - ZAssert(iCog0 && iCog1); + ZAssert(sIsPending(iCog0) && sIsPending(iCog1)); - if (sIsTerm(iCog0)) - return iCog0; - ZCog<Param> newCog0 = iCog0; - if (sCallValidCogChanged(newCog0, iParam)) + if (sCallPendingCog_Changed(newCog0, iParam)) return newCog0; ZCog<Param> newCog1 = iCog1; - if (sCallValidCogChanged(newCog1, iParam)) + if (sCallPendingCog_Changed(newCog1, iParam)) { - if (newCog1 && not sIsTerm(newCog1)) - return spCog_WhileUnchanged(newCog0, newCog1); + if (sIsPending(newCog1)) + return spCog_WithUnchanged(newCog0, newCog1); return newCog0; } @@ -771,121 +840,53 @@ ZCog<Param> operator% (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return sCog_WhileUnchanged<Param>(iCallable0, iCallable1); } + { return sCog_WithUnchanged<Param>(iCallable0, iCallable1); } template <class Param> ZCog<Param>& operator%= (ZCog<Param>& ioCog0, const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) - { return ioCog0 = sCog_WhileUnchanged<Param>(ioCog0, iCallable1); } + { return ioCog0 = sCog_WithUnchanged<Param>(ioCog0, iCallable1); } // ================================================================================================= #pragma mark - -#pragma mark * sCog_Once +#pragma mark * Binary, sCog_Each -template <class Param> -ZCog<Param> sCog_Once(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return iCallable * sCog_Term<Param>(); } +// Call cog0 till it finishes, then call cog1 -// ================================================================================================= -#pragma mark - -#pragma mark * sCog_While - template <class Param> -ZCog<Param> spCog_While(const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); - -template <class Param> -ZCog<Param> spCogFun_While(const ZCog<Param>& iSelf, Param iParam, - const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) +ZCog<Param> sCog_Each + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) { - ZAssert(iCog_Init && iCog); - - if (sIsTerm(iCog)) - return iSelf; - - ZCog<Param> newCog = iCog; - if (sCallValidCogUnchanged(newCog, iParam)) - return iSelf; - - if (not newCog) - return null; - - if (not sIsTerm(newCog)) - return spCog_While(iCog_Init, newCog); - - // To get unbroken repetition on hitting a term we need to call the new cog. - // Only do so if this was not the first time, otherwise we're likely to - // recurse indefinitely and blow the stack. - if (iCog_Init == iCog) - return spCog_While(iCog_Init, iCog_Init); - else - return sCallCog(spCog_While(iCog_Init, iCog_Init), iParam); - } - -template <class Param> -ZCog<Param> spCog_While(const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) - { - static ZMACRO_auto(spCallable, sCallable(spCogFun_While<Param>)); - return sBindR(spCallable, iCog_Init, iCog); - } - -template <class Param> -ZCog<Param> sCog_While(const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { - if (iCallable) + if (sIsPending(iCallable0)) { - if (sIsTerm(iCallable)) - return iCallable; - return spCog_While<Param>(iCallable, iCallable); + if (sIsPending(iCallable1)) + return +iCallable0 >> iCallable1; + return iCallable0; } - return null; + return iCallable1; } template <class Param> -ZCog<Param> operator*(const ZCog<Param>& iCog) - { return sCog_While<Param>(iCog); } +ZCog<Param> operator^ + (const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return sCog_Each<Param>(iCallable0, iCallable1); } -// ================================================================================================= -#pragma mark - -#pragma mark * sCog_Repeat (count) - template <class Param> -ZCog<Param> spCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog); +ZCog<Param>& operator^= + (ZCog<Param>& ioCog0, + const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable1) + { return ioCog0 = ioCog0 ^ iCallable1; } template <class Param> -ZCog<Param> spCogFun_Repeat(const ZCog<Param>& iSelf, Param iParam, - size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) +struct ZCogAccumulatorCombiner_Each { - if (sCallCogUnchanged(iCog, iParam)) - return iSelf; + void operator()(ZCog<Param>& io0, const ZCog<Param>& i1) const + { io0 ^= i1; } + }; - if (not iCog) - return null; - - if (sIsTerm(iCog)) - return sCallCog(spCog_Repeat(iCount - 1, iCog_Init, iCog_Init), iParam); - else - return spCog_Repeat(iCount, iCog_Init, iCog); - } - -template <class Param> -ZCog<Param> spCog_Repeat(size_t iCount, const ZCog<Param>& iCog_Init, const ZCog<Param>& iCog) - { - static ZMACRO_auto(spCallable, sCallable(spCogFun_Repeat<Param>)); - if (iCount && iCog_Init) - { - if (sIsTerm(iCog_Init)) - return iCog_Init; - return sBindR(spCallable, iCount, iCog_Init, iCog); - } - return null; - } - -template <class Param> -ZCog<Param> sCog_Repeat(size_t iCount, - const ZRef<ZCallable<ZCog<Param>(const ZCog<Param>&,Param)> >& iCallable) - { return spCog_Repeat<Param>(iCount, iCallable, iCallable); } - } // namespace ZooLib #endif // __ZCog_h__ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |