Just enough cleaning to get rid of all those warnings.
Pull-Request: #921
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>
# assert a == b
class Array[E]
super AbstractArray[E]
- super ArrayCapable[E]
redef fun [](index)
do
var c = _capacity
if cap <= c then return
while c <= cap do c = c * 2 + 2
- var a = calloc_array(c)
+ var a = new NativeArray[E](c)
if _capacity > 0 then _items.copy_to(a, _length)
_items = a
_capacity = c
init with_capacity(cap: Int)
do
assert positive: cap >= 0
- _items = calloc_array(cap)
+ _items = new NativeArray[E](cap)
_capacity = cap
_length = 0
end
init filled_with(value: E, count: Int)
do
assert positive: count >= 0
- _items = calloc_array(count)
+ _items = new NativeArray[E](count)
_capacity = count
_length = count
var i = 0
redef var index = 0
- private var array: AbstractArrayRead[E]
+ var array: AbstractArrayRead[E]
end
private class ArrayReverseIterator[E]
redef fun item: E do return _iter.item
- private var iter: ArrayIterator[E]
+ var iter: ArrayIterator[E]
end
# Native classes ##############################################################
-# Subclasses of this class can create native arrays
-interface ArrayCapable[E]
- # Get a new array of `size` elements.
- protected fun calloc_array(size: Int): NativeArray[E] is intern
-end
-
# Native Nit array
# Access are unchecked and it has a fixed size
# Not for public use: may become private.
fun length: Int is intern
# Use `self` to initialize a standard Nit Array.
fun to_a: Array[E] do return new Array[E].with_native(self, length)
+
+ # Get item at `index`.
fun [](index: Int): E is intern
+
+ # Set `item` at `index`.
fun []=(index: Int, item: E) is intern
+
+ # Copy `length` items to `dest`.
fun copy_to(dest: NativeArray[E], length: Int) is intern
#fun =(o: NativeArray[E]): Bool is intern
#fun !=(o: NativeArray[E]): Bool is intern
import array
# A HashCollection is an array of HashNode[K] indexed by the K hash value
-private abstract class HashCollection[K: Object, N: HashNode[Object]]
- super ArrayCapable[nullable N]
+private abstract class HashCollection[K: Object]
+ type N: HashNode[K]
- private var array: nullable NativeArray[nullable N] = null # Used to store items
- private var capacity: Int = 0 # Size of _array
- private var the_length: Int = 0 # Number of items in the map
+ var array: nullable NativeArray[nullable N] = null # Used to store items
+ var capacity: Int = 0 # Size of _array
+ var the_length: Int = 0 # Number of items in the map
- private var first_item: nullable N = null # First added item (used to visit items in nice order)
- private var last_item: nullable N = null # Last added item (same)
+ var first_item: nullable N = null # First added item (used to visit items in nice order)
+ var last_item: nullable N = null # Last added item (same)
# The last key accessed (used for cache)
- private var last_accessed_key: nullable K = null
+ var last_accessed_key: nullable K = null
# The last node accessed (used for cache)
- private var last_accessed_node: nullable N = null
+ var last_accessed_node: nullable N = null
# Return the index of the key k
fun index_at(k: K): Int
_last_accessed_key = null
# get a new array
- var new_array = calloc_array(cap)
+ var new_array = new NativeArray[nullable N](cap)
_array = new_array
# clean the new array
end
private abstract class HashNode[K: Object]
- private var key: K
+ var key: K
type N: HashNode[K]
- private var next_item: nullable N = null
- private var prev_item: nullable N = null
- private var prev_in_bucklet: nullable N = null
- private var next_in_bucklet: nullable N = null
+ var next_item: nullable N = null
+ var prev_item: nullable N = null
+ var prev_in_bucklet: nullable N = null
+ var next_in_bucklet: nullable N = null
end
# A map implemented with a hash table.
# Keys of such a map cannot be null and require a working `hash` method
class HashMap[K: Object, V]
super Map[K, V]
- super HashCollection[K, HashMapNode[K, V]]
+ super HashCollection[K]
+
+ redef type N: HashMapNode[K, V] is fixed
redef fun [](key)
do
private class HashMapNode[K: Object, V]
super HashNode[K]
redef type N: HashMapNode[K, V]
- private var value: V
+ var value: V
end
+ # A `MapIterator` over a `HashMap`.
class HashMapIterator[K: Object, V]
super MapIterator[K, V]
redef fun is_ok do return _node != null
# Keys of such a map cannot be null and require a working `hash` method
class HashSet[E: Object]
super Set[E]
- super HashCollection[E, HashSetNode[E]]
+ super HashCollection[E]
+
+ redef type N: HashSetNode[E] is fixed
redef fun length do return _the_length
end
# The set to iterate on
- private var set: HashSet[E]
+ var set: HashSet[E]
# The position in the internal map storage
- private var node: nullable HashSetNode[E] = null
+ var node: nullable HashSetNode[E] = null
init
do
#
# Currently, Object is also used to collect all top-level methods.
interface Object
+ # Type of this instance, automatically specialized in every class
+ #
+ # A common use case of the virtual type `SELF` is to type an attribute and
+ # store another instance of the same type as `self`. It can also be used as as
+ # return type to a method producing a copy of `self` or returning an instance
+ # expected to be the exact same type as self.
+ #
+ # This virtual type must be used with caution as it can hinder specialization.
+ # In fact, it imposes strict restrictions on all sub-classes and their usage.
+ # For example, using `SELF` as a return type of a method `foo`
+ # forces all subclasses to ensure that `foo` returns the correct and updated
+ # type.
+ # A dangerous usage take the form of a method typed by `SELF` which creates
+ # and returns a new instance.
+ # If not correctly specialized, this method would break when invoked on a
+ # sub-class.
+ #
+ # A general rule for safe usage of `SELF` is to ensure that inputs typed
+ # `SELF` are stored in attributes typed `SELF` and returned by methods typed
+ # `SELF`, pretty much the same things as you would do with parameter types.
+ type SELF: Object
+
# The unique object identifier in the class.
# Unless specific code, you should not use this method.
# The identifier is used internally to provide a hash value.
end
end
+ # Compare float numbers with a given precision.
+ #
+ # Because of the loss of precision in floating numbers,
+ # the `==` method is often not the best way to compare them.
+ #
+ # ~~~
+ # assert 0.01.is_approx(0.02, 0.1) == true
+ # assert 0.01.is_approx(0.02, 0.001) == false
+ # ~~~
+ fun is_approx(other, precision: Float): Bool
+ do
+ assert precision >= 0.0
+ return self <= other + precision and self >= other - precision
+ end
+
redef fun max(other)
do
if self < other then
redef fun -(i) is intern
redef fun *(i) is intern
redef fun /(i) is intern
+
+ # Modulo of `self` with `i`.
+ #
+ # Finds the remainder of division of `self` by `i`.
+ #
+ # assert 5 % 2 == 1
+ # assert 10 % 2 == 0
fun %(i: Int): Int is intern
redef fun zero do return 0
end
redef class Float
+
+ # Returns the non-negative square root of `self`.
+ #
+ # assert 9.0.sqrt == 3.0
+ # #assert 3.0.sqrt == 1.732
+ # assert 1.0.sqrt == 1.0
+ # assert 0.0.sqrt == 0.0
fun sqrt: Float is extern "kernel_Float_Float_sqrt_0"
+
+ # Computes the cosine of `self` (expressed in radians).
+ #
+ # #assert pi.cos == -1.0
fun cos: Float is extern "kernel_Float_Float_cos_0"
+
+ # Computes the sine of `self` (expressed in radians).
+ #
+ # #assert pi.sin == 0.0
fun sin: Float is extern "kernel_Float_Float_sin_0"
+
+ # Computes the cosine of x (expressed in radians).
+ #
+ # #assert 0.0.tan == 0.0
fun tan: Float is extern "kernel_Float_Float_tan_0"
+
+ # Computes the arc cosine of `self`.
+ #
+ # #assert 0.0.acos == pi / 2.0
fun acos: Float is extern "kernel_Float_Float_acos_0"
+
+ # Computes the arc sine of `self`.
+ #
+ # #assert 1.0.asin == pi / 2.0
fun asin: Float is extern "kernel_Float_Float_asin_0"
+
+ # Computes the arc tangent of `self`.
+ #
+ # #assert 0.0.tan == 0.0
fun atan: Float is extern "kernel_Float_Float_atan_0"
+
+ # Returns the absolute value of `self`.
+ #
+ # assert 12.0.abs == 12.0
+ # assert (-34.56).abs == 34.56
+ # assert -34.56.abs == -34.56
fun abs: Float `{ return fabs(recv); `}
+ # Returns `self` raised at `e` power.
+ #
+ # #assert 2.0.pow(0.0) == 1.0
+ # #assert 2.0.pow(3.0) == 8.0
+ # #assert 0.0.pow(9.0) == 0.0
fun pow(e: Float): Float is extern "kernel_Float_Float_pow_1"
+
+ # Returns the logarithm of `self`.
+ #
+ # assert 0.0.log.is_inf == -1
+ # #assert 1.0.log == 0.0
fun log: Float is extern "kernel_Float_Float_log_0"
+
+ # Returns **e** raised to `self`.
fun exp: Float is extern "kernel_Float_Float_exp_0"
# assert 1.1.ceil == 2.0
# assert 2.0.floor == 2.0
# assert (-1.5).floor == -2.0
fun floor: Float `{ return floor(recv); `}
+
+ # Rounds the value of a float to its nearest integer value
+ #
+ # assert 1.67.round == 2.0
+ # assert 1.34.round == 1.0
+ # assert -1.34.round == -1.0
+ # assert -1.67.round == -2.0
+ fun round: Float is extern "round"
# Returns a random `Float` in `[0.0 .. self[`.
fun rand: Float is extern "kernel_Float_Float_rand_0"
- fun hypot_with( b : Float ) : Float is extern "hypotf"
+ # Returns the euclidean distance from `b`.
+ fun hypot_with(b : Float): Float is extern "hypotf"
+
+ # Returns true is self is not a number.
fun is_nan: Bool is extern "isnan"
# Is the float an infinite value
end
end
+ # Computes the arc tangent given `x` and `y`.
+ #
+ # assert atan2(-0.0, 1.0) == -0.0
+ # assert atan2(0.0, 1.0) == 0.0
fun atan2(x: Float, y: Float): Float is extern "kernel_Any_Any_atan2_2"
+
+ # Approximate value of **pi**.
fun pi: Float is extern "kernel_Any_Any_pi_0"
# Initialize the pseudo-random generator with the given seed.
# High-level abstraction for all text representations
abstract class Text
super Comparable
- super StringCapable
redef type OTHER: Text
redef var length: Int = 0
- init do end
-
redef fun output
do
var i = 0
type SELFTYPE: Text
- private var target: SELFTYPE
+ var target: SELFTYPE
redef fun is_empty do return target.is_empty
end
+ # A `String` holds and manipulates an arbitrary sequence of characters.
+ #
+ # String objects may be created using literals.
+ #
+ # assert "Hello World!" isa String
abstract class String
super Text
# assert "abc" * 0 == ""
fun *(i: Int): SELFTYPE is abstract
+ # Insert `s` at `pos`.
+ #
+ # assert "helloworld".insert_at(" ", 5) == "hello world"
fun insert_at(s: String, pos: Int): SELFTYPE is abstract
redef fun substrings: Iterator[String] is abstract
redef fun reversed
do
- var native = calloc_string(self.length + 1)
+ var native = new NativeString(self.length + 1)
var length = self.length
var items = self.items
var pos = 0
redef fun to_upper
do
- var outstr = calloc_string(self.length + 1)
+ var outstr = new NativeString(self.length + 1)
var out_index = 0
var myitems = self.items
redef fun to_lower
do
- var outstr = calloc_string(self.length + 1)
+ var outstr = new NativeString(self.length + 1)
var out_index = 0
var myitems = self.items
if real_items != null then
return real_items.as(not null)
else
- var newItems = calloc_string(length + 1)
+ var newItems = new NativeString(length + 1)
self.items.copy_to(newItems, length, index_from, 0)
newItems[length] = '\0'
self.real_items = newItems
var total_length = my_length + its_length
- var target_string = calloc_string(my_length + its_length + 1)
+ var target_string = new NativeString(my_length + its_length + 1)
self.items.copy_to(target_string, my_length, index_from, 0)
if s isa FlatString then
var my_items = self.items
- var target_string = calloc_string((final_length) + 1)
+ var target_string = new NativeString(final_length + 1)
target_string[final_length] = '\0'
end
+ # A mutable sequence of characters.
abstract class Buffer
super Text
# The COW flag can be set at false here, since
# it does a copy of the current `Buffer`
written = false
- var a = calloc_string(c+1)
+ var a = new NativeString(c+1)
if length > 0 then items.copy_to(a, length, 0, 0)
items = a
capacity = c
redef fun to_cstring
do
if is_dirty then
- var new_native = calloc_string(length + 1)
+ var new_native = new NativeString(length + 1)
new_native[length] = '\0'
if length > 0 then items.copy_to(new_native, length, 0, 0)
real_items = new_native
# Create a new empty string.
init do end
+ # Create a new string copied from `s`.
init from(s: Text)
do
capacity = s.length + 1
length = s.length
- items = calloc_string(capacity)
+ items = new NativeString(capacity)
if s isa FlatString then
s.items.copy_to(items, length, s.index_from, 0)
else if s isa FlatBuffer then
do
assert cap >= 0
# _items = new NativeString.calloc(cap)
- items = calloc_string(cap+1)
+ items = new NativeString(cap+1)
capacity = cap
length = 0
end
redef fun reverse
do
written = false
- var ns = calloc_string(capacity)
+ var ns = new NativeString(capacity)
var si = length - 1
var ni = 0
var it = items
private class FlatBufferCharView
super BufferCharView
- super StringCapable
redef type SELFTYPE: FlatBuffer
redef fun append(s)
do
- var my_items = target.items
var s_length = s.length
if target.capacity < s.length then enlarge(s_length + target.length)
end
# Native strings are simple C char *
extern class NativeString `{ char* `}
- super StringCapable
# Creates a new NativeString with a capacity of `length`
new(length: Int) is intern
+
+ # Get char at `index`.
fun [](index: Int): Char is intern
+
+ # Set char `item` at index.
fun []=(index: Int, item: Char) is intern
+
+ # Copy `self` to `dest`.
fun copy_to(dest: NativeString, length: Int, from: Int, to: Int) is intern
# Position of the first nul character.
while self[l] != '\0' do l += 1
return l
end
+
+ # Parse `self` as an Int.
fun atoi: Int is intern
+
+ # Parse `self` as a Float.
fun atof: Float is extern "atof"
redef fun to_s
return to_s_with_length(cstring_length)
end
+ # Returns `self` as a String of `length`.
fun to_s_with_length(length: Int): FlatString
do
assert length >= 0
return str
end
+ # Returns `self` as a new String.
fun to_s_with_copy: FlatString
do
var length = cstring_length
- var new_self = calloc_string(length + 1)
+ var new_self = new NativeString(length + 1)
copy_to(new_self, length, 0, 0)
var str = new FlatString.with_infos(new_self, length, 0, length - 1)
new_self[length] = '\0'
end
end
-# StringCapable objects can create native strings
-interface StringCapable
-
- # Allocate a string of `size`.
- protected fun calloc_string(size: Int): NativeString is intern
-end
-
redef class Sys
private var args_cache: nullable Sequence[String]
- Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/standard/collection/array.nit:782)
-Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/standard/collection/array.nit:792)
++Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/standard/collection/array.nit:785)
NativeString
N
Nit