Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
pikelang
pike
Commits
9d748258
Commit
9d748258
authored
Jul 29, 2020
by
Henrik (Grubba) Grubbström
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Val.Range: Fixed multiple warnings.
Also adds some FIXMEs regarding the API.
parent
26f4a5de
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
26 deletions
+41
-26
lib/modules/Val.pmod/Range.pike
lib/modules/Val.pmod/Range.pike
+41
-26
No files found.
lib/modules/Val.pmod/Range.pike
View file @
9d748258
...
@@ -2,7 +2,8 @@
...
@@ -2,7 +2,8 @@
#pragma strict_types
#pragma strict_types
//! Generic lightweight range type. Supports any values for lower
//! Generic lightweight range type. Supports any values for lower
//! and upper boundaries that implement the @expr{`<@} lfun.
//! and upper boundaries that implement @[lfun::`<()] and @[lfun::`-@()],
//! and preferrably also cast to @expr{int@} and @expr{string@}.
//! @note
//! @note
//! Can only contain a single contiguous range.
//! Can only contain a single contiguous range.
//! @note
//! @note
...
@@ -11,17 +12,19 @@
...
@@ -11,17 +12,19 @@
constant is_range = 1;
constant is_range = 1;
protected typedef int|float|string|object value_type;
//! The lower inclusive boundary.
//! The lower inclusive boundary.
mixed
from;
value_type
from;
//! The upper exclusive boundary.
//! The upper exclusive boundary.
mixed
till;
value_type
till;
array(
mixed
) _encode() {
array(
value_type
) _encode() {
return ({from, till});
return ({from, till});
}
}
void _decode(array(
mixed
) x) {
void _decode(array(
value_type
) x) {
from = x[0];
from = x[0];
till = x[1];
till = x[1];
}
}
...
@@ -41,7 +44,7 @@ protected int __hash() {
...
@@ -41,7 +44,7 @@ protected int __hash() {
//! by filling in @expr{Math.inf@}.
//! by filling in @expr{Math.inf@}.
//! @seealso
//! @seealso
//! @[Math.inf]
//! @[Math.inf]
protected variant void create(
mixed from, mixed
till) {
protected variant void create(
value_type from, value_type
till) {
if (from >= till) {
if (from >= till) {
from = Math.inf;
from = Math.inf;
till = -Math.inf;
till = -Math.inf;
...
@@ -57,9 +60,9 @@ protected variant void create() {
...
@@ -57,9 +60,9 @@ protected variant void create() {
}
}
//! Difference
//! Difference
protected
mixed `-(mixed
that) {
protected
this_program `-(this_program
that) {
this_program n = this_program(max(from,
([object]
that
)
->from),
this_program n = this_program(max(from, that->from),
min(till,
([object]
that
)
->till));
min(till, that->till));
if (!n)
if (!n)
return this;
return this;
if (till == n->till) {
if (till == n->till) {
...
@@ -76,38 +79,41 @@ protected mixed `-(mixed that) {
...
@@ -76,38 +79,41 @@ protected mixed `-(mixed that) {
}
}
//! Union
//! Union
protected
mixed `+(mixed
that) {
protected
this_program `+(this_program
that) {
if (from != ([object]that)->till && ([object]that)->from != till
if (from != ([object]that)->till && ([object]that)->from != till
&& !(this & ([object]that)))
&& !(this & ([object]that)))
error("Result of range union would not be contiguous\n");
error("Result of range union would not be contiguous\n");
return this_program(min(from,
([object]
that
)
->from),
return this_program(min(from, that->from),
max(till,
([object]
that
)
->till));
max(till, that->till));
}
}
//! Intersection
//! Intersection
protected
mixed `*(mixed
that) {
protected
this_program `*(this_program
that) {
return this_program(max(from,
([object]
that
)
->from),
return this_program(max(from, that->from),
min(till,
([object]
that
)
->till));
min(till, that->till));
}
}
//! Overlap: have points in common.
//! Overlap: have points in common.
protected int(0..1) `&(
mixed
that) {
protected int(0..1) `&(
this_program
that) {
return till >
([object]
that
)
->from &&
([object]
that
)
->till > from;
return till > that->from && that->till > from;
}
}
//! Is adjacent to
//! Is adjacent to
protected int(0..1) `|(mixed that) {
//!
return till == ([object]that)->from || from == ([object]that)->till;
//! @fixme
//! This does NOT seem like a good operator for this operation.
protected int(0..1) `|(this_program that) {
return till == that->from || from == that->till;
}
}
//! Strictly left of
//! Strictly left of
protected int(0..1) `<<(
mixed
that) {
protected int(0..1) `<<(
this_program
that) {
return till <=
([object]
that
)
->from;
return till <= that->from;
}
}
//! Strictly right of
//! Strictly right of
protected int(0..1) `>>(
mixed
that) {
protected int(0..1) `>>(
this_program
that) {
return from >=
([object]
that
)
->till;
return from >= that->till;
}
}
protected int(0..1) `<(mixed that) {
protected int(0..1) `<(mixed that) {
...
@@ -122,12 +128,18 @@ protected int(0..1) `==(mixed that) {
...
@@ -122,12 +128,18 @@ protected int(0..1) `==(mixed that) {
//! @returns
//! @returns
//! True if range is empty.
//! True if range is empty.
//!
//! @seealso
//! @[isempty()]
protected inline int(0..1) `!() {
protected inline int(0..1) `!() {
return from >= till;
return from >= till;
}
}
//! @returns
//! @returns
//! True if range is empty.
//! True if range is empty.
//!
//! @seealso
//! @[`!()]
inline int(0..1) isempty() {
inline int(0..1) isempty() {
return !this;
return !this;
}
}
...
@@ -135,15 +147,18 @@ inline int(0..1) isempty() {
...
@@ -135,15 +147,18 @@ inline int(0..1) isempty() {
//! @param other
//! @param other
//! Extends the current range to the smallest range which encompasses
//! Extends the current range to the smallest range which encompasses
//! itself and all other given ranges.
//! itself and all other given ranges.
//!
//! @fixme
//! This seems like the operation that @expr{`|()@} ought to do.
this_program merge(this_program ... other) {
this_program merge(this_program ... other) {
from = min(from, @other->from);
from =
[object(value_type)]
min(from, @other->from);
till = max(till, @other->till);
till =
[object(value_type)]
max(till, @other->till);
return this;
return this;
}
}
//! @returns
//! @returns
//! True if this range fully contains another range or element.
//! True if this range fully contains another range or element.
int(0..1) contains(
mixed
other) {
int(0..1) contains(
object(this_program)|value_type
other) {
return objectp(other) && ([object]other)->is_range
return objectp(other) && ([object]other)->is_range
? from <= ([object]other)->from && ([object]other)->till <= till
? from <= ([object]other)->from && ([object]other)->till <= till
: from <= other && other < till;
: from <= other && other < till;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment