Dequeue removes data into the queue if data exists.
()
| 72 | |
| 73 | // Dequeue removes data into the queue if data exists. |
| 74 | func (q *Queue) Dequeue() (*Data, error) { |
| 75 | |
| 76 | // If the front and end are the same, the |
| 77 | // queue is empty |
| 78 | // EF - (Empty) |
| 79 | // [ ][ ][ ] |
| 80 | if q.front == q.end { |
| 81 | return nil, errors.New("queue is empty") |
| 82 | } |
| 83 | |
| 84 | var data *Data |
| 85 | switch { |
| 86 | case q.end == len(q.data): |
| 87 | |
| 88 | // If we are at the end of the capacity, then |
| 89 | // circle back to the beginning of the capacity by |
| 90 | // moving the end pointer to the beginning. |
| 91 | q.end = 0 |
| 92 | data = q.data[q.end] |
| 93 | default: |
| 94 | |
| 95 | // Remove the data from the current end position |
| 96 | // and then move the end pointer. |
| 97 | data = q.data[q.end] |
| 98 | q.end++ |
| 99 | } |
| 100 | |
| 101 | q.Count-- |
| 102 | |
| 103 | return data, nil |
| 104 | } |
| 105 | |
| 106 | // Operate accepts a function that takes data and calls |
| 107 | // the specified function for every piece of data found. |
no outgoing calls