-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. introduce `Successor` trait 2. refactor `SortBuffer` to use `Successor` Approved-By: st1page Approved-By: BugenZhao
- Loading branch information
1 parent
5e860b0
commit 6a1be40
Showing
5 changed files
with
102 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 Singularity Data | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use chrono::Duration; | ||
|
||
use super::{NaiveDateTimeWrapper, NaiveDateWrapper, ScalarImpl}; | ||
|
||
/// A successor is a term that comes right after a particular value. Suppose n is a number (where n | ||
/// belongs to any whole number), then the successor of n is 'n+1'. The other terminologies used for | ||
/// a successor are just after, immediately after, and next value. | ||
pub trait Successor { | ||
/// Returns the successor of the current value if it exists, otherwise returns None. | ||
fn successor(&self) -> Option<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
None | ||
} | ||
} | ||
|
||
impl Successor for i16 { | ||
fn successor(&self) -> Option<Self> { | ||
self.checked_add(1) | ||
} | ||
} | ||
|
||
impl Successor for i32 { | ||
fn successor(&self) -> Option<Self> { | ||
self.checked_add(1) | ||
} | ||
} | ||
|
||
impl Successor for i64 { | ||
fn successor(&self) -> Option<Self> { | ||
self.checked_add(1) | ||
} | ||
} | ||
|
||
impl Successor for NaiveDateTimeWrapper { | ||
fn successor(&self) -> Option<Self> { | ||
self.0 | ||
.checked_add_signed(Duration::nanoseconds(1)) | ||
.map(NaiveDateTimeWrapper) | ||
} | ||
} | ||
|
||
impl Successor for NaiveDateWrapper { | ||
fn successor(&self) -> Option<Self> { | ||
self.0 | ||
.checked_add_signed(Duration::days(1)) | ||
.map(NaiveDateWrapper) | ||
} | ||
} | ||
|
||
impl ScalarImpl { | ||
/// Returns the successor of the current value if it exists. | ||
/// | ||
/// See also [`Successor`]. | ||
/// | ||
/// The function may return None when: | ||
/// 1. The current value is the maximum value of the type. | ||
/// 2. The successor value of the type is not well-defined. | ||
pub fn successor(&self) -> Option<Self> { | ||
match self { | ||
ScalarImpl::Int16(v) => v.successor().map(ScalarImpl::Int16), | ||
ScalarImpl::Int32(v) => v.successor().map(ScalarImpl::Int32), | ||
ScalarImpl::Int64(v) => v.successor().map(ScalarImpl::Int64), | ||
ScalarImpl::NaiveDateTime(v) => v.successor().map(ScalarImpl::NaiveDateTime), | ||
ScalarImpl::NaiveDate(v) => v.successor().map(ScalarImpl::NaiveDate), | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters