Search results

There are no results.

std.int.Int

Value
class pub builtin Int

A 64-bits signed integer type.

Int values can represent values in the range -9223372036854775808 <= value <= 9223372036854775807.

Panics

Integer operations that would result in an overflow produce a panic.

Static methods

parse

Show source code
Hide source code
fn pub static parse[T: Bytes](
  bytes: ref T,
  format: ref Format,
) -> Option[Int] {
  let input = bytes.bytes.peekable
  let base = format.to_base
  let mut byte = -1
  let pos = match input.next {
    case Some(PLUS) -> {
      byte = try input.next
      true
    }
    case Some(MINUS) -> {
      byte = try input.next
      false
    }
    case Some(val) -> {
      byte = val
      true
    }
    case None -> return Option.None
  }

  let mut num = 0

  # For base 16 numbers, we allow them to start with -0x and 0x, as most base
  # 16 numbers will likely use the format 0xABC and not just ABC.
  if base == 16 and byte == ZERO {
    match input.peek {
      case Some(LOWER_X) -> {
        input.next
        byte = try input.next
      }
      case _ -> {}
    }
  }

  # Rust's approach is to check if an overflow is possible, and use wrapped
  # arithmetic in that case, in an attempt to improve performance. This was
  # implemented in https://github.com/rust-lang/rust/pull/95399, with
  # https://github.com/rust-lang/rust/pull/83371 and
  # https://github.com/rust-lang/rust/pull/83088 being related pull requests.
  #
  # I opted not to take this approach at the time of writing, as the
  # performance improvements seem very small, and simply not worth the extra
  # code at the time of writing. If this ever changes, we can consider taking
  # a similar approach.
  loop {
    let digit = if base <= 10 {
      byte.wrapping_sub(ZERO)
    } else if byte >= ZERO and byte <= NINE {
      byte - ZERO
    } else if byte >= LOWER_A and byte <= LOWER_F {
      byte - LOWER_A + 10
    } else if byte >= UPPER_A and byte <= UPPER_F {
      byte - UPPER_A + 10
    } else {
      return Option.None
    }

    if digit > base or digit < 0 { return Option.None }

    num = try num.checked_mul(base)
    num = try if pos {
      num.checked_add(digit)
    } else {
      num.checked_sub(digit)
    }

    match input.next {
      case Some(val) -> byte = val
      case _ -> return Option.Some(num)
    }
  }
}
fn pub static parse[T: Bytes](bytes: ref T, format: ref Format) -> Option[Int]

Parses a Bytes into an Int in the format specified in format, returning a Some if the value is valid, and a None otherwise.

For numbers in binary, the valid digits are 0 and 1. For decimal numbers, the valid digits are in the range 0-9. For hexadecimal numbers, the valid digits are in the ranges a-z, A-Z, 0-9, and the input may start with 0x.

In addition, the input may start with + or -, regardless of the input format.

Leading and/or trailing whitespace is considered invalid.

Examples

Parsing a binary number:

import std.int (Format)

Int.parse('11', Format.Binary)  # => Option.Some(3)
Int.parse('-11', Format.Binary) # => Option.Some(-3)
Int.parse('ff', Format.Binary)  # => Option.None

Parsing a decimal number:

import std.int (Format)

Int.parse('123', Format.Decimal)  # => Option.Some(123)
Int.parse('-123', Format.Decimal) # => Option.Some(-123)
Int.parse('abc', Format.Decimal)  # => Option.None

Parsing a hexadecimal number:

import std.int (Format)

Int.parse('ef', Format.Hex)    # => Option.Some(239)
Int.parse('0xef', Format.Hex)  # => Option.Some(239)
Int.parse('-0xef', Format.Hex) # => Option.Some(-239)
Int.parse('zz', Format.Hex)    # => Option.None

sum

Show source code
Hide source code
fn pub static sum[I: Iter[Int]](iterator: move I) -> Int {
  iterator.reduce(0, fn (acc, val) { acc + val })
}
fn pub static sum[I: Iter[Int]](iterator: move I) -> Int

Sums the values of iterator into a single Int.

Examples

Int.sum([10, 20, 30].into_iter) # => 60

Instance methods

!=

Show source code
Hide source code
fn pub !=(other: T) -> Bool {
  (self == other).false?
}
fn pub !=(other: T) -> Bool

Returns true if self and the given object are not equal to each other.

%

Show source code
Hide source code
fn pub %(other: ref Int) -> Int {
  if other == 0 or (self == MIN and other == -1) {
    overflow(self, '%', other)
  }

  let sum = _INKO.int_checked_add(_INKO.int_rem(self, other), other)

  if sum.tag as Int == 0 {
    _INKO.int_rem(sum.value as Int, other)
  } else {
    overflow(self, '%', other)
  }
}
fn pub %(other: ref Int) -> Int

Gets the remainder after dividing self by the given object.

&

Show source code
Hide source code
fn pub &(other: ref Int) -> Int {
  _INKO.int_bit_and(self, other)
}
fn pub &(other: ref Int) -> Int

Returns the result of a bitwise AND with self and the given object.

*

Show source code
Hide source code
fn pub *(other: ref Int) -> Int {
  let res = _INKO.int_checked_mul(self, other)

  if res.tag as Int == 0 {
    res.value as Int
  } else {
    overflow(self, '*', other)
  }
}
fn pub *(other: ref Int) -> Int

Multiplies self with the given object.

**

Show source code
Hide source code
fn pub **(other: ref Int) -> Int {
  let res = inko_int_checked_pow(self, other)

  if res.tag as Int == 0 {
    res.value as Int
  } else {
    overflow(self, '**', other)
  }
}
fn pub **(other: ref Int) -> Int

Raises self to the power of the given exponent.

+

Show source code
Hide source code
fn pub +(other: ref Int) -> Int {
  let res = _INKO.int_checked_add(self, other)

  if res.tag as Int == 0 {
    res.value as Int
  } else {
    overflow(self, '+', other)
  }
}
fn pub +(other: ref Int) -> Int

Adds the given object to self.

-

Show source code
Hide source code
fn pub -(other: ref Int) -> Int {
  let res = _INKO.int_checked_sub(self, other)

  if res.tag as Int == 0 {
    res.value as Int
  } else {
    overflow(self, '-', other)
  }
}
fn pub -(other: ref Int) -> Int

Subtracts the given object from self.

/

Show source code
Hide source code
fn pub /(other: ref Int) -> Int {
  if other == 0 or (self == MIN and other == -1) {
    overflow(self, '-', other)
  }

  unchecked_div(other)
}
fn pub /(other: ref Int) -> Int

Divides self by the given object.

<

Show source code
Hide source code
fn pub <(other: ref Int) -> Bool {
  _INKO.int_lt(self, other)
}
fn pub <(other: ref Int) -> Bool

Returns true if self is lower than the given argument.

<<

Show source code
Hide source code
fn pub <<(other: ref Int) -> Int {
  if other >= BITS { overflow(self, '<<', other) }

  _INKO.int_shl(self, other)
}
fn pub <<(other: ref Int) -> Int

Returns the result of a bitwise shift to the left with self and the given object.

<=

Show source code
Hide source code
fn pub <=(other: ref Int) -> Bool {
  _INKO.int_le(self, other)
}
fn pub <=(other: ref Int) -> Bool

Returns true if self is lower than or equal to the given argument.

==

Show source code
Hide source code
fn pub ==(other: ref Int) -> Bool {
  _INKO.int_eq(self, other)
}
fn pub ==(other: ref Int) -> Bool

Returns true if self and the given object are equal to each other.

This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.

>

Show source code
Hide source code
fn pub >(other: ref Int) -> Bool {
  _INKO.int_gt(self, other)
}
fn pub >(other: ref Int) -> Bool

Returns true if self is greater than the given argument.

>=

Show source code
Hide source code
fn pub >=(other: ref Int) -> Bool {
  _INKO.int_ge(self, other)
}
fn pub >=(other: ref Int) -> Bool

Returns true if self is equal to or greater than the given argument.

>>

Show source code
Hide source code
fn pub >>(other: ref Int) -> Int {
  if other >= BITS { overflow(self, '>>', other) }

  _INKO.int_shr(self, other)
}
fn pub >>(other: ref Int) -> Int

Returns the result of a bitwise shift to the right with self and the given object.

>>>

Show source code
Hide source code
fn pub >>>(other: ref Int) -> Int {
  if other >= BITS { overflow(self, '>>>', other) }

  _INKO.int_unsigned_shr(self, other)
}
fn pub >>>(other: ref Int) -> Int

Casts self to an unsigned integer, shifts it to the right, then returns the result as a signed integer.

^

Show source code
Hide source code
fn pub ^(other: ref Int) -> Int {
  _INKO.int_bit_xor(self, other)
}
fn pub ^(other: ref Int) -> Int

Returns the result of a bitwise XOR with self and the given object.

absolute

Show source code
Hide source code
fn pub absolute -> Int {
  if self < 0 { 0 - self } else { clone }
}
fn pub absolute -> Int

Returns the absolute value of self.

Examples

-4.absolute # => 4
4.absolute  # => 4

checked_add

Show source code
Hide source code
fn pub checked_add(other: Int) -> Option[Int] {
  let res = _INKO.int_checked_add(self, other)

  if res.tag as Int == 0 {
    Option.Some(res.value as Int)
  } else {
    Option.None
  }
}
fn pub checked_add(other: Int) -> Option[Int]

Adds other to self, returning a None when overflowing.

Examples

import std.int (MAX)

1.checked_add(5)   # => Option.Some(6)
MAX.checked_add(1) # => Option.None

checked_div

Show source code
Hide source code
fn pub checked_div(other: Int) -> Option[Int] {
  if other == 0 or (self == MIN and other == -1) {
    Option.None
  } else {
    Option.Some(unchecked_div(other))
  }
}
fn pub checked_div(other: Int) -> Option[Int]

Divides self by other, returning a None when overflowing or if other is zero.

Examples

import std.int (MAX)

10.checked_div(0) # => Option.None
10.checked_div(2) # => Option.Some(5)

checked_mul

Show source code
Hide source code
fn pub checked_mul(other: Int) -> Option[Int] {
  let res = _INKO.int_checked_mul(self, other)

  if res.tag as Int == 0 {
    Option.Some(res.value as Int)
  } else {
    Option.None
  }
}
fn pub checked_mul(other: Int) -> Option[Int]

Multiplies other with self, returning a None when overflowing.

Examples

import std.int (MAX)

1.checked_mul(2)   # => Option.Some(2)
MAX.checked_mul(2) # => Option.None

checked_pow

Show source code
Hide source code
fn pub checked_pow(other: Int) -> Option[Int] {
  let res = inko_int_checked_pow(self, other)

  if res.tag as Int == 0 {
    Option.Some(res.value as Int)
  } else {
    Option.None
  }
}
fn pub checked_pow(other: Int) -> Option[Int]

Raises self to the power of other, returning a None when overflowing.

Examples

import std.int (MAX)

2.checked_pow(2)   # => Option.Some(4)
MAX.checked_pow(2) # => Option.None

checked_sub

Show source code
Hide source code
fn pub checked_sub(other: Int) -> Option[Int] {
  let res = _INKO.int_checked_sub(self, other)

  if res.tag as Int == 0 {
    Option.Some(res.value as Int)
  } else {
    Option.None
  }
}
fn pub checked_sub(other: Int) -> Option[Int]

Subtracts other from self, returning a None when overflowing.

Examples

import std.int (MAX, MIN)

1.checked_sub(1)   # => Option.Some(0)
MIN.checked_sub(1) # => Option.None

clone

Show source code
Hide source code
fn pub clone -> Int {
  self
}
fn pub clone -> Int

Creates a clone of self.

cmp

Show source code
Hide source code
fn pub cmp(other: ref Int) -> Ordering {
  if self > other {
    Ordering.Greater
  } else if self < other {
    Ordering.Less
  } else {
    Ordering.Equal
  }
}
fn pub cmp(other: ref Int) -> Ordering

Returns the ordering between self and the given argument.

The returned value should be as follows:

  • a == b: Ordering.Equal
  • a > b: Ordering.Greater
  • a < b: Ordering.Less

digits

Show source code
Hide source code
fn pub digits -> Int {
  if self == 0 { return 1 }

  let mut digits = 0
  let mut number = absolute

  while number > 0 {
    number /= 10
    digits += 1
  }

  digits
}
fn pub digits -> Int

Returns the number of digits of self.

Examples

0.digits    # => 1
10.digits   # => 2
100.digits  # => 3
-100.digits # => 3

fmt

Show source code
Hide source code
fn pub fmt(formatter: mut Formatter) {
  formatter.write(to_string)
}
fn pub fmt(formatter: mut Formatter)

Formats self in a human-readable format for debugging purposes.

format

Show source code
Hide source code
fn pub format(format: ref Format) -> String {
  let base = format.to_base

  match self {
    case 0 -> return '0'
    # MIN can't be turned positive using absolute(), so we have to handle
    # these cases explicitly.
    case MIN if base == 2 -> {
      return '-1000000000000000000000000000000000000000000000000000000000000000'
    }
    case MIN if base == 10 -> return '-9223372036854775808'
    case MIN if base == 16 -> return '-8000000000000000'
    case _ -> {}
  }

  let alphabet = '0123456789abcdef'
  let bytes = ByteArray.new
  let mut int = absolute

  while int > 0 {
    bytes.push(alphabet.byte(int % base))
    int /= base
  }

  if self < 0 { bytes.push(0x2D) }

  bytes.reverse
  bytes.into_string
}
fn pub format(format: ref Format) -> String

Formats self as a String in the given format.

Examples

Formatting an Int as binary number:

import std.int (Format)

3.format(Format.Binary)  # => '11'
-1.format(Format.Binary) # => '-11'

Formatting an Int as a a decimal number:

123.format(Format.Decimal) # => '123'
123.format(Format.Hex)     # => '7b'

Formatting an Int as a hexadecimal number:

-123.format(Format.Hex) # => '-7b'

hash

Show source code
Hide source code
fn pub hash[H: mut + Hasher](hasher: mut H) {
  hasher.write(clone)
}
fn pub hash[H: mut + Hasher](hasher: mut H: mut)

Writes the hash for self into the given Hasher.

nearest_power_of_two

Show source code
Hide source code
fn pub nearest_power_of_two -> Int {
  if self <= 0 { return 0 }

  let mut rounded = clone

  rounded -= 1
  rounded |= rounded >> 1
  rounded |= rounded >> 2
  rounded |= rounded >> 4
  rounded |= rounded >> 8
  rounded |= rounded >> 16
  rounded |= rounded >> 32
  rounded += 1

  rounded
}
fn pub nearest_power_of_two -> Int

Rounds self to the nearest power of two.

If self <= 0, this method returns zero.

Examples

0.nearest_power_of_two # => 0
1.nearest_power_of_two # => 1
2.nearest_power_of_two # => 2
3.nearest_power_of_two # => 4

not

Show source code
Hide source code
fn pub not -> Int {
  _INKO.int_bit_not(self)
}
fn pub not -> Int

Returns the result of performing a bitwise NOT on self.

As Inko doesn't support unary operators besides not (which only supports booleans), this is just a regular method.

Examples

12.not # => -13

opposite

Show source code
Hide source code
fn pub opposite -> Int {
  0 - self
}
fn pub opposite -> Int

Returns a value with the opposite sign of self.

Examples

-42.opposite # => 42
42.opposite  # => -42

rotate_left

Show source code
Hide source code
fn pub rotate_left(amount: Int) -> Int {
  _INKO.int_rotate_left(self, amount)
}
fn pub rotate_left(amount: Int) -> Int

Shifts the bits to the left, wrapping the truncated bits to the end of the resulting integer.

Examples

0xaa00000000006e1.rotate_left(12) # => 0x6e10aa

rotate_right

Show source code
Hide source code
fn pub rotate_right(amount: Int) -> Int {
  _INKO.int_rotate_right(self, amount)
}
fn pub rotate_right(amount: Int) -> Int

Shifts the bits to the right, wrapping the truncated bits to the end of the resulting integer.

Examples

0x6e10aa.rotate_right(12) # => 0xaa00000000006e1

times

Show source code
Hide source code
fn pub times(block: fn (Int)) {
  let mut index = 0

  while index < self {
    block.call(index)

    index += 1
  }
}
fn pub times(block: fn (Int))

Calls the supplied closure self times.

Examples

4.times fn (i) {
  i # => 0, 1, 2, 3
}

to

Show source code
Hide source code
fn pub to(other: Int) -> InclusiveRange {
  InclusiveRange.new(clone, other)
}
fn pub to(other: Int) -> InclusiveRange

Returns a Range from self up to and including other.

to_float

Show source code
Hide source code
fn pub to_float -> Float {
  self as Float
}
fn pub to_float -> Float

Converts self to a Float

to_int

Show source code
Hide source code
fn pub to_int -> Int {
  clone
}
fn pub to_int -> Int

Converts self to a Int

to_string

Show source code
Hide source code
fn pub to_string -> String {
  format(Format.Decimal)
}
fn pub to_string -> String

Converts self to a String.

until

Show source code
Hide source code
fn pub until(other: Int) -> ExclusiveRange {
  ExclusiveRange.new(clone, other)
}
fn pub until(other: Int) -> ExclusiveRange

Returns a Range from self up to but excluding other.

wrapping_add

Show source code
Hide source code
fn pub wrapping_add(other: Int) -> Int {
  _INKO.int_wrapping_add(self, other)
}
fn pub wrapping_add(other: Int) -> Int

Adds other to self, wrapping around when overflowing.

Examples

import std.int (MAX, MIN)

1.wrapping_add(1)   # => 2
MAX.wrapping_add(1) # => MIN

wrapping_mul

Show source code
Hide source code
fn pub wrapping_mul(other: Int) -> Int {
  _INKO.int_wrapping_mul(self, other)
}
fn pub wrapping_mul(other: Int) -> Int

Multiplies other with self, wrapping around when overflowing.

Examples

import std.int (MAX)

1.wrapping_mul(2)   # => 2
MAX.wrapping_mul(2) # => -2

wrapping_sub

Show source code
Hide source code
fn pub wrapping_sub(other: Int) -> Int {
  _INKO.int_wrapping_sub(self, other)
}
fn pub wrapping_sub(other: Int) -> Int

Subtracts other from self, wrapping around when overflowing.

Examples

import std.int (MAX, MIN)

1.wrapping_sub(1)   # => 0
MIN.wrapping_sub(1) # => MAX

|

Show source code
Hide source code
fn pub |(other: ref Int) -> Int {
  _INKO.int_bit_or(self, other)
}
fn pub |(other: ref Int) -> Int

Returns the result of a bitwise OR with self and the given object.

Implemented traits

std.clone.

Clone

impl Clone[Int] for Int
std.cmp.

Compare

impl Compare[Int] for Int
std.cmp.

Equal

impl Equal[ref Int] for Int
std.float.

ToFloat

impl ToFloat for Int
std.fmt.

Format

impl Format for Int
std.hash.

Hash

impl Hash for Int
std.int.

ToInt

impl ToInt for Int
std.ops.

Add

impl Add[Int, Int] for Int
std.ops.

BitAnd

impl BitAnd[Int, Int] for Int
std.ops.

BitOr

impl BitOr[Int, Int] for Int
std.ops.

BitXor

impl BitXor[Int, Int] for Int
std.ops.

Divide

impl Divide[Int, Int] for Int
std.ops.

Modulo

impl Modulo[Int, Int] for Int
std.ops.

Multiply

impl Multiply[Int, Int] for Int
std.ops.

Power

impl Power[Int, Int] for Int
std.ops.

ShiftLeft

impl ShiftLeft[Int, Int] for Int
std.ops.

ShiftRight

impl ShiftRight[Int, Int] for Int
std.ops.

Subtract

impl Subtract[Int, Int] for Int
std.ops.

UnsignedShiftRight

impl UnsignedShiftRight[Int, Int] for Int
std.string.

ToString

impl ToString for Int