2014-12-15 01:27:03 +01:00
|
|
|
|
/*
|
2015-01-04 12:36:13 +01:00
|
|
|
|
LIST-DIFF-APPLICATION.hpp - consume and apply a list diff
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2014, Hermann Vosseler <Ichthyostega@web.de>
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file list-diff-application.hpp
|
2014-12-15 03:21:19 +01:00
|
|
|
|
** Apply a "list diff" to a concrete sequence of elements in a container.
|
|
|
|
|
|
** This header provides specialisation(s) of the DiffApplicationStrategy to
|
|
|
|
|
|
** actual containers, choosing an implementation approach suitable for this
|
2017-04-02 04:22:51 +02:00
|
|
|
|
** specific kind of container. Together with a lib::diff::DiffApplicator,
|
|
|
|
|
|
** this allows to receive the description of changes (as a linearised sequence
|
|
|
|
|
|
** of DiffStep tokens) and apply them to a given concrete sequence of data
|
2014-12-15 03:21:19 +01:00
|
|
|
|
** elements, thereby transforming the contents of this target sequence.
|
2014-12-15 01:27:03 +01:00
|
|
|
|
**
|
|
|
|
|
|
** @see diff-list-application-test.cpp
|
2014-12-15 03:21:19 +01:00
|
|
|
|
** @see ListDiffLanguage
|
2014-12-15 01:27:03 +01:00
|
|
|
|
**
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_DIFF_LIST_DIFF_APPLICATION_H
|
|
|
|
|
|
#define LIB_DIFF_LIST_DIFF_APPLICATION_H
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-15 03:21:19 +01:00
|
|
|
|
#include "lib/diff/list-diff.hpp"
|
2014-12-15 01:27:03 +01:00
|
|
|
|
#include "lib/format-string.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2014-12-15 03:21:19 +01:00
|
|
|
|
namespace diff{
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
2014-12-15 03:21:19 +01:00
|
|
|
|
using util::_Fmt;
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
using std::move;
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* concrete strategy to apply a list diff to a target sequence given as vector.
|
|
|
|
|
|
* The implementation swaps aside the existing content of the target sequence
|
|
|
|
|
|
* and then consumes it step by step, while building up the altered content
|
|
|
|
|
|
* within the previously emptied target vector. Whenever possible, elements
|
|
|
|
|
|
* are moved directly to the target location.
|
|
|
|
|
|
* @throws lumiera::error::State when diff application fails due to the
|
|
|
|
|
|
* target sequence being different than assumed by the given diff.
|
|
|
|
|
|
* @warning behaves only EX_SANE in case of diff application errors,
|
|
|
|
|
|
* i.e. only partially modified / rebuilt sequence might be
|
|
|
|
|
|
* in the target when diff application is aborted
|
2015-01-02 13:18:25 +01:00
|
|
|
|
* @see #ListDiffInterpreter explanation of the verbs
|
2014-12-15 01:27:03 +01:00
|
|
|
|
*/
|
|
|
|
|
|
template<typename E, typename...ARGS>
|
|
|
|
|
|
class DiffApplicationStrategy<vector<E,ARGS...>>
|
|
|
|
|
|
: public ListDiffInterpreter<E>
|
|
|
|
|
|
{
|
|
|
|
|
|
using Vec = vector<E,ARGS...>;
|
|
|
|
|
|
using Iter = typename Vec::iterator;
|
|
|
|
|
|
|
|
|
|
|
|
Vec orig_;
|
|
|
|
|
|
Vec& seq_;
|
|
|
|
|
|
Iter pos_;
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
end_of_target()
|
|
|
|
|
|
{
|
|
|
|
|
|
return pos_ == orig_.end();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
__expect_in_target (E const& elm, Literal oper)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (end_of_target())
|
|
|
|
|
|
throw error::State(_Fmt("Unable to %s element %s from target as demanded; "
|
|
|
|
|
|
"no (further) elements in target sequence") % oper % elm
|
2018-04-02 01:48:51 +02:00
|
|
|
|
, LERR_(DIFF_CONFLICT));
|
2014-12-15 01:27:03 +01:00
|
|
|
|
if (*pos_ != elm)
|
|
|
|
|
|
throw error::State(_Fmt("Unable to %s element %s from target as demanded; "
|
|
|
|
|
|
"found element %s on current target position instead")
|
|
|
|
|
|
% oper % elm % *pos_
|
2018-04-02 01:48:51 +02:00
|
|
|
|
, LERR_(DIFF_CONFLICT));
|
2014-12-15 01:27:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-01-02 13:18:25 +01:00
|
|
|
|
__expect_further_elements (E const& elm)
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (end_of_target())
|
2015-01-02 13:18:25 +01:00
|
|
|
|
throw error::State(_Fmt("Premature end of target sequence, still expecting element %s; "
|
|
|
|
|
|
"unable to apply diff further.") % elm
|
2018-04-02 01:48:51 +02:00
|
|
|
|
, LERR_(DIFF_CONFLICT));
|
2014-12-15 01:27:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
__expect_found (E const& elm, Iter const& targetPos)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (targetPos == orig_.end())
|
|
|
|
|
|
throw error::State(_Fmt("Premature end of sequence; unable to locate "
|
2015-01-02 13:18:25 +01:00
|
|
|
|
"element %s in the remainder of the target.") % elm
|
2018-04-02 01:48:51 +02:00
|
|
|
|
, LERR_(DIFF_CONFLICT));
|
2014-12-15 01:27:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == Implementation of the diff application primitives == */
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-24 02:42:13 +02:00
|
|
|
|
ins (E const& elm) override
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
seq_.push_back(elm);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-24 02:42:13 +02:00
|
|
|
|
del (E const& elm) override
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
__expect_in_target(elm, "remove");
|
|
|
|
|
|
++pos_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-24 02:42:13 +02:00
|
|
|
|
pick (E const& elm) override
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
__expect_in_target(elm, "pick");
|
|
|
|
|
|
seq_.push_back (move(*pos_));
|
|
|
|
|
|
++pos_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-24 02:42:13 +02:00
|
|
|
|
skip (E const& elm) override
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
2015-01-02 13:18:25 +01:00
|
|
|
|
__expect_further_elements (elm);
|
2014-12-15 01:27:03 +01:00
|
|
|
|
++pos_;
|
2015-01-02 13:18:25 +01:00
|
|
|
|
} // assume the actual content has been moved away by a previous find()
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-24 02:42:13 +02:00
|
|
|
|
find (E const& elm) override
|
2015-01-02 13:18:25 +01:00
|
|
|
|
{
|
|
|
|
|
|
__expect_further_elements (elm);
|
|
|
|
|
|
Iter found = std::find(pos_, orig_.end(), elm);
|
|
|
|
|
|
__expect_found (elm, found);
|
|
|
|
|
|
seq_.push_back (move(*found));
|
|
|
|
|
|
} // consume and leave waste, expected to be cleaned-up by skip() later
|
2014-12-15 01:27:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit
|
|
|
|
|
|
DiffApplicationStrategy(vector<E>& targetVector)
|
|
|
|
|
|
: seq_(targetVector)
|
2015-10-31 05:15:47 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
initDiffApplication()
|
2014-12-15 01:27:03 +01:00
|
|
|
|
{
|
2015-10-31 05:15:47 +01:00
|
|
|
|
swap (seq_, orig_);
|
|
|
|
|
|
seq_.reserve (orig_.size() * 120 / 100); // heuristics for storage pre-allocation
|
|
|
|
|
|
pos_ = orig_.begin();
|
2014-12-15 01:27:03 +01:00
|
|
|
|
}
|
2019-12-15 15:06:04 +01:00
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
completeDiffApplication()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (not end_of_target())
|
|
|
|
|
|
throw error::State(_Fmt("Not all source data consumed after diff application. "
|
|
|
|
|
|
"Element %s waiting to be consumed") % *pos_
|
|
|
|
|
|
, LERR_(DIFF_STRUCTURE));
|
|
|
|
|
|
// discard storage
|
|
|
|
|
|
orig_.clear();
|
|
|
|
|
|
}
|
2014-12-15 01:27:03 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-15 03:21:19 +01:00
|
|
|
|
}} // namespace lib::diff
|
|
|
|
|
|
#endif /*LIB_DIFF_LIST_DIFF_APPLICATION_H*/
|