• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • skycomponents
starblockfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  starblockfactory.cpp - K Desktop Planetarium
3  -------------------
4  begin : 7 Jun 2008
5  copyright : (C) 2008 by Akarsh Simha
6  email : akarshsimha@gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "starblockfactory.h"
19 
20 // TODO: Remove later
21 #include <cstdio>
22 #include <kdebug.h>
23 
24 // TODO: Implement a better way of deciding this
25 #define DEFAULT_NCACHE 12
26 
27 StarBlockFactory *StarBlockFactory::pInstance = 0;
28 
29 StarBlockFactory *StarBlockFactory::Instance() {
30  if( !pInstance )
31  pInstance = new StarBlockFactory();
32  return pInstance;
33 }
34 
35 StarBlockFactory::StarBlockFactory() {
36  first = NULL;
37  last = NULL;
38  nBlocks = 0;
39  drawID = 0;
40  nCache = DEFAULT_NCACHE;
41 }
42 
43 StarBlockFactory::~StarBlockFactory() {
44  deleteBlocks( nBlocks );
45  if( pInstance )
46  pInstance = 0;
47 }
48 
49 StarBlock *StarBlockFactory::getBlock() {
50  StarBlock *freeBlock = NULL;
51  if( nBlocks < nCache ) {
52  freeBlock = new StarBlock;
53  if( freeBlock ) {
54  ++nBlocks;
55  return freeBlock;
56  }
57  }
58  if( last && ( last->drawID != drawID || last->drawID == 0 ) ) {
59  // kDebug() << "Recycling block with drawID =" << last->drawID << "and current drawID =" << drawID;
60  if( last->parent->block( last->parent->getBlockCount() - 1 ) != last )
61  kDebug() << "ERROR: Goof up here!";
62  freeBlock = last;
63  last = last->prev;
64  if( last )
65  last->next = NULL;
66  if( freeBlock == first )
67  first = NULL;
68  freeBlock->reset();
69  freeBlock->prev = NULL;
70  freeBlock->next = NULL;
71  return freeBlock;
72  }
73  freeBlock = new StarBlock;
74  if( freeBlock )
75  ++nBlocks;
76  return freeBlock;
77 }
78 
79 bool StarBlockFactory::markFirst( StarBlock *block ) {
80 
81  if( !block )
82  return false;
83 
84  // fprintf(stderr, "markFirst()!\n");
85  if( !first ) {
86  // kDebug() << "INFO: Linking in first block" << endl;
87  last = first = block;
88  first->prev = first->next = NULL;
89  first->drawID = drawID;
90  return true;
91  }
92 
93  if( block == first ) { // Block is already in the front
94  block->drawID = drawID;
95  return true;
96  }
97 
98  if( block == last )
99  last = block -> prev;
100 
101  if( block->prev )
102  block->prev->next = block->next;
103 
104  if( block->next )
105  block->next->prev = block->prev;
106 
107  first->prev = block;
108  block->next = first;
109  block->prev = NULL;
110  first = block;
111 
112  block->drawID = drawID;
113 
114  return true;
115 }
116 
117 bool StarBlockFactory::markNext( StarBlock *after, StarBlock *block ) {
118 
119  // fprintf(stderr, "markNext()!\n");
120  if( !block || !after ) {
121  kDebug() << "WARNING: markNext called with NULL argument" << endl;
122  return false;
123  }
124 
125  if( !first ) {
126  kDebug() << "WARNING: markNext called without an existing linked list" << endl;
127  return false;
128  }
129 
130  if( block == after ) {
131  kDebug() << "ERROR: Trying to mark a block after itself!" << endl;
132  return false;
133  }
134 
135  if( block->prev == after ) { // Block is already after 'after'
136  block->drawID = drawID;
137  return true;
138  }
139 
140  if( block == first ) {
141  if( block->next == NULL ) {
142  kDebug() << "ERROR: Trying to mark only block after some other block";
143  return false;
144  }
145  first = block->next;
146  }
147 
148  if( after->getFaintMag() > block->getFaintMag() && block->getFaintMag() != -5 ) {
149  kDebug() << "WARNING: Marking block with faint mag = " << block->getFaintMag() << " after block with faint mag "
150  << after->getFaintMag() << "in trixel" << block->parent->getTrixel();
151  }
152 
153  if( block == last )
154  last = block->prev;
155 
156  if( block->prev )
157  block->prev->next = block->next;
158  if( block->next )
159  block->next->prev = block->prev;
160 
161  block->next = after->next;
162  if( block->next )
163  block->next->prev = block;
164  block->prev = after;
165  after->next = block;
166 
167  if( after == last )
168  last = block;
169 
170  block->drawID = drawID;
171 
172  return true;
173 }
174 
175 /*
176 bool StarBlockFactory::groupMove( StarBlock *start, const int nblocks ) {
177 
178  StarBlock *end;
179 
180  // Check for trivial cases
181  if( !start || nblocks < 0 )
182  return false;
183 
184  if( nblocks == 0 )
185  return true;
186 
187  if( !first )
188  return false;
189 
190  // Check for premature end
191  end = start;
192  for( int i = 1; i < nblocks; ++i ) {
193  if( end == NULL )
194  return false;
195  end = end->next;
196  }
197  if( end == NULL )
198  return false;
199 
200  // Update drawIDs
201  end = start;
202  for( int i = 1; i < nblocks; ++i ) {
203  end->drawID = drawID;
204  end = end->next;
205  }
206  end->drawID = drawID;
207 
208  // Check if we are already in the front
209  if( !start->prev )
210  return true;
211 
212  start->prev->next = end->next;
213  end->next->prev = start->prev;
214 
215  first->prev = end;
216  end->next = first;
217  start->prev = NULL;
218  first = start;
219 }
220 */
221 
222 int StarBlockFactory::deleteBlocks( int nblocks ) {
223  int i;
224  StarBlock *temp;
225 
226  i = 0;
227  while( last != NULL && i != nblocks ) {
228  temp = last->prev;
229  delete last;
230  last = temp;
231  i++;
232  }
233  if( last )
234  last->next = NULL;
235  else
236  first = NULL;
237 
238  kDebug() << nblocks << "StarBlocks freed from StarBlockFactory" << endl;
239 
240  nBlocks -= i;
241  return i;
242 }
243 
244 void StarBlockFactory::printStructure() {
245 
246  StarBlock *cur;
247  Trixel curTrixel = 513; // TODO: Change if we change HTMesh level
248  int index;
249  bool draw = false;
250  cur = first;
251  index = 0;
252  do {
253  if( curTrixel != cur->parent->getTrixel() ) {
254  kDebug() << "Trixel" << cur->parent->getTrixel() << "starts at index" << index << endl;
255  curTrixel = cur->parent->getTrixel();
256  }
257  if( cur->drawID == drawID && !draw ) {
258  kDebug() << "Blocks from index" << index << "are drawn";
259  draw = true;
260  }
261  if( cur->drawID != drawID && draw ) {
262  kDebug() << "Blocks from index" << index << "are not drawn";
263  draw = false;
264  }
265  cur = cur->next;
266  ++index;
267  }while( cur != last );
268 }
269 
270 int StarBlockFactory::freeUnused() {
271  int i;
272  StarBlock *temp;
273 
274  i = 0;
275  while( last != NULL && last->drawID < drawID && i != nBlocks ) {
276  temp = last->prev;
277  delete last;
278  last = temp;
279  i++;
280  }
281  if( last )
282  last->next = NULL;
283  else
284  first = NULL;
285 
286  kDebug() << i << "StarBlocks freed from StarBlockFactory" << endl;
287 
288  nBlocks -= i;
289  return i;
290 }
StarBlockFactory::drawID
quint32 drawID
Definition: starblockfactory.h:99
StarBlock::parent
StarBlockList * parent
Definition: starblock.h:119
StarBlock
Holds a block of stars and various peripheral variables to mark its place in data structures...
Definition: starblock.h:38
StarBlockFactory::markFirst
bool markFirst(StarBlock *block)
Mark a StarBlock as most recently used and sync its drawID with the current drawID.
Definition: starblockfactory.cpp:79
StarBlock::drawID
quint32 drawID
Definition: starblock.h:122
StarBlockList::getBlockCount
int getBlockCount()
Returns the total number of blocks in theis StarBlockList.
Definition: starblocklist.h:100
StarBlockFactory::~StarBlockFactory
~StarBlockFactory()
Destructor Deletes the linked list that maintains the Cache, sets the pointer to NULL.
Definition: starblockfactory.cpp:43
StarBlockFactory
A factory that creates StarBlocks and recycles them in an LRU Cache.
Definition: starblockfactory.h:32
StarBlockFactory::freeUnused
int freeUnused()
Frees all StarBlocks that are not used in this draw cycle.
Definition: starblockfactory.cpp:270
StarBlockList::getTrixel
Trixel getTrixel()
Returns the trixel that this SBL is meant for.
Definition: starblocklist.h:112
starblockfactory.h
StarBlock::next
StarBlock * next
Definition: starblock.h:121
StarBlock::reset
void reset()
Reset this StarBlock's data, for reuse of the StarBl.
Definition: starblock.cpp:39
Trixel
unsigned int Trixel
Definition: htmesh/typedef.h:4
StarBlock::getFaintMag
float getFaintMag()
Return the magnitude of the faintest star in this StarBlock.
Definition: starblock.h:104
StarBlockFactory::Instance
static StarBlockFactory * Instance()
Definition: starblockfactory.cpp:29
StarBlockFactory::markNext
bool markNext(StarBlock *after, StarBlock *block)
Rank a given StarBlock after another given StarBlock in the LRU list and sync its drawID with the cur...
Definition: starblockfactory.cpp:117
StarBlock::prev
StarBlock * prev
Definition: starblock.h:120
StarBlockFactory::getBlock
StarBlock * getBlock()
Return a StarBlock available for use.
Definition: starblockfactory.cpp:49
StarBlockFactory::printStructure
void printStructure()
Prints the structure of the cache, for debugging.
Definition: starblockfactory.cpp:244
DEFAULT_NCACHE
#define DEFAULT_NCACHE
Definition: starblockfactory.cpp:25
StarBlockList::block
StarBlock * block(unsigned int i)
Returns the i-th block in this StarBlockList.
Definition: starblocklist.h:88
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal