1 /**
2  * Copyright (c) 2007-2012, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include "spookyhash/SpookyV2.h"
33 
34 #include "sequence_matcher.hh"
35 
36 using namespace std;
37 
sequence_matcher(field_col_t & example)38 sequence_matcher::sequence_matcher(field_col_t &example)
39 {
40     for (field_col_t::iterator col_iter = example.begin();
41          col_iter != example.end();
42          ++col_iter) {
43         std::string first_value;
44         field       sf;
45 
46         sf.sf_value = *col_iter;
47         for (field_row_t::iterator row_iter = (*col_iter).begin();
48              row_iter != (*col_iter).end();
49              ++row_iter) {
50             if (row_iter == (*col_iter).begin()) {
51                 first_value = *row_iter;
52             }
53             else if (first_value != *row_iter) {
54                 sf.sf_type = FT_CONSTANT;
55             }
56         }
57         if (sf.sf_type == FT_VARIABLE) {
58             sf.sf_value.clear();
59         }
60         this->sm_fields.push_back(sf);
61     }
62     this->sm_count = example.front().size();
63 }
64 
identity(const std::vector<string> & values,id_t & id_out)65 void sequence_matcher::identity(const std::vector<string> &values,
66                                 id_t &id_out)
67 {
68     SpookyHash context;
69     int     lpc = 0;
70 
71     context.Init(0, 0);
72     for (std::list<field>::iterator iter = sm_fields.begin();
73          iter != sm_fields.end();
74          ++iter, lpc++) {
75         if (iter->sf_type == FT_VARIABLE) {
76             context.Update( values[lpc].c_str(),
77                             values[lpc].length() + 1);
78         }
79     }
80     context.Final(id_out.out(0), id_out.out(1));
81 }
82