sbIndex.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 
26 #ifndef SBINDEX_H_
27 #define SBINDEX_H_
28 
29 #include <vector>
33 template <class KeyT, class IterT, class CompareT>
34 class sbIndex
35 {
36 public:
37  typedef std::vector<IterT> Indexes;
38  typedef typename Indexes::const_iterator const_iterator;
39  typedef typename Indexes::iterator iterator;
43  sbIndex(IterT aBegin,
44  IterT aEnd) :
45  mIndexes(aEnd - aBegin),
46  mEnd(aEnd),
47  mSorted(false)
48  {
49  while (aBegin != aEnd) {
50  mIndexes.push_back(aBegin++);
51  }
52  }
53  sbIndex() : mSorted(false) {}
54  void Build(IterT aBegin,
55  IterT aEnd)
56  {
57  mIndexes.reserve(aEnd - aBegin);
58  while (aBegin != aEnd) {
59  mIndexes.push_back(aBegin++);
60  }
61  sort();
62  }
63  void sort()
64  {
65  std::sort(mIndexes.begin(), mIndexes.end(), CompareT());
66  mSorted = true;
67  }
68  const_iterator find(KeyT const & aData) const
69  {
70  NS_ASSERTION(mSorted || mIndexes.empty(),
71  "sbIndex::find called without a call to sort");
72  const_iterator const iter = std::lower_bound(mIndexes.begin(),
73  mIndexes.end(),
74  aData,
75  mCompareIter);
76  if (iter != mIndexes.end() && !mCompareIter(aData, *iter)) {
77  return iter;
78  }
79  return mIndexes.end();
80  }
81  iterator find(KeyT const & aData)
82  {
83  NS_ASSERTION(mSorted || mIndexes.empty(),
84  "sbIndex::find called without a call to sort");
85  iterator const iter = std::lower_bound(mIndexes.begin(),
86  mIndexes.end(),
87  aData,
88  mCompareIter);
89  if (iter != mIndexes.end() && !mCompareIter(aData, *iter)) {
90  return iter;
91  }
92  return mIndexes.end();
93  }
95  {
96  return mIndexes.begin();
97  }
99  {
100  return mIndexes.end();
101  }
103  {
104  return mIndexes.begin();
105  }
107  {
108  return mIndexes.end();
109  }
110 private:
111  Indexes mIndexes;
112  IterT mEnd;
113  bool mSorted;
114  CompareT mCompareIter;
115 };
116 
117 #endif /* SBINDEXEDVECTOR_H_ */
iterator end()
Definition: sbIndex.h:106
iterator begin()
Definition: sbIndex.h:102
Indexes::iterator iterator
Definition: sbIndex.h:39
iterator find(KeyT const &aData)
Definition: sbIndex.h:81
sbIndex(IterT aBegin, IterT aEnd)
Definition: sbIndex.h:43
void Build(IterT aBegin, IterT aEnd)
Definition: sbIndex.h:54
const_iterator find(KeyT const &aData) const
Definition: sbIndex.h:68
std::vector< IterT > Indexes
Definition: sbIndex.h:37
const_iterator begin() const
Definition: sbIndex.h:94
this _document false
Definition: FeedWriter.js:1085
const_iterator end() const
Definition: sbIndex.h:98
sbIndex()
Definition: sbIndex.h:53
Indexes::const_iterator const_iterator
Definition: sbIndex.h:38
_updateTextAndScrollDataForFrame aData
void sort()
Definition: sbIndex.h:63