QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgschunklist_p.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgschunklist_p.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgschunklist_p.h"
17 
18 #include "qgschunknode_p.h"
19 
21 
22 int QgsChunkList::trueCount() const
23 {
24  int len = 0;
25  QgsChunkListEntry *entry = mHead;
26  while ( entry )
27  {
28  ++len;
29  entry = entry->next;
30  }
31  return len;
32 }
33 
34 void QgsChunkList::insertEntry( QgsChunkListEntry *entry, QgsChunkListEntry *next )
35 {
36  if ( !mHead )
37  {
38  Q_ASSERT( next == nullptr );
39  mTail = mHead = entry;
40  }
41  else
42  {
43  entry->next = next;
44  if ( next == nullptr )
45  {
46  entry->prev = mTail;
47  mTail->next = entry;
48  mTail = entry;
49  }
50  else
51  {
52  entry->prev = next->prev;
53  next->prev = entry;
54  }
55  if ( next == mHead )
56  mHead = entry; // update head if "entry" we was head before
57  }
58  ++mCount;
59 }
60 
61 void QgsChunkList::takeEntry( QgsChunkListEntry *entry )
62 {
63  Q_ASSERT( entry );
64 
65  if ( !entry->prev && !entry->next )
66  {
67  // last item in the list
68  Q_ASSERT( mHead == entry && mTail == entry );
69  mHead = mTail = nullptr;
70  }
71  else if ( !entry->prev )
72  {
73  // head item
74  Q_ASSERT( mHead == entry );
75  entry->next->prev = nullptr;
76  mHead = entry->next;
77  entry->next = nullptr;
78  }
79  else if ( !entry->next )
80  {
81  // tail item
82  Q_ASSERT( mTail == entry );
83  entry->prev->next = nullptr;
84  mTail = entry->prev;
85  entry->prev = nullptr;
86  }
87  else
88  {
89  // ordinary item
90  entry->prev->next = entry->next;
91  entry->next->prev = entry->prev;
92  entry->next = nullptr;
93  entry->prev = nullptr;
94  }
95  --mCount;
96  Q_ASSERT( !entry->prev );
97  Q_ASSERT( !entry->next );
98 }
99 
100 QgsChunkListEntry *QgsChunkList::takeFirst()
101 {
102  QgsChunkListEntry *entry = mHead;
103  takeEntry( entry );
104  return entry;
105 }
106 
107 QgsChunkListEntry *QgsChunkList::takeLast()
108 {
109  QgsChunkListEntry *entry = mTail;
110  takeEntry( entry );
111  return entry;
112 }
113 
114 void QgsChunkList::insertFirst( QgsChunkListEntry *entry )
115 {
116  insertEntry( entry, mHead );
117 }
118 
119 void QgsChunkList::insertLast( QgsChunkListEntry *entry )
120 {
121  insertEntry( entry, nullptr );
122 }
123 
124 bool QgsChunkList::isEmpty() const
125 {
126  return mHead == nullptr;
127 }
128