mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:57:44 +00:00
Kernel: Add a Range::intersect(other) helper
This commit is contained in:
parent
7b22fb70a2
commit
7874b89426
2 changed files with 13 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -43,5 +44,15 @@ Vector<Range, 2> Range::carve(const Range& taken) const
|
|||
parts.append({ taken.end(), end().get() - taken.end().get() });
|
||||
return parts;
|
||||
}
|
||||
Range Range::intersect(const Range& other) const
|
||||
{
|
||||
if (*this == other) {
|
||||
return *this;
|
||||
}
|
||||
auto new_base = max(base(), other.base());
|
||||
auto new_end = min(end(), other.end());
|
||||
VERIFY(new_base < new_end);
|
||||
return Range(new_base, (new_end - new_base).get());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue