1 /**
2    dtl -- Diff Template Library
3 
4    In short, Diff Template Library is distributed under so called "BSD license",
5 
6    Copyright (c) 2015 Tatsuhiko Kubo <cubicdaiya@gmail.com>
7    All rights reserved.
8 
9    Redistribution and use in source and binary forms, with or without modification,
10    are permitted provided that the following conditions are met:
11 
12    * Redistributions of source code must retain the above copyright notice,
13    this list of conditions and the following disclaimer.
14 
15    * Redistributions in binary form must reproduce the above copyright notice,
16    this list of conditions and the following disclaimer in the documentation
17    and/or other materials provided with the distribution.
18 
19    * Neither the name of the authors nor the names of its contributors
20    may be used to endorse or promote products derived from this software
21    without specific prior written permission.
22 
23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 
36 /* If you use this library, you must include dtl.hpp only. */
37 
38 #ifndef DTL_SES_H
39 #define DTL_SES_H
40 
41 namespace dtl {
42 
43     /**
44      * Shortest Edit Script template class
45      */
46     template <typename elem>
47     class Ses : public Sequence< elem >
48     {
49     private :
50         typedef pair< elem, elemInfo > sesElem;
51         typedef vector< sesElem >      sesElemVec;
52     public :
53 
Ses()54         Ses () : onlyAdd(true), onlyDelete(true), onlyCopy(true), deletesFirst(false) {
55             nextDeleteIdx = 0;
56         }
Ses(bool moveDel)57         Ses (bool moveDel) : onlyAdd(true), onlyDelete(true), onlyCopy(true), deletesFirst(moveDel) {
58             nextDeleteIdx = 0;
59         }
~Ses()60         ~Ses () {}
61 
isOnlyAdd() const62         bool isOnlyAdd () const {
63             return onlyAdd;
64         }
65 
isOnlyDelete() const66         bool isOnlyDelete () const {
67             return onlyDelete;
68         }
69 
isOnlyCopy() const70         bool isOnlyCopy () const {
71             return onlyCopy;
72         }
73 
isOnlyOneOperation() const74         bool isOnlyOneOperation () const {
75             return isOnlyAdd() || isOnlyDelete() || isOnlyCopy();
76         }
77 
isChange() const78         bool isChange () const {
79             return !onlyCopy;
80         }
81 
82         using Sequence< elem >::addSequence;
addSequence(elem e,long long beforeIdx,long long afterIdx,const edit_t type)83         void addSequence (elem e, long long beforeIdx, long long afterIdx, const edit_t type) {
84             elemInfo info;
85             info.beforeIdx = beforeIdx;
86             info.afterIdx  = afterIdx;
87             info.type      = type;
88             sesElem pe(e, info);
89             if (!deletesFirst) {
90                 sequence.push_back(pe);
91             }
92             switch (type) {
93             case SES_DELETE:
94                 onlyCopy   = false;
95                 onlyAdd    = false;
96                 if (deletesFirst) {
97                     sequence.insert(sequence.begin() + nextDeleteIdx, pe);
98                     nextDeleteIdx++;
99                 }
100                 break;
101             case SES_COMMON:
102                 onlyAdd    = false;
103                 onlyDelete = false;
104                 if (deletesFirst) {
105                     sequence.push_back(pe);
106                     nextDeleteIdx = sequence.size();
107                 }
108                 break;
109             case SES_ADD:
110                 onlyDelete = false;
111                 onlyCopy   = false;
112                 if (deletesFirst) {
113                     sequence.push_back(pe);
114                 }
115                 break;
116             }
117         }
118 
getSequence() const119         sesElemVec getSequence () const {
120             return sequence;
121         }
122     private :
123         sesElemVec sequence;
124         bool       onlyAdd;
125         bool       onlyDelete;
126         bool       onlyCopy;
127         bool       deletesFirst;
128         size_t     nextDeleteIdx;
129     };
130 }
131 
132 #endif // DTL_SES_H
133