2008-11-05 03:57:18 +01:00
|
|
|
#ifndef ARABICA_XSLT_PRECEDENCE_HPP
|
|
|
|
#define ARABICA_XSLT_PRECEDENCE_HPP
|
|
|
|
|
2009-02-26 15:38:23 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
class Precedence
|
|
|
|
{
|
|
|
|
public:
|
2008-11-19 18:26:07 +01:00
|
|
|
static const Precedence& InitialPrecedence()
|
2008-11-05 23:55:53 +01:00
|
|
|
{
|
|
|
|
static Precedence initial_(0);
|
|
|
|
return initial_;
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
static const Precedence& FrozenPrecedence()
|
2008-11-05 23:55:53 +01:00
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
static Precedence frozen_;
|
2008-11-05 23:55:53 +01:00
|
|
|
return frozen_;
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
Precedence() :
|
2008-11-25 13:27:33 +01:00
|
|
|
precedence_(),
|
|
|
|
children_(0)
|
2008-11-05 03:57:18 +01:00
|
|
|
{
|
2008-11-05 23:55:53 +01:00
|
|
|
//precedence_.push_back(0);
|
2008-11-05 03:57:18 +01:00
|
|
|
} // Precedence
|
|
|
|
|
|
|
|
Precedence(const Precedence& rhs) :
|
2008-11-25 13:27:33 +01:00
|
|
|
precedence_(rhs.precedence_),
|
|
|
|
children_(rhs.children_)
|
2008-11-05 03:57:18 +01:00
|
|
|
{
|
|
|
|
} // Precedence
|
|
|
|
|
|
|
|
private:
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence(const std::vector<int> precedence) :
|
2008-11-25 13:27:33 +01:00
|
|
|
precedence_(precedence),
|
|
|
|
children_(0)
|
2008-11-05 03:57:18 +01:00
|
|
|
{
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence(int precedence) :
|
2008-11-25 13:27:33 +01:00
|
|
|
precedence_(),
|
|
|
|
children_(0)
|
2008-11-25 00:11:22 +01:00
|
|
|
{
|
|
|
|
precedence_.push_back(precedence);
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
public:
|
|
|
|
~Precedence() { }
|
|
|
|
|
|
|
|
bool operator==(const Precedence& rhs) const
|
|
|
|
{
|
|
|
|
return precedence_ == rhs.precedence_;
|
|
|
|
} // operator==
|
|
|
|
|
|
|
|
Precedence& operator=(const Precedence& rhs)
|
|
|
|
{
|
2008-11-25 00:11:22 +01:00
|
|
|
std::vector<int> other(rhs.precedence_);
|
|
|
|
precedence_.swap(other);
|
2008-11-25 13:27:33 +01:00
|
|
|
children_ = rhs.children_;
|
2008-11-05 03:57:18 +01:00
|
|
|
return *this;
|
|
|
|
} // operator=
|
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
bool is_descendant(const Precedence& other) const
|
|
|
|
{
|
|
|
|
if(other.precedence_.size() <= precedence_.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(int i = 0, ie = precedence_.size(); i != ie; ++i)
|
|
|
|
if(other.precedence_[i] != precedence_[i])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} // is_descendant
|
|
|
|
|
|
|
|
Precedence next_generation()
|
2008-11-05 03:57:18 +01:00
|
|
|
{
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence next(precedence_);
|
2008-11-25 13:27:33 +01:00
|
|
|
next.precedence_.push_back(++children_);
|
2008-11-05 03:57:18 +01:00
|
|
|
return next;
|
|
|
|
} // nextGeneration
|
|
|
|
|
|
|
|
private:
|
2008-11-25 00:11:22 +01:00
|
|
|
std::vector<int> precedence_;
|
2008-11-25 13:27:33 +01:00
|
|
|
int children_;
|
2008-11-24 23:05:16 +01:00
|
|
|
|
|
|
|
friend bool operator<(const Precedence& lhs, const Precedence& rhs);
|
2008-12-02 12:26:28 +01:00
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Precedence& prec);
|
2008-11-05 03:57:18 +01:00
|
|
|
}; // class Precedence
|
|
|
|
|
2008-11-24 23:05:16 +01:00
|
|
|
bool operator<(const Precedence& lhs, const Precedence& rhs)
|
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
if(lhs.precedence_ == rhs.precedence_)
|
|
|
|
return false;
|
2008-11-24 23:05:16 +01:00
|
|
|
|
2009-02-26 15:38:23 +01:00
|
|
|
int len = (std::min)(lhs.precedence_.size(), rhs.precedence_.size());
|
2008-12-02 12:26:28 +01:00
|
|
|
for(int c = 0; c != len; ++c)
|
2008-11-25 00:11:22 +01:00
|
|
|
{
|
2008-11-25 13:27:33 +01:00
|
|
|
if(lhs.precedence_[c] < rhs.precedence_[c])
|
|
|
|
return true;
|
|
|
|
if(lhs.precedence_[c] > rhs.precedence_[c])
|
|
|
|
return false;
|
2008-11-25 00:11:22 +01:00
|
|
|
}
|
2008-11-25 13:27:33 +01:00
|
|
|
if(lhs.precedence_.size() < rhs.precedence_.size())
|
|
|
|
return false;
|
2008-11-05 23:55:53 +01:00
|
|
|
|
2008-11-25 13:27:33 +01:00
|
|
|
return true;
|
|
|
|
} // operator>
|
|
|
|
|
|
|
|
bool operator>(const Precedence& lhs, const Precedence& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs) && !(lhs < rhs);
|
|
|
|
} // operator>
|
2008-12-02 12:26:28 +01:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Precedence& prec)
|
|
|
|
{
|
|
|
|
os << '(' << prec.precedence_[0];
|
2010-01-10 23:02:43 +01:00
|
|
|
for(size_t i = 1; i != prec.precedence_.size(); ++i)
|
2008-12-02 12:26:28 +01:00
|
|
|
os << ',' << prec.precedence_[i];
|
|
|
|
os << ')';
|
|
|
|
return os;
|
|
|
|
} // operator<<
|
2008-11-25 13:27:33 +01:00
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
#endif
|