Package beaver.spec.ast

Examples of beaver.spec.ast.Rule$Definition$Element


     */
    protected final Object fetch(int index, boolean recordHitsOrMisses) {

        // attempt to get the element from the cache
        Object value = null;
        Element cacheNode = (Element)indexTree.get(index);

        // The value is cached, return cached value
        if(cacheNode != null) {
            if(recordHitsOrMisses) cacheHits ++;;
            AgedNode agedNode = (AgedNode)cacheNode.get();
            value = agedNode.getValue();
            cache.remove(cacheNode);
            SparseListNode indexNode = agedNode.getIndexNode();
            indexNode.setValue(cache.addInSortedOrder((byte)1, agedNode, 1));

        // The value is not cached, lookup from source and cache
        } else {
            if(recordHitsOrMisses) cacheMisses++;
            // Make room in the cache if it is full
            if(currentSize >= maxSize) {
                Element oldestInCache = cache.get(0);
                cache.remove(oldestInCache);
                AgedNode oldAgedNode = (AgedNode)oldestInCache.get();
                SparseListNode oldIndexNode = oldAgedNode.getIndexNode();
                indexTree.set(oldIndexNode.getIndex(), null);
                currentSize--;
            }

View Full Code Here


            // get the current change info
            int index = listChanges.getIndex();
            int changeType = listChanges.getType();

            // Lookup the cache entry for this index if possible
            Element cacheNode = null;
            if(index < lastKnownSize) {
                cacheNode = (Element)indexTree.get(index);
            }

            // An INSERT causes the indexes of cached values to be offset.
View Full Code Here

        }

        private void populateIndicesByValue(int firstNeeded, int lastNeeded) {

            // fill in everything between firstNeeded and firstAvailable
            Element firstAvailableElement = indicesByValue.first();
            int firstAvailable = firstAvailableElement != null
                    ? data.indexOfNode(firstAvailableElement, ALL_NODES)
                    : lastNeeded;
            populate(firstNeeded, firstAvailable);

            // fill in everything between lastAvailable and lastNeeded
            Element lastAvailableElement = indicesByValue.last();
            int lastAvailable = lastAvailableElement != null
                    ? data.indexOfNode(lastAvailableElement, ALL_NODES)
                    : firstAvailable;
            populate(lastAvailable + 1, lastNeeded);
        }
View Full Code Here

                insertNodes.addLast(unsortedNode);

            // on update, mark the updated node as unsorted and save it so it can be moved
            } else if(changeType == ListEvent.UPDATE) {
                Element<Element> unsortedNode = unsorted.get(unsortedIndex);
                Element sortedNode = unsortedNode.get();
                sortedNode.setSorted(Element.PENDING);
                updateNodes.add(sortedNode);
                previousValues.add(listChanges.getOldValue());

            // on delete, delete the index and sorted node
            } else if(changeType == ListEvent.DELETE) {
                Element<Element> unsortedNode = unsorted.get(unsortedIndex);
                E deleted = listChanges.getOldValue();
                unsorted.remove(unsortedNode);
                int deleteSortedIndex = deleteByUnsortedNode(unsortedNode);
                updates.elementDeleted(deleteSortedIndex, deleted);

            }
        }

        // decide which updated elements need to be shifted. We walk through the
        // tree, marking updated elements as sorted or unsorted depending on their
        // value relative to their neighbours
        for(int i = 0, size = updateNodes.size(); i < size; i++) {
            Element<Element> sortedNode = updateNodes.get(i);
            // we may have already handled this via a neighbour
            if(sortedNode.getSorted() != Element.PENDING) continue;

            // find the bounds (by value) on this element. this is the last element
            // preceeding current that's sorted and the first element after current
            // that's sorted. If there's no such element (ie. the end of the list),
            // then the bound element is null
            Element lowerBound = null;
            Element upperBound = null;
            Element firstUnsortedNode = sortedNode;
            for(Element leftNeighbour = sortedNode.previous(); leftNeighbour != null; leftNeighbour = leftNeighbour.previous()) {
                if(leftNeighbour.getSorted() != Element.SORTED) {
                    firstUnsortedNode = leftNeighbour;
                    continue;
                }
                lowerBound = leftNeighbour;
                break;
            }
            for(Element rightNeighbour = sortedNode.next(); rightNeighbour != null; rightNeighbour = rightNeighbour.next()) {
                if(rightNeighbour.getSorted() != Element.SORTED) continue;
                upperBound = rightNeighbour;
                break;
            }

            // walk from the leader to the follower, marking elements as in sorted
            // order or not. We simply compare them to our 2 potentially distant neighbours
            // on either side - the lower and upper bounds
            Comparator nodeComparator = sorted.getComparator();
            for(Element current = firstUnsortedNode; current != upperBound; current = current.next()) {

                // ensure we're less than the upper bound
                if(upperBound != null && nodeComparator.compare(current.get(), upperBound.get()) > 0) {
                    current.setSorted(Element.UNSORTED);
                    continue;
                }

                // and greater than the lower bound
                if(lowerBound != null && nodeComparator.compare(current.get(), lowerBound.get()) < 0) {
                    current.setSorted(Element.UNSORTED);
                    continue;
                }

                // so the node is sorted, and it's our new lower bound
                current.setSorted(Element.SORTED);
                lowerBound = current;
            }
        }

        // fire update events
        for(int i = 0, size = updateNodes.size(); i < size; i++) {
            E previous = previousValues.get(i);
            Element<Element> sortedNode = updateNodes.get(i);
            assert(sortedNode.getSorted() != Element.PENDING);
            int originalIndex = sorted.indexOfNode(sortedNode, ALL_COLORS);

            // the element is still in sorted order, forward the update event
            if(sortedNode.getSorted() == Element.SORTED) {
                updates.elementUpdated(originalIndex, previous);

            // sort order is not enforced so we lose perfect sorting order
            // but we don't need to move elements around
            } else if(mode == AVOID_MOVING_ELEMENTS) {
                updates.elementUpdated(originalIndex, previous);

            // sort order is enforced so move the element to its new location
            } else {
                sorted.remove(sortedNode);
                updates.elementDeleted(originalIndex, previous);
                int insertedIndex = insertByUnsortedNode(sortedNode.get());
                updates.addInsert(insertedIndex);
            }
        }

        // fire insert events
        while(!insertNodes.isEmpty()) {
            Element insertNode = insertNodes.removeFirst();
            int insertedIndex = insertByUnsortedNode(insertNode);
            updates.addInsert(insertedIndex);
        }

        // commit the changes and notify listeners
View Full Code Here

     *
     * @return the sortIndex of the deleted object.
     */
    private int deleteByUnsortedNode(Element unsortedNode) {
        // get the sorted node
        Element sortedNode = (Element)unsortedNode.get();
        // look up the sorted index before removing the nodes
        int sortedIndex = sorted.indexOfNode(sortedNode, ALL_COLORS);
        // delete the sorted node from its tree
        sorted.remove(sortedIndex, 1);
        // return the sorted index
View Full Code Here

        return sortedIndex;
    }

    /** {@inheritDoc} */
    protected int getSourceIndex(int mutationIndex) {
        Element sortedNode = sorted.get(mutationIndex);
        Element unsortedNode = (Element)sortedNode.get();
        return unsorted.indexOfNode(unsortedNode, ALL_COLORS);
    }
View Full Code Here

        // create a list which knows the offsets of the indexes to initialize this list
        if(previousSorted == null && unsorted == null) {
            unsorted = new SimpleTree<Element>();
            // add all elements in the source list, in order
            for(int i = 0, n = source.size(); i < n; i++) {
                Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1);
                insertByUnsortedNode(unsortedNode);
            }
            // this is the first sort so we're done
            return;
        }

        // if the lists are empty, we're done
        if(source.size() == 0) return;

        // rebuild the sorted tree to reflect the new Comparator
        for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(unsorted); i.hasNext(); ) {
            i.next();
            Element unsortedNode = i.node();
            insertByUnsortedNode(unsortedNode);
        }

        // construct the reorder map
        int[] reorderMap = new int[size()];
        int oldSortedIndex = 0;
        for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) {
            i.next();
            Element oldSortedNode = i.node();
            Element unsortedNode = (Element)oldSortedNode.get();
            Element newSortedNode = (Element)unsortedNode.get();
            int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS);
            reorderMap[newSortedIndex] = oldSortedIndex;
        }

        // notification about the big change
View Full Code Here

            Object alphaObject = alpha;
            Object betaObject = beta;
            int alphaIndex = -1;
            int betaIndex = -1;
            if(alpha instanceof Element) {
                Element alphaTreeNode = (Element)alpha;
                alphaIndex = unsorted.indexOfNode(alphaTreeNode, ALL_COLORS);
                alphaObject = source.get(alphaIndex);
            }
            if(beta instanceof Element) {
                Element betaTreeNode = (Element)beta;
                betaIndex = unsorted.indexOfNode(betaTreeNode, ALL_COLORS);
                betaObject = source.get(betaIndex);
            }
            int result = comparator.compare(alphaObject, betaObject);
            if(result != 0) return result;
View Full Code Here

    private class ElementRawOrderComparator implements Comparator {
        /**
         * Compares the alpha object to the beta object by their indices.
         */
        public int compare(Object alpha, Object beta) {
            Element alphaTreeNode = (Element)alpha;
            Element betaTreeNode = (Element)beta;
            int alphaIndex = unsorted.indexOfNode(alphaTreeNode, ALL_COLORS);
            int betaIndex = unsorted.indexOfNode(betaTreeNode, ALL_COLORS);
            return alphaIndex - betaIndex;
        }
View Full Code Here

        /**
         * Returns the next value in the iteration.
         */
        public E next() {
            treeIterator.next();
            Element unsortedNode = treeIterator.value();
            return source.get(unsorted.indexOfNode(unsortedNode, ALL_COLORS));
        }
View Full Code Here

TOP

Related Classes of beaver.spec.ast.Rule$Definition$Element

Copyright © 2018 www.massapicom. 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.