Package abstrasy.pcfx

Source Code of abstrasy.pcfx.PCFx_foreach_part

package abstrasy.pcfx;


import abstrasy.Heap;
import abstrasy.Interpreter;
import abstrasy.Node;
import abstrasy.PCoder;

import abstrasy.interpreter.InterpreterException;
import abstrasy.interpreter.StdErrors;

/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/

public class PCFx_foreach_part extends PCFx {

    /*
     * Algorithme basé sur un papier de Michael Orlov <orlov@cs.bgu.ac.il>
     * du 26 mars 2002, intitulé "Efficient generation of set partitions"
     * (Pour plus d'informations consultez http://www.cs.bgu.ac.il/~orlovm/).
     */
    private static final class Part {

        private int[] k = null;
        private int[] m = null;
        private Node[] l = null;
        private int n = 0;
        private int cnt = 0;

        Part(Node[] l) {
            if (l != null) {
                this.l = l;
                this.k = new int[l.length];
                this.m = new int[l.length];
                this.n = l.length - 1;
                this.cnt = 0;
            }
        }

        boolean hasNext() {
            if (l == null) {
                return false;
            }
            if (cnt == 0) {
                cnt++;
                return true;
            }
            else {
                cnt++;
                for (int i = n; i >= 1; i--) {
                    if (k[i] <= m[i - 1]) {
                        k[i]++;
                        m[i] = Math.max(m[i], k[i]);
                        for (int j = i + 1; j <= n; j++) {
                            k[j] = k[0];
                            m[j] = m[i];
                        }
                        return true;
                    }
                }
                return false;
            }
        }

        Node[] getNext() throws Exception {
            Node[] p = new Node[l.length];
            for (int i = 0; i <= n; i++) {
                p[i] = Node.createCList();
            }
            for (int i = 0; i <= n; i++) {
                p[k[i]].addElement(l[i]);
            }
            return p;
        }

    }

    public PCFx_foreach_part() {
    }
   
    private final static void _clear_closure_(Heap closure){ if (closure.isLoaded()) closure.clear(); }

    /**
     * eval
     *
     * @param startAt Node
     * @return Node
     * @throws Exception
     * @todo Implémenter cette méthode abstrasy.PCFx
     */
    public Node eval(Node startAt) throws Exception {
        /*
         * forme (foreach-part [liste] do{...})
         *       (foreach-part [liste] list{...})
         */
        int i = 1;
        startAt.isGoodArgsCnt(4);
        Node rnode = null;
        Node xnode = null;
        Node lNode = startAt.getSubNode(i++, Node.TYPE_CLIST);
        /**
         * foreach-part ne supporte que la liste comme argument entrant.
         *
         * Il faut, en effet, connaître le nombre d'éléments pour envisager
         * toutes les partitions possibles.
         */

        Part permut = new Part(lNode.getArray());

        Node btype = startAt.getSubNode(i++, Node.TYPE_PCODE);
        Node enode = startAt.getSubNode(i++, Node.TYPE_LAZY);
        Interpreter interpreter = Interpreter.mySelf();
        boolean oldCanLoop = interpreter.isCanLoop();
        boolean oldInLoop = interpreter.isInLoop();
        interpreter.setCanLoop(true);
        interpreter.setInLoop(true);

        try {
            if (btype.isPCode(PCoder.PC_DO)) {
                /**
                 * (foreach-part [liste] do {...})
                 */
                Node argv;

                Heap.push(); // nouvel espace local pour argv (exclusivment)
                Heap argv_h = Heap.current(); // optimisation du 09/03/2012
               
                Heap.push(); // nouvel espace local
                Heap local = Heap.current();

                /*
                 * Correction du 10 mai 2011:
                 * =========================
                 *    Les boucles du type do{...} s'arrête dès qu'un résultat est retourné. Cela ne signifie
                 *    bien entendu pas que la condition qui permet l'itération n'est pas vérifiée. Toutefois,
                 *    comme les boucle du type do{...} ne peuvent retourner qu'un seul résultat, il est inutile
                 *    de relancer l'itération suivante ddès qu'un résultat est fourni. Ainsi, pour permettre
                 *    de continuer la boucle, il est possible de placer le résultat dans une variable et non de
                 *    la retourner directement comme résultat.
                 *   
                 */
                while (interpreter.isCanIterate() && permut.hasNext() && xnode==null) {
                    _clear_closure_(local);
                    argv = Node.createCList();
                    argv.setArray(permut.getNext());
                    argv_h.put(PCoder.ARGV, argv);
                    xnode = enode.exec(true);
                }

                Heap.pull(); // supprimer l'espace local...
                Heap.pull(); // supprimer l'espace de noms pour argv...

            }
            else if (btype.isPCode(PCoder.PC_LIST)) {
                /**
                 * (foreach-part [liste] list{...})
                 */
                Node argv;
                xnode = Node.createCList();

                Heap.push(); // nouvel espace local pour argv (exclusivment)
                Heap argv_h = Heap.current(); // optimisation du 09/03/2012

                Heap.push(); // nouvel espace local
                Heap local = Heap.current();

                while (interpreter.isCanIterate() && permut.hasNext()) {
                    _clear_closure_(local);
                    argv = Node.createCList();
                    argv.setArray(permut.getNext());
                    argv_h.put(PCoder.ARGV, argv);
                    rnode = enode.exec(true);
                    if (rnode != null)
                        xnode.addElement(rnode.secure());
                   
                }

                Heap.pull(); // supprimer l'espace local...
                Heap.pull(); // supprimer l'espace de noms pour argv...

            }
            else {
                // erreur de syntaxe.
                throw new InterpreterException(StdErrors.Syntax_error);
            }
        }
        catch (Exception ex) {
            interpreter.consumeBreakCode_onLoop();
            interpreter.setCanLoop(oldCanLoop);
            interpreter.setInLoop(oldInLoop);
            throw ex;
        }
        interpreter.consumeBreakCode_onLoop();
        interpreter.setCanLoop(oldCanLoop);
        interpreter.setInLoop(oldInLoop);
        return xnode;
    }

}
TOP

Related Classes of abstrasy.pcfx.PCFx_foreach_part

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.