File: RandomAccessLList.h

package info (click to toggle)
clustalx 2.1%2Blgpl-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,332 kB
  • ctags: 3,424
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 11
file content (196 lines) | stat: -rw-r--r-- 5,971 bytes parent folder | download | duplicates (12)
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
/**
 * Author: Mark Larkin
 * 
 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
 */
/**
 * Author: Mark Larkin UCD Conway Institute
 *
 * This class is to be used as a linked list type container. 
 * The features are as follows:
 * 1) It allows efficient deletions anywhere.
 * 2) It also allows efficient random access.
 * 3) Its maximum size is fixed at creation.
 * 4) Items can only be added at the end.
 * 5) when it is destroyed it will delete the structure, but not the elements.
 *
 * Limitations:
 * It returns the ListElements directly, this is not the best way. A better way would be
 * to return an iterator of some kind, but this would take alot more work. At the moment
 * it is possible to have 2 LList objects and get an element from one list, and delete
 * it from another list. But this is ok for the time being. I will fix this later.
 */
#ifndef RANDOMACCESSLLIST_H
#define RANDOMACCESSLLIST_H

namespace clustalw
{

template <class T>
class ListElement
{
    public:
        ListElement()
        {
            // Do nothing
        }
        
        ListElement(T* e, ListElement* n, ListElement* p, unsigned int _index)
        {
            element = e;
            next = n;
            prev = p;
            index = _index;
        }
                
        ~ListElement()
        {
            element = 0;
            next = 0;
        }
        
        unsigned int getIndex(){return index;}
                
        T* element;
        ListElement* next;
        ListElement* prev;
    private:    
        unsigned int index;
};

template <class T>
class RandomAccessLList
{
    public:
        RandomAccessLList(int size)
         : maxSize(size),
           numElements(0)
        {
            elements = new ListElement<T>*[size];
            for(int i = 0; i < size; i++)
            {
                elements[i] = 0;
            }
        }
        
        ~RandomAccessLList()
        {
            for(int i = 0; i < maxSize; i++)
            {
                if(elements[i] != 0)
                {
                    delete elements[i];
                }
            }
            delete [] elements;        
        }
        
        unsigned int getNumElements(){return numElements;}
        
        void addElementToBack(T* element)
        {
            if(numElements < maxSize)
            {
                // Add the element
                elements[numElements] = new ListElement<T>(element, 0, 0, numElements);
                
                if(numElements == 0)
                {
                    // First element
                    elements[numElements]->next = 0;
                    elements[numElements]->prev = 0;
                    firstElementInList = elements[numElements];
                    numElements++;             
                }
                else if(numElements > 0) // If it is not the first element
                {
                    elements[numElements - 1]->next = elements[numElements];
                    elements[numElements]->prev = elements[numElements - 1];
                    elements[numElements]->next = 0;
                    numElements++;
                }
            }
        }
        
        ListElement<T>* getAt(int index)
        {
            if(index >= 0 && index < maxSize)
            {
                return elements[index];
            }
            else
            {
                cout << "\nCannot get item : " << index << "\n";
                return 0;
            }
        }
        
        ListElement<T>* getFirst()
        {
            return firstElementInList;
        }        
        
        void removeItem(ListElement<T>* item)
        {
            if(item != 0 )
            {
                int index = item->getIndex();
                if(elements[index] != 0)
                { 
                    if(item->prev == 0 && item->next == 0) // Last item in the list
                    {
                        firstElementInList = 0;
                        delete item; // Not sure if this is good or not.
                        item = 0;
                        numElements--;
                    }
                    else if(item->prev == 0) // remove First in the list
                    {
                        firstElementInList = item->next;
                        firstElementInList->prev = 0;
                        item->next = 0;
                        delete item; // Not sure if this is good or not.
                        item = 0;
                        numElements--;
                    }
                    else if(item->next == 0) // remove last item
                    {
                        ListElement<T>* prevElem = item->prev;
                        prevElem->next = 0;
                        item->prev = 0;
                        delete item; // Not sure if this is good or not.
                        item = 0;
                        numElements--;                
                    }
                    else // remove middle item
                    {
                        ListElement<T>* prevElem = item->prev;
                        ListElement<T>* nextElem = item->next;
                        prevElem->next = nextElem;
                        nextElem->prev = prevElem;
                        item->prev = 0;
                        item->next = 0;
                        delete item; // Not sure if this is good or not.
                        item = 0;
                        numElements--;                 
                    }
                    elements[index] = 0;
                }
            }
            else
            {
                cout << "ERROR: trying to remove item outside the bounds\n";
            }            
        }
        
    private:    
        ListElement<T>** elements;
        ListElement<T>* firstElementInList;
        int maxSize;
        unsigned int numElements;
        
};

}

#endif