f9687a2017-12-11Stephen R. van den Berg #pike __REAL_VERSION__ #pragma strict_types
ef04e02017-12-13Stephen R. van den Berg //! Generic lightweight range type. Supports any values for lower //! and upper boundaries that implement the @expr{`<@} lfun.
f9687a2017-12-11Stephen R. van den Berg //! @note //! Can only contain a single contiguous range.
ef04e02017-12-13Stephen R. van den Berg //! The lower inclusive boundary.
f9687a2017-12-11Stephen R. van den Berg mixed from;
ef04e02017-12-13Stephen R. van den Berg //! The upper exclusive boundary.
f9687a2017-12-11Stephen R. van den Berg mixed till;
ebc4212017-12-11Stephen R. van den Berg //! @param from
ef04e02017-12-13Stephen R. van den Berg //! Lower inclusive boundary for the range. Specify no lower-boundary
ebc4212017-12-11Stephen R. van den Berg //! by filling in @expr{-Math.inf@}. //! @param till
ef04e02017-12-13Stephen R. van den Berg //! Upper exclusive boundary for the range. Specify no upper-boundary
ebc4212017-12-11Stephen R. van den Berg //! by filling in @expr{Math.inf@}. //! @seealso //! [Math.inf]
f9687a2017-12-11Stephen R. van den Berg protected variant void create(mixed from, mixed till) { this::from = from; this::till = till; }
4c24162017-12-11Martin Nilsson protected variant void create(object/*this_program*/ copy) {
f9687a2017-12-11Stephen R. van den Berg  from = copy->from; till = copy->till; } protected variant void create() { } //! Difference protected mixed `-(mixed that) { this_program n = this_program(max(from, ([object]that)->from), min(till, ([object]that)->till)); if (n->from >= n->till) return this; if (till == n->till) { n->till = n->from; n->from = from; return n; } if (from == n->from) { n->from = n->till; n->till = till; return n; } error("Result of range difference would not be contiguous\n"); } //! Union protected mixed `+(mixed that) { if (from != ([object]that)->till && ([object]that)->from != till && !(this & ([object]that))) error("Result of range union would not be contiguous\n"); return this_program(min(from, ([object]that)->from), max(till, ([object]that)->till)); } //! Intersection protected mixed `*(mixed that) { return this_program(max(from, ([object]that)->from), min(till, ([object]that)->till)); } //! Overlap: have points in common. protected int(0..1) `&(mixed that) { return till > ([object]that)->from && ([object]that)->till > from; } protected int(0..1) `<(mixed that) { return from < ([object]that)->from || from == ([object]that)->from && till < ([object]that)->till; } protected int(0..1) `==(mixed that) { return objectp(that) && from == ([object]that)->from && till == ([object]that)->till; }
0e58d02017-12-14Stephen R. van den Berg //! @returns //! Calculates the value of the interval: @expr{till - from@}. mixed interval() { return till - from; }
ef04e02017-12-13Stephen R. van den Berg //! Casting a range to string delivers an SQL-compliant value.
f9687a2017-12-11Stephen R. van den Berg protected mixed cast(string to) { if (to == "string") return from >= till ? "empty" : sprintf("%c%s,%s)",
ebc4212017-12-11Stephen R. van den Berg  from == -Math.inf ? '(' : '[', from == -Math.inf ? "" : (string)from, till == Math.inf ? "" : (string)till);
f9687a2017-12-11Stephen R. van den Berg  return UNDEFINED; } protected string _sprintf(int fmt, mapping(string:mixed) params) { switch (fmt) { case 'O': return sprintf("this_program( %s )", (string)this); case 's': return (string)this; } return sprintf(sprintf("%%*%c", fmt), params, 0); }