10 These functions merge two vectors using marked positions.
11 The original vectors are deleted.
14 template<class V, class E>
16 handleOne(E& encoder, pair_type& run, usint position)
24 if(position == run.first + run.second)
29 encoder.setRun(run.first, run.second);
35 template<class V, class E>
37 handleRun(E& encoder, pair_type& run, pair_type& next, usint limit)
41 run.first = next.first;
42 run.second = std::min(limit - run.first, next.second);
43 next.first += run.second;
44 next.second -= run.second;
48 if(next.first == run.first + run.second)
50 usint cont = std::min(limit - next.first, next.second);
57 encoder.setRun(run.first, run.second);
58 run.first = next.first;
59 run.second = std::min(limit - run.first, next.second);;
60 next.first += run.second;
61 next.second -= run.second;
65 template<class V, class E, class I>
67 mergeVectors(V* first, V* second, usint* positions, usint n, usint size, usint block_size)
69 if((first == 0 && second == 0) || positions == 0) { return 0; }
78 first_run = pair_type(size, 0);
79 first_finished = true;
83 first_iter = new I(*first);
84 first_run = first_iter->selectRun(0, size);
86 first_finished = false;
96 second_iter = new I(*second);
97 second_bit = second_iter->select(0);
100 E encoder(block_size);
101 pair_type run = pair_type(size, 0);
102 for(usint i = 0; i < n; i++, first_run.first++)
104 while(!first_finished && first_run.first < positions[i])
106 handleRun<V, E>(encoder, run, first_run, positions[i]);
107 if(first_run.second == 0)
109 if(first_iter->hasNext())
111 first_run = first_iter->selectNextRun(size);
112 first_run.first += i;
117 first_finished = true;
122 if(i == second_bit) // positions[i] is one
124 handleOne<V, E>(encoder, run, positions[i]);
125 second_bit = second_iter->selectNext();
127 else // positions[i] is zero
131 encoder.setRun(run.first, run.second);
137 while(!first_finished)
139 handleRun<V, E>(encoder, run, first_run, size);
140 if(first_iter->hasNext())
142 first_run = first_iter->selectNextRun(size);
143 first_run.first += n;
151 encoder.setRun(run.first, run.second);
154 delete first_iter; delete second_iter;
155 delete first; delete second;
156 return new V(encoder, size);