Xpace
sort.h
Go to the documentation of this file.
1 
2 /*************************************************************
3  **
4  ** @file index/sort.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 #ifndef XPACE_SORT_H
13 #define XPACE_SORT_H
14 
15 #include "index/reflist/reflist_bitmap.h"
16 
17 namespace Xpace
18 {
19  struct NullList
20  {
21  void add
23  {
24  }
25  };
26  extern NullList null_list; // to quiet GCC warning
27 
28  template <typename LIST = NullList>
29  class SortToSink
30  {
31  public:
33  (const RefListCursor& searchResult,
34  const std::vector<SortContext>& sort,
35  LIST* list = &null_list) :
36  state(new st(searchResult, sort, list))
37  {
38  }
39 
40  ~SortToSink
41  ()
42  {
43  }
44 
45  int64 operator()
46  (int64 doc)
47  {
48  return state->next(doc);
49  }
50 
51  void complete
52  ()
53  {
54  return state->complete();
55  }
56 
57  private:
58  class st
59  {
60  public:
61  st
62  (const RefListCursor& searchResult,
63  const std::vector<SortContext>& sort,
64  LIST *list) :
65  search_result((searchResult.hasRef(0), searchResult.constImpl()->makeRandom())), // force result to be random
66  sort(sort),
67  sort_iter(sort[0].index->begin()),
68  srlc(sort_iter.getRefList().impl()),
69  sort_rlc(dynamic_cast<RLC_Index*>(srlc.impl())),
70  list(list)
71  {
72  assert(sort_rlc);
73  }
74 
75  int64 next
76  (int64 doc)
77  {
78  while (1)
79  {
80  if (!*sort_rlc)
81  {
82  if (!sort_iter.move(1))
83  return NDR_abort_true;
84  srlc = sort_iter.getRefList();
85  sort_rlc = dynamic_cast<RLC_Index*>(srlc.impl());
86  assert(sort_rlc);
87  }
88 
89  bool b;
90  uint64 ref(*sort_rlc->getRef());
91  if (search_result->hasRef(&b, ref), b)
92  {
93  list->add(doc = ref);
94  sort_rlc->move(&b, 1);
95  return doc;
96  }
97  }
98  }
99 
100  void complete
101  ()
102  {
103  while (1)
104  {
105  if (!*sort_rlc)
106  {
107  if (!sort_iter.move(1))
108  return;
109  srlc = sort_iter.getRefList();
110  sort_rlc = dynamic_cast<RLC_Index*>(srlc.impl());
111  }
112  bool b;
113  uint ref(*sort_rlc->getRef());
114  if (search_result->hasRef(&b, ref), b)
115  list->add(ref);
116  sort_rlc->move(&b, 1);
117  }
118  }
119 
120  private:
121  st
122  (const st&);
123  st& operator=
124  (const st&);
125 
126  const RLC_Bitmap* search_result;
127  const std::vector<SortContext>& sort;
128  Index::Iter sort_iter;
129  RefListCursor srlc;
130  RLC_Index* sort_rlc;
131 
132  LIST* list;
133  };
134 
135  std::shared_ptr<st> state;
136  };
137 }
138 
139 #endif
unsigned int uint
Definition: types.h:75
NullList null_list
unsigned long long uint64
Definition: types.h:87
index term iterator
Definition: index.h:130
void add(uint64)
Definition: sort.h:22
bool hasRef(uint64 ref, uint64 *pos=0) const
Does this reference list have this ref?
long long int64
Definition: types.h:86
A cursor into a list of references - e.g., a search result A single reference is an array of uint64s...
Definition: reflist.h:26
Xpace project main namespace
Definition: datetime.h:18
void complete()
Definition: sort.h:52

current as of Wed Jun 10 2026 12:00:05