File: baseq.cpp

package info (click to toggle)
bcpp 0.0.20131209-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 960 kB
  • sloc: cpp: 4,956; sh: 3,151; makefile: 265
file content (219 lines) | stat: -rw-r--r-- 6,207 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef _QUEUE_LIST_CODE
#define _QUEUE_LIST_CODE

// $Id: baseq.cpp,v 1.5 2005/04/10 18:29:14 tom Exp $
// Code written by Steven De Toni ACBC 11

// These class methods used to implement a object that holds other objects
// that are descendant of ANYOBJECT (i.e universal container).

#include "baseq.h"
#include <stdio.h>      // NULL CONSTANT


// ############################################################################
// #### QueueList Class ####
// #########################

// ############################ Protected Methods #############################

// Create new list item and place programmer's data within it,
// make new links with previous item if wished.
// returns a pointer to the new item, NULL if operation failed.
//
// Parameters:
//     pItem    : Pointer to the object to be stored.
//     LinkItem : Pointer to end of list where item to be added.
//
// Return Values:
//     LinkItem : returns pointer to newly added item in list,
//                NULL if operation failed. However items within list
//                before hand still exist.
LinkItem* QueueList::newItem (ANYOBJECT* pItem, LinkItem* pEndList)
{
    LinkItem*  pNewStruct = new LinkItem;

    if (pNewStruct != NULL)
    {
        pNewStruct->pLinkedItem    = pEndList;
        pNewStruct->pStoredItem    = pItem;
    }

    return pNewStruct;
}


// ############################## Public Methods ##############################
// ############################### Constructors ###############################
#define MY_DEFAULT \
   itemCount(0), \
   pEndPos(NULL), \
   spaceAvailable(0)

QueueList::QueueList (void)
    : MY_DEFAULT
{
}

// Parameters:
//     pItem    : Pointer to a object to be stored, must be descendant of
//                base class ANYOBJECT.
QueueList::QueueList (ANYOBJECT* pItem)
    : MY_DEFAULT
{
   putLast(pItem);
}

#undef MY_DEFAULT

// ########################### User Methods ###################################

// Place programmers object into list
//
// Parameters:
//     pItem    : Pointer to a object to be stored, must be descendant of
//                base class ANYOBJECT.
//
// Return Values:
//     int      : Returns a error code indicating whether operation was
//                successful.
//                Values:
//                    0 = No Worries
//                   -1 = Arrgh ... No memory
//
int QueueList::putLast (ANYOBJECT* pItem)
{
    LinkItem*   pNewItem = newItem (pItem, pEndPos);
    if (pNewItem != NULL)
    {
        pEndPos = pNewItem;
        itemCount++;
        return 0;
    }
    spaceAvailable = -1;
    return -1;          // could not add item to list!
}


// Take first item placed in Queue, out and return it.
// Type casting is required to return object back to its original
// state.
//
// Return Values:
//     ANYOBJECT* : Pointer to the object that was stored within queue.
//
ANYOBJECT* QueueList::takeNext (void)
{
    if (pEndPos != NULL)
    {
        LinkItem* pUpDateList = pEndPos;
        LinkItem* pStartPos   = pEndPos;

        // move down list until start has been reached
        while (pStartPos->pLinkedItem != NULL)
            pStartPos = pStartPos->pLinkedItem;

        if (pStartPos != pUpDateList) // if not the last item in list
        {
            // retrieve data and delete item from list
            while (pUpDateList->pLinkedItem != pStartPos)
            pUpDateList = pUpDateList->pLinkedItem;
        }
        else
        {
            pEndPos = NULL;     // start new list after all items gone
        }

        ANYOBJECT* pTemp = pStartPos->pStoredItem;  // copy value to user
        pUpDateList->pLinkedItem = NULL;            // make new start of list
        delete pStartPos;                           // delete object
        itemCount--;                                // one less
        if (spaceAvailable)                         // if no memory available before...
            spaceAvailable = 0; // there is now!
        return pTemp;
    }
    else
        return NULL;
}

// Returns the number of items contained within the queue.
//
// Returns Values:
//     int :    Num of items within queue.
//
int QueueList::status (void)        // return number of item in Queue
{
    return itemCount;
}

// Method returns whether last operation failed due to memory allocation
// failure.
//
// Return Values:
//     int  : Returns 1 of two values ...
//            Values:
//              0  =  memory available
//             -1  =  Last memory allocation failed.
//
int QueueList::space (void)     // return Queue space left
{
    return spaceAvailable;      // return -1 if no space available
}

// Methods is used to peek within the queue at objects, and return their
// pointer without taking them out of the queue.
//
// Parameters:
//     NumFromNext : The object number to look at from the start of the
//                   queue. The start of the queue is 1, not 0.
//
// Return Values:
//     ANYOBJECT* : Pointer to the object that is stored within queue,
//                  at said position. Returns NULL if operation failed.
//
ANYOBJECT*   QueueList::peek (int numFromNext)
{
    if (pEndPos != NULL)
    {
        //if (numFromNext > itemCount) //error checking !
        //   return NULL;

        int count = itemCount - numFromNext;
        LinkItem* pStartPos   = pEndPos;

        if (count >= 0)
        {
            // move down list until start has been reached
            while (count > 0)
            {
                  pStartPos = pStartPos->pLinkedItem;
                  count--;
            }

            if (pStartPos != NULL)
                return pStartPos->pStoredItem;
        }
    }
    return NULL;
}



// ############################### Destructor ###############################
// Method will remove all list items from memory if they still exist,
// no garbage collection provided, or used.
//
QueueList::~QueueList (void)
{
    LinkItem* pTemp = pEndPos;

    while (pEndPos != NULL)
    {
        pEndPos = pEndPos->pLinkedItem;   // advance to next item
        delete    pTemp  ->pStoredItem;   // kill data contained
        delete    pTemp;                  // kill item
        pTemp   = pEndPos;
    }
}

#endif