bitart: Add square unary operation
This commit is contained in:
@@ -60,6 +60,7 @@ pub enum UnaryOperator {
|
|||||||
Negation,
|
Negation,
|
||||||
Complement,
|
Complement,
|
||||||
ReverseBits,
|
ReverseBits,
|
||||||
|
Square,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnaryOperator {
|
impl UnaryOperator {
|
||||||
@@ -67,6 +68,7 @@ impl UnaryOperator {
|
|||||||
UnaryOperator::Negation,
|
UnaryOperator::Negation,
|
||||||
UnaryOperator::Complement,
|
UnaryOperator::Complement,
|
||||||
UnaryOperator::ReverseBits,
|
UnaryOperator::ReverseBits,
|
||||||
|
UnaryOperator::Square,
|
||||||
];
|
];
|
||||||
|
|
||||||
fn random<R: Rng>(rng: &mut R) -> UnaryOperator {
|
fn random<R: Rng>(rng: &mut R) -> UnaryOperator {
|
||||||
@@ -79,16 +81,7 @@ impl UnaryOperator {
|
|||||||
UnaryOperator::Negation => -operand,
|
UnaryOperator::Negation => -operand,
|
||||||
UnaryOperator::Complement => !operand,
|
UnaryOperator::Complement => !operand,
|
||||||
UnaryOperator::ReverseBits => operand.reverse_bits(),
|
UnaryOperator::ReverseBits => operand.reverse_bits(),
|
||||||
}
|
UnaryOperator::Square => operand * operand,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for UnaryOperator {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
UnaryOperator::Negation => write!(f, "-"),
|
|
||||||
UnaryOperator::Complement => write!(f, "~"),
|
|
||||||
UnaryOperator::ReverseBits => write!(f, "r"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +101,13 @@ impl UnaryOperation {
|
|||||||
}
|
}
|
||||||
impl fmt::Display for UnaryOperation {
|
impl fmt::Display for UnaryOperation {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "{}{}", self.operator, self.operand)
|
match self.operator {
|
||||||
|
UnaryOperator::Negation => write!(f, "-{}", self.operand)?,
|
||||||
|
UnaryOperator::Complement => write!(f, "~{}", self.operand)?,
|
||||||
|
UnaryOperator::ReverseBits => write!(f, "{}.rev", self.operand)?,
|
||||||
|
UnaryOperator::Square => write!(f, "{}²", self.operand)?,
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user