|
- /**
- * @file Queue2D.java
- * @brief
- * File containing the Queue2D class. A queue for x,y coordinate
- * pairs
- *
- * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr
- * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr
- */
- package gr.auth.ee.dsproject.node;
-
- /**
- * @brief
- * A queue implementation for (x, y) coordinates
- */
- public class Queue2D
- {
- int [][] q; //!< The queue array
- //int f;
- int r; // Rear pointer
- int size, nItem; // Queue helper size and queued items
-
- /*
- * ============ Constructors ==============
- */
- /**
- * @brief
- * Simple constructor. Initialize all to zero
- * @note
- * As long as there is no Setter for buffer and read pointer
- * this constructor is useless.
- */
- public Queue2D () {
- //f = 0;
- r = 0;
- size = nItem = 0;
- q = null;
- }
-
- /**
- * @brief
- * Constructor with buffer size
- * @param size The size of buffer to allocate
- */
- public Queue2D (int size) {
- //f = 0;
- r = -1; // Init the rear value
- nItem = 0;
- this.size = size;
- q = new int[size][2];
- }
-
- /**
- * @return The "waiting" items in queue
- */
- public int size () { return nItem; }
- /**
- * @return True if queue is empty
- */
- public boolean isEmpty () { return (nItem == 0) ? true : false; }
- /**
- * @return True if queue is full
- */
- public boolean isFull () { return (nItem >= size) ? true : false; }
-
- /**
- * @return
- * The first item waiting in queue, without removing it
- * If the queue is empty return {-1, -1}
- */
- public int [] peek () {
- int [] none = {-1, -1};
- if (!isEmpty ()) {
- return q[0]; //return q[f];
- }
- else
- return none;
- }
-
- /**
- * @return
- * The first item waiting in queue and removes it
- * If the queue is empty return {-1, -1}
- */
- public int [] remove () {
- int [] ret = {-1, -1};
- if (!isEmpty ()) {
- ret[0] = q[0][0]; //ret[0] = q[f][0];
- ret[1] = q[0][1]; //ret[1] = q[f][1];
- // shift the buffered items
- for (int i=0 ; i<r ; ++i) {
- q[i][0] = q[i+1][0];
- q[i][1] = q[i+1][1];
- }
- //++f;
- --r;
- --nItem;
- return ret;
- }
- else
- return ret;
- }
-
- /**
- * @brief
- * Insert an item to the queue
- * @param it Reference to item to insert
- * @return The inserted item
- * If the queue is full return {-1, -1}
- */
- public int [] insert (int [] it) {
- int [] none = {-1, -1};
-
- if (!isFull ()) {
- ++r;
- q[r][0] = it[0];
- q[r][1] = it[1];
- ++nItem;
- return it;
- }
- else
- return none;
- }
- }
|