X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2Fchr%2FCHRPriority.java;fp=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2Fchr%2FCHRPriority.java;h=008830b93e7d988f0a178bf1b9c3e462d6f9d810;hp=0000000000000000000000000000000000000000;hb=a2df536f7fc878982c6c960a79ed49f350cddc6f;hpb=5f0ad7a26810df602600c5eddad317588fce0ac4 diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRPriority.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRPriority.java new file mode 100644 index 000000000..008830b93 --- /dev/null +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/chr/CHRPriority.java @@ -0,0 +1,78 @@ +package org.simantics.scl.runtime.chr; + +/** + * This class implements a pairing heap of CHR priorities. + */ +public abstract class CHRPriority implements Comparable { + + public final int priority; + + // Pairing heap + private CHRPriority sibling; + private CHRPriority child; + + boolean inContext; + + public CHRPriority(int priority) { + this.priority = priority; + } + + /** + * This method assume that a and b are roots and their sibling field + * can be overridden. + */ + public static CHRPriority merge(CHRPriority a, CHRPriority b) { + if(a.priority <= b.priority) { + a.sibling = null; + b.sibling = a.child; + a.child = b; + return a; + } + else { + a.sibling = b.child; + b.sibling = null; + b.child = a; + return b; + } + } + + protected void ensureInContext(CHRContext context) { + if(!inContext) { + CHRPriority topPriority = context.topPriority; + if(topPriority == null) + context.topPriority = this; + else + context.topPriority = merge(topPriority, this); + inContext = true; + } + } + + private CHRPriority mergeSiblings() { + if(sibling == null) + return this; + CHRPriority nextSibling = sibling.sibling; + CHRPriority merged = merge(this, sibling); + if(nextSibling == null) + return merged; + else + return merge(merged, nextSibling.mergeSiblings()); + } + + public CHRPriority nextPriority() { + if(child == null) + return null; + else { + CHRPriority result = child.mergeSiblings(); + child = null; + return result; + } + } + + @Override + public int compareTo(CHRPriority o) { + return Double.compare(priority, o.priority); + } + + public abstract void activate(CHRContext context); + //public abstract CHRRuntimeRuleset getParent(); +}