BitMagic-C++
svsample09.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2019 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For more information please visit: http://bitmagic.io
17*/
18
19/** \example svsample09.cpp
20 Mismatch search. bm::sparse_vector_find_first_mismatch
21
22 \sa bm::sparse_vector
23 \sa bm::sparse_vector_find_first_mismatch
24
25 \sa bm::bvector<>::find_first_mismatch()
26
27*/
28
29/*! \file svsample09.cpp
30 \brief Example: Use of sparse vector mismatch search
31*/
32
33#include <assert.h>
34#include <stdlib.h>
35
36#include <iostream>
37#include <vector>
38#include <utility>
39
40#include "bm.h"
41#include "bmsparsevec.h"
42#include "bmsparsevec_algo.h"
43#include "bmundef.h" /* clear the pre-proc defines from BM */
44
45using namespace std;
46
47
49
50
51int main(void)
52{
53 try
54 {
56
57 cout << "sparse vectors without NULL:" << endl;
58 {
59 svector_u32 sv1, sv2;
60
61 sv1.push_back(1);
62 sv1.push_back(2);
63 sv1.push_back(2);
64 sv1.push_back(3);
65
66 sv2 = sv1;
67
68 bool found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
69 if (found)
70 std::cout << "Mismatch found." << endl; // this would be an error
71 else
72 std::cout << "Mismatch not found" << endl; // identical vectors
73
74 // modify sv2 to introduce a mismatch
75
76 sv2[2] = 0;
77 found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
78 if (found)
79 {
80 std::cout << "Mismatch found at: " << pos << endl; // at 2
81 }
82 }
83
84 // bm::sparse_vector_find_first_mismatch works with NULL-able vectors
85 // first NULL (unsassigned) between vectors is a mimatch
86 //
87 cout << endl << "sparse vectors with NULL:" << endl;
88 {
91
92 sv1[1] = 1;
93 sv1[2] = 2;
94 sv1.set_null(3); // set element 3 to NULL
95 sv1[4] = 0;
96
97 sv2 = sv1;
98
99 bool found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
100 if (found)
101 std::cout << "Mismatch found." << endl; // this would be an error
102 else
103 std::cout << "Mismatch not found" << endl; // identical vectors
104
105 sv2[4] = 10;
106 found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
107 if (found)
108 {
109 std::cout << "Mismatch found at: " << pos << endl; // at 4
110 }
111
112
113 // set element 3 to 0, now we have a situation when element 3 in both
114 // vectors has value 0, except it is NULL value in vector 1
115 // mismatch search should detect it
116 //
117 sv2[3] = 0;
118 found = bm::sparse_vector_find_first_mismatch(sv1, sv2, pos);
119 if (found)
120 {
121 std::cout << "Mismatch found at: " << pos << endl; // at 3
122 }
123 }
124
125 }
126 catch(std::exception& ex)
127 {
128 std::cerr << ex.what() << std::endl;
129 return 1;
130 }
131
132
133
134 return 0;
135}
136
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
Algorithms for bm::sparse_vector.
pre-processor un-defines to avoid global space pollution (internal)
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
void set_null(size_type idx)
set specified element to unassigned value (NULL)
Definition: bmsparsevec.h:1146
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1838
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:229
bool sparse_vector_find_first_mismatch(const SV &sv1, const SV &sv2, typename SV::size_type &midx, bm::null_support null_proc=bm::use_null)
Find first mismatch (element which is different) between two sparse vectors (uses linear scan in bit-...
bm::sparse_vector< unsigned, bm::bvector<> > svector_u32
Definition: svsample09.cpp:48
int main(void)
Definition: svsample09.cpp:51