diff options
Diffstat (limited to '')
-rw-r--r-- | src/list.h | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -142,6 +142,55 @@ namespace Bu | |||
142 | } | 142 | } |
143 | 143 | ||
144 | /** | 144 | /** |
145 | * Insert a new item in sort order by searching for the first item that | ||
146 | * is larger and inserting this before it, or at the end if none are | ||
147 | * larger. If this is the only function used to insert data in the | ||
148 | * List all items will be sorted. To use this, the value type must | ||
149 | * support the > operator. | ||
150 | */ | ||
151 | void insertSorted( const value &v ) | ||
152 | { | ||
153 | Link *pNew = la.allocate( 1 ); | ||
154 | pNew->pValue = va.allocate( 1 ); | ||
155 | va.construct( pNew->pValue, v ); | ||
156 | nSize++; | ||
157 | if( pFirst == NULL ) | ||
158 | { | ||
159 | // Empty list | ||
160 | pFirst = pLast = pNew; | ||
161 | pNew->pNext = pNew->pPrev = NULL; | ||
162 | return; | ||
163 | } | ||
164 | else | ||
165 | { | ||
166 | Link *pCur = pFirst; | ||
167 | for(;;) | ||
168 | { | ||
169 | if( !(v > *(pCur->pValue)) ) | ||
170 | { | ||
171 | pNew->pNext = pCur; | ||
172 | pNew->pPrev = pCur->pPrev; | ||
173 | pCur->pPrev = pNew; | ||
174 | if( pNew->pPrev == NULL ) | ||
175 | pFirst = pNew; | ||
176 | else | ||
177 | pNew->pPrev->pNext = pNew; | ||
178 | return; | ||
179 | } | ||
180 | pCur = pCur->pNext; | ||
181 | if( pCur == NULL ) | ||
182 | { | ||
183 | pNew->pNext = NULL; | ||
184 | pNew->pPrev = pLast; | ||
185 | pLast->pNext = pNew; | ||
186 | pLast = pNew; | ||
187 | return; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | |||
193 | /** | ||
145 | * An iterator to iterate through your list. | 194 | * An iterator to iterate through your list. |
146 | */ | 195 | */ |
147 | typedef struct iterator | 196 | typedef struct iterator |