View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j.minisat.core;
27  
28  import java.io.Serializable;
29  
30  /**
31   * Implementation of a queue.
32   * 
33   * Formerly used in the solver to maintain unit literals for unit propagation.
34   * No longer used currently.
35   * 
36   * @author leberre
37   */
38  public final class IntQueue implements Serializable {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private static final int INITIAL_QUEUE_CAPACITY = 10;
43  
44      /**
45       * Add an element to the queue. The queue is supposed to be large enough for
46       * that!
47       * 
48       * @param x
49       *            the element to add
50       */
51      public void insert(final int x) {
52          // ensure(size + 1);
53          assert size < myarray.length;
54          myarray[size++] = x;
55      }
56  
57      /**
58       * returns the nexdt element in the queue. Unexpected results if the queue
59       * is empty!
60       * 
61       * @return the firsst element on the queue
62       */
63      public int dequeue() {
64          assert first < size;
65          return myarray[first++];
66      }
67  
68      /**
69       * Vide la queue
70       */
71      public void clear() {
72          size = 0;
73          first = 0;
74      }
75  
76      /**
77       * Pour connaitre la taille de la queue.
78       * 
79       * @return le nombre d'elements restant dans la queue
80       */
81      public int size() {
82          return size - first;
83      }
84  
85      /**
86       * Utilisee pour accroitre dynamiquement la taille de la queue.
87       * 
88       * @param nsize
89       *            la taille maximale de la queue
90       */
91      public void ensure(final int nsize) {
92          if (nsize >= myarray.length) {
93              int[] narray = new int[Math.max(nsize, size * 2)];
94              System.arraycopy(myarray, 0, narray, 0, size);
95              myarray = narray;
96          }
97      }
98  
99      @Override
100     public String toString() {
101         StringBuffer stb = new StringBuffer();
102         stb.append(">"); //$NON-NLS-1$
103         for (int i = first; i < size - 1; i++) {
104             stb.append(myarray[i]);
105             stb.append(" "); //$NON-NLS-1$
106         }
107         if (first != size) {
108             stb.append(myarray[size - 1]);
109         }
110         stb.append("<"); //$NON-NLS-1$
111         return stb.toString();
112     }
113 
114     private int[] myarray = new int[INITIAL_QUEUE_CAPACITY];
115 
116     private int size = 0;
117 
118     private int first = 0;
119 
120 }