Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

starbrowser.cpp File Reference

#include <string>
#include <algorithm>
#include <set>
#include "starbrowser.h"

Include dependency graph for starbrowser.cpp:

Go to the source code of this file.

Functions

template<class Pred>
static std::vector< const
Star * > * 
findStars (const StarDatabase &stardb, Pred pred, int nStars)


Function Documentation

template<class Pred>
static std::vector<const Star*>* findStars const StarDatabase stardb,
Pred  pred,
int  nStars
[static]
 

Definition at line 99 of file starbrowser.cpp.

Referenced by StarBrowser::listStars(), and StarBrowser::nearestStar().

00100 {
00101     std::vector<const Star*>* finalStars = new std::vector<const Star*>();
00102     if (nStars == 0)
00103         return finalStars;
00104     if(nStars > 500)
00105         nStars = 500;
00106 
00107     typedef std::multiset<const Star*, Pred> StarSet;
00108     StarSet firstStars(pred);
00109 
00110     int totalStars = stardb.size();
00111     if (totalStars < nStars)
00112         nStars = totalStars;
00113 
00114     // We'll need at least nStars in the set, so first fill
00115     // up the list indiscriminately.
00116     int i = 0;
00117     for (i = 0; i < nStars; i++)
00118         firstStars.insert(stardb.getStar(i));
00119 
00120     // From here on, only add a star to the set if it's
00121     // a better match than the worst matching star already
00122     // in the set.
00123     const Star* lastStar = *--firstStars.end();
00124     for (; i < totalStars; i++)
00125     {
00126         Star* star = stardb.getStar(i);
00127         if (pred(star, lastStar))
00128         {
00129             firstStars.insert(star);
00130             firstStars.erase(--firstStars.end());
00131             lastStar = *--firstStars.end();
00132         }
00133     }
00134 
00135     // Move the best matching stars into the vector
00136     finalStars->reserve(nStars);
00137     for (typename StarSet::const_iterator iter = firstStars.begin();
00138          iter != firstStars.end(); iter++)
00139     {
00140         finalStars->insert(finalStars->end(), *iter);
00141     }
00142 
00143     return finalStars;
00144 }


Generated on Sat Jan 14 22:31:18 2006 for Celestia by  doxygen 1.4.1