diff dwtx/jface/text/reconciler/DirtyRegionQueue.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/reconciler/DirtyRegionQueue.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.reconciler.DirtyRegionQueue;
+
+import dwt.dwthelper.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * Queue used by {@link dwtx.jface.text.reconciler.AbstractReconciler} to manage
+ * dirty regions. When a dirty region is inserted into the queue, the queue tries
+ * to fold it into the neighboring dirty region.
+ *
+ * @see dwtx.jface.text.reconciler.AbstractReconciler
+ * @see dwtx.jface.text.reconciler.DirtyRegion
+ */
+class DirtyRegionQueue {
+
+    /** The list of dirty regions. */
+    private List fDirtyRegions= new ArrayList();
+
+    /**
+     * Creates a new empty dirty region.
+     */
+    public DirtyRegionQueue() {
+        super();
+    }
+
+    /**
+     * Adds a dirty region to the end of the dirty-region queue.
+     *
+     * @param dr the dirty region to add
+     */
+    public void addDirtyRegion(DirtyRegion dr) {
+        // If the dirty region being added is directly after the last dirty
+        // region on the queue then merge the two dirty regions together.
+        DirtyRegion lastDR= getLastDirtyRegion();
+        bool wasMerged= false;
+        if (lastDR !is null)
+            if (lastDR.getType() is dr.getType())
+                if (lastDR.getType() is DirtyRegion.INSERT) {
+                    if (lastDR.getOffset() + lastDR.getLength() is dr.getOffset()) {
+                        lastDR.mergeWith(dr);
+                        wasMerged= true;
+                    }
+                } else if (lastDR.getType() is DirtyRegion.REMOVE) {
+                    if (dr.getOffset() + dr.getLength() is lastDR.getOffset()) {
+                        lastDR.mergeWith(dr);
+                        wasMerged= true;
+                    }
+                }
+
+        if (!wasMerged)
+            // Don't merge- just add the new one onto the queue.
+            fDirtyRegions.add(dr);
+    }
+
+    /**
+     * Returns the last dirty region that was added to the queue.
+     *
+     * @return the last DirtyRegion on the queue
+     */
+    private DirtyRegion getLastDirtyRegion() {
+        int size= fDirtyRegions.size();
+        return (size is 0 ? null : (DirtyRegion) fDirtyRegions.get(size - 1));
+    }
+
+    /**
+     * Returns the number of regions in the queue.
+     *
+     * @return the dirty-region queue-size
+     */
+    public int getSize() {
+        return fDirtyRegions.size();
+    }
+
+    /**
+     * Throws away all entries in the queue.
+     */
+    public void purgeQueue() {
+        fDirtyRegions.clear();
+    }
+
+    /**
+     * Removes and returns the first dirty region in the queue
+     *
+     * @return the next dirty region on the queue
+     */
+    public DirtyRegion removeNextDirtyRegion() {
+        if (fDirtyRegions.size() is 0)
+            return null;
+        DirtyRegion dr= (DirtyRegion) fDirtyRegions.get(0);
+        fDirtyRegions.remove(0);
+        return dr;
+    }
+}