1 | /* $Id: queue.cpp,v 1.1 2000-03-09 19:03:20 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Very simply generic queue class
|
---|
5 | *
|
---|
6 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
9 | *
|
---|
10 | */
|
---|
11 | #include <stdio.h>
|
---|
12 | #include <stdlib.h>
|
---|
13 | #include <string.h>
|
---|
14 | #include <os2win.H>
|
---|
15 | #include <misc.h>
|
---|
16 | #include "queue.h"
|
---|
17 |
|
---|
18 | #define DBG_LOCALLOG DBG_queue
|
---|
19 | #include "dbglocal.h"
|
---|
20 |
|
---|
21 | //******************************************************************************
|
---|
22 | //******************************************************************************
|
---|
23 | Queue::Queue()
|
---|
24 | {
|
---|
25 | head = tail = NULL;
|
---|
26 | }
|
---|
27 | //******************************************************************************
|
---|
28 | //******************************************************************************
|
---|
29 | Queue::~Queue()
|
---|
30 | {
|
---|
31 | while(hasElements()) {
|
---|
32 | Pop();
|
---|
33 | }
|
---|
34 | }
|
---|
35 | //******************************************************************************
|
---|
36 | //******************************************************************************
|
---|
37 | void Queue::operator=(Queue& queue)
|
---|
38 | {
|
---|
39 | QueueItem *item = queue.Head();
|
---|
40 |
|
---|
41 | if(item)
|
---|
42 | PushRev(queue, item);
|
---|
43 | }
|
---|
44 | //******************************************************************************
|
---|
45 | //******************************************************************************
|
---|
46 | void Queue::PushRev(Queue& queue, QueueItem *item)
|
---|
47 | {
|
---|
48 | QueueItem *nextitem = queue.getNext(item);
|
---|
49 |
|
---|
50 | if(nextitem) {
|
---|
51 | PushRev(queue, nextitem);
|
---|
52 | }
|
---|
53 | Push(queue.getItem(item));
|
---|
54 | }
|
---|
55 | //******************************************************************************
|
---|
56 | //******************************************************************************
|
---|
57 | void Queue::Push(ULONG dwItemData)
|
---|
58 | {
|
---|
59 | QueueItem *item;
|
---|
60 |
|
---|
61 | item = new QueueItem(dwItemData);
|
---|
62 | if(item == NULL) {
|
---|
63 | DebugInt3();
|
---|
64 | return;
|
---|
65 | }
|
---|
66 | if(head == NULL) {
|
---|
67 | head = item;
|
---|
68 | tail = item;
|
---|
69 | item->next = NULL;
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | item->next = head;
|
---|
73 | head = item;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | //******************************************************************************
|
---|
77 | //******************************************************************************
|
---|
78 | void Queue::Remove(ULONG dwItemData)
|
---|
79 | {
|
---|
80 | QueueItem *item = head, *tmp;
|
---|
81 |
|
---|
82 | if(item == NULL) {
|
---|
83 | DebugInt3();
|
---|
84 | return;
|
---|
85 | }
|
---|
86 | if(item->dwItemData == dwItemData) {
|
---|
87 | head = item->next;
|
---|
88 | delete item;
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | while(item->next) {
|
---|
92 | if(item->next->dwItemData == dwItemData) {
|
---|
93 | tmp = item->next;
|
---|
94 | item->next = tmp->next;
|
---|
95 | delete tmp;
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | item = item->next;
|
---|
99 | }
|
---|
100 | DebugInt3();
|
---|
101 | dprintf(("Queue::Remove: item %x not found!", dwItemData));
|
---|
102 | }
|
---|
103 | //******************************************************************************
|
---|
104 | //******************************************************************************
|
---|
105 | void Queue::Remove(QueueItem *removeitem)
|
---|
106 | {
|
---|
107 | QueueItem *item = head, *tmp;
|
---|
108 |
|
---|
109 | if(item == NULL) {
|
---|
110 | DebugInt3();
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | if(item == removeitem) {
|
---|
114 | head = item->next;
|
---|
115 | delete item;
|
---|
116 | return;
|
---|
117 | }
|
---|
118 | while(item->next) {
|
---|
119 | if(item->next == removeitem) {
|
---|
120 | tmp = item->next;
|
---|
121 | item->next = tmp->next;
|
---|
122 | delete tmp;
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | item = item->next;
|
---|
126 | }
|
---|
127 | DebugInt3();
|
---|
128 | dprintf(("Queue::Remove: item %x not found!", removeitem));
|
---|
129 | }
|
---|
130 | //******************************************************************************
|
---|
131 | //******************************************************************************
|
---|
132 | ULONG Queue::Pop()
|
---|
133 | {
|
---|
134 | QueueItem *item;
|
---|
135 | ULONG itemdata;
|
---|
136 |
|
---|
137 | if(head == NULL) {
|
---|
138 | DebugInt3();
|
---|
139 | return NULL;
|
---|
140 | }
|
---|
141 | item = head;
|
---|
142 | head = item->next;
|
---|
143 | if(head == NULL) {
|
---|
144 | tail = NULL;
|
---|
145 | }
|
---|
146 | itemdata = item->dwItemData;
|
---|
147 | delete item;
|
---|
148 | return itemdata;
|
---|
149 | }
|
---|
150 | //******************************************************************************
|
---|
151 | //******************************************************************************
|
---|
152 |
|
---|