From e555733468534bd6c2fd644bf8fa2388a2f2b343 Mon Sep 17 00:00:00 2001 From: Adam Wardynski <award@.(win7-laptop)> Date: Fri, 19 Nov 2010 11:51:43 +0100 Subject: [PATCH] Some checks for Position operating on sentence context. Checks wether Position is_inside, is_outside, or equals to other Position given the SentenceContext. --- libwccl/values/position.cpp | 8 ++++++++ libwccl/values/position.h | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/libwccl/values/position.cpp b/libwccl/values/position.cpp index 074f7b3..e71912e 100644 --- a/libwccl/values/position.cpp +++ b/libwccl/values/position.cpp @@ -1,5 +1,7 @@ #include <libwccl/values/position.h> +#include <libwccl/sentencecontext.h> #include <boost/lexical_cast.hpp> + namespace Wccl { const char* Position::type_name = "Position"; @@ -25,4 +27,10 @@ std::string Position::var_repr(const std::string &var_name) return ss.str(); } +bool Position::is_inside(const SentenceContext& context) const +{ + int abs_position = context.get_abs_position(*this); + return context.is_inside(abs_position); +} + } /* end ns Wccl */ diff --git a/libwccl/values/position.h b/libwccl/values/position.h index a87a8a9..3283b84 100644 --- a/libwccl/values/position.h +++ b/libwccl/values/position.h @@ -7,6 +7,8 @@ namespace Wccl { +class SentenceContext; + class Position : public Value { public: @@ -32,10 +34,36 @@ public: /// Value override std::string to_raw_string() const; - bool equals(const Position& other) { + /** + * @returns True if Position is within bounds of a sentence, false otherwise. + */ + bool is_inside(const SentenceContext& context) const; + + /** + * @returns True if Position is outside of bounds of a sentence, false otherwise. + */ + bool is_outside(const SentenceContext& context) const { + return !is_inside(context); + } + /** + * @returns True if underlying position values are equal, false otherwise. + * @note This version does not take into account sentence context, only + * the raw value of position. + */ + bool equals(const Position& other) const { return val_ == other.val_; } + /** + * @returns True if positions are equal in context of a sentence, false otherwise. + * @note This version determines if position values point outside of a sentence, + * and if both of them do, they are considered to be equal as well (both "nowhere"). + */ + bool equals(const Position& other, const SentenceContext& context) const + { + return equals(other) || is_outside(context) && other.is_outside(context); + } + private: int val_; }; -- GitLab