View Javadoc

1   /*******************************************************************************
2   * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3   *
4   * All rights reserved. This program and the accompanying materials
5   * are made available under the terms of the Eclipse Public License v1.0
6   * which accompanies this distribution, and is available at
7   * http://www.eclipse.org/legal/epl-v10.html
8   *
9   * Alternatively, the contents of this file may be used under the terms of
10  * either the GNU Lesser General Public License Version 2.1 or later (the
11  * "LGPL"), in which case the provisions of the LGPL are applicable instead
12  * of those above. If you wish to allow use of your version of this file only
13  * under the terms of the LGPL, and not to allow others to use your version of
14  * this file under the terms of the EPL, indicate your decision by deleting
15  * the provisions above and replace them with the notice and other provisions
16  * required by the LGPL. If you do not delete the provisions above, a recipient
17  * may use your version of this file under the terms of the EPL or the LGPL.
18  * 
19  * Based on the original MiniSat specification from:
20  * 
21  * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22  * Sixth International Conference on Theory and Applications of Satisfiability
23  * Testing, LNCS 2919, pp 502-518, 2003.
24  *
25  * See www.minisat.se for the original solver in C++.
26  * 
27  *******************************************************************************/
28  package org.sat4j.minisat.core;
29  
30  import java.io.Serializable;
31  
32  /**
33   * Implementation of a queue.
34   * 
35   * Formerly used in the solver to maintain unit literals for unit propagation.
36   * No longer used currently.
37   * 
38   * @author leberre
39   */
40  public final class IntQueue implements Serializable {
41  
42      private static final long serialVersionUID = 1L;
43  
44      private static final int INITIAL_QUEUE_CAPACITY = 10;
45  
46      /**
47       * Add an element to the queue. The queue is supposed to be large enough for
48       * that!
49       * 
50       * @param x
51       *            the element to add
52       */
53      public void insert(final int x) {
54          // ensure(size + 1);
55          assert size < myarray.length;
56          myarray[size++] = x;
57      }
58  
59      /**
60       * returns the nexdt element in the queue. Unexpected results if the queue
61       * is empty!
62       * 
63       * @return the firsst element on the queue
64       */
65      public int dequeue() {
66          assert first < size;
67          return myarray[first++];
68      }
69  
70      /**
71       * Vide la queue
72       */
73      public void clear() {
74          size = 0;
75          first = 0;
76      }
77  
78      /**
79       * Pour connaitre la taille de la queue.
80       * 
81       * @return le nombre d'elements restant dans la queue
82       */
83      public int size() {
84          return size - first;
85      }
86  
87      /**
88       * Utilisee pour accroitre dynamiquement la taille de la queue.
89       * 
90       * @param nsize
91       *            la taille maximale de la queue
92       */
93      public void ensure(final int nsize) {
94          if (nsize >= myarray.length) {
95              int[] narray = new int[Math.max(nsize, size * 2)];
96              System.arraycopy(myarray, 0, narray, 0, size);
97              myarray = narray;
98          }
99      }
100 
101     @Override
102     public String toString() {
103         StringBuffer stb = new StringBuffer();
104         stb.append(">"); //$NON-NLS-1$
105         for (int i = first; i < size - 1; i++) {
106             stb.append(myarray[i]);
107             stb.append(" "); //$NON-NLS-1$
108         }
109         if (first != size) {
110             stb.append(myarray[size - 1]);
111         }
112         stb.append("<"); //$NON-NLS-1$
113         return stb.toString();
114     }
115 
116     private int[] myarray = new int[INITIAL_QUEUE_CAPACITY];
117 
118     private int size = 0;
119 
120     private int first = 0;
121 
122 }