OTest2
A C++ testing framework
difflogarray.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OTest2__LIB_DIFFLOGARRAY_H_
21 #define OTest2__LIB_DIFFLOGARRAY_H_
22 
23 #include <vector>
24 
25 #include <otest2/hirschberg.h>
26 
27 namespace OTest2 {
28 
38 struct DiffRecord {
40  int left_index;
42 };
43 
47 typedef std::vector<DiffRecord> DiffLogArray;
48 
53  private:
54  DiffLogArray* array;
55 
56  public:
62  explicit DiffLogBuilderArray(
63  DiffLogArray* array_);
64 
68  virtual ~DiffLogBuilderArray();
69 
70  /* -- avoid copying */
72  const DiffLogBuilderArray&) = delete;
74  const DiffLogBuilderArray&) = delete;
75 
76  /* -- diff log builder interface */
77  virtual void addMatch(
78  int left_index_,
79  int right_index_);
80  virtual void addChange(
81  int left_index_,
82  int right_index_);
83  virtual void addDelete(
84  int right_index_);
85  virtual void addInsert(
86  int left_index_);
87 };
88 
99 template<typename Type_, typename ScoreFce_ = DiffScoreLCS<Type_> >
101  const Type_ left_[],
102  std::size_t left_len_,
103  const Type_ right_[],
104  std::size_t right_len_,
105  DiffLogArray& diff_log_,
106  ScoreFce_ score_fce_ = ScoreFce_()) {
107  /* -- I expect 1/4 changes of the average length */
108  DiffLogArray diff_;
109  diff_.reserve((left_len_ + right_len_) / 8);
110 
111  /* -- do the job */
112  DiffLogBuilderArray builder_(&diff_);
113  hirschbergDiff(left_, left_len_, right_, right_len_, builder_, score_fce_);
114 
115  /* -- return the result */
116  diff_log_.swap(diff_);
117 }
118 
119 } /* namespace OTest2 */
120 
121 #endif /* OTest2__LIB_DIFFLOGARRAY_H_ */
OTest2::DiffLogBuilderArray::addDelete
virtual void addDelete(int right_index_)
Add deleted item from the right sequence.
Definition: difflogarray.cpp:48
OTest2::hirschbergDiff
void hirschbergDiff(const Type_ left_[], std::size_t left_len_, const Type_ right_[], std::size_t right_len_, DiffLogArray &diff_log_, ScoreFce_ score_fce_=ScoreFce_())
Compute the diff algorithm for specified sequences.
Definition: difflogarray.h:100
OTest2::DiffAction
DiffAction
Definition: hirschberg.h:31
OTest2::DiffLogBuilder
Generic interface of the log builder.
Definition: difflogbuilder.h:30
hirschberg.h
OTest2
Definition: assertbean.h:25
OTest2::DiffLogBuilderArray::addInsert
virtual void addInsert(int left_index_)
Add inserted item to the left sequence.
Definition: difflogarray.cpp:53
OTest2::DiffLogBuilderArray::addChange
virtual void addChange(int left_index_, int right_index_)
Add change of both sequences.
Definition: difflogarray.cpp:42
OTest2::DiffRecord
A diff change record.
Definition: difflogarray.h:38
OTest2::DiffLogArray
std::vector< DiffRecord > DiffLogArray
Ordered (indexes into the sequences) list of diff changes.
Definition: difflogarray.h:47
OTest2::DiffLogBuilderArray::addMatch
virtual void addMatch(int left_index_, int right_index_)
Add match of characters in both sequences.
Definition: difflogarray.cpp:36
OTest2::DiffLogBuilderArray::DiffLogBuilderArray
DiffLogBuilderArray(DiffLogArray *array_)
Ctor.
Definition: difflogarray.cpp:25
OTest2::DiffRecord::left_index
int left_index
Definition: difflogarray.h:40
OTest2::DiffRecord::action
DiffAction action
Definition: difflogarray.h:39
OTest2::DiffLogBuilderArray
Builder of the diff array.
Definition: difflogarray.h:52
OTest2::DiffLogBuilderArray::operator=
DiffLogBuilderArray & operator=(const DiffLogBuilderArray &)=delete
OTest2::DiffLogBuilderArray::~DiffLogBuilderArray
virtual ~DiffLogBuilderArray()
Dtor.
Definition: difflogarray.cpp:32
OTest2::DiffRecord::right_index
int right_index
Definition: difflogarray.h:41