Remove ArrayCapable, StringCapable and use virtual type instead of generics for HashCollection::N
Pull-Request: #919
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
end
redef class Array[E]
- super StringCapable
-
redef fun to_s: String do
var l = length
var its = _items
na[i] = tmp
i += 1
end
- var ns = calloc_string(sl + 1)
+ var ns = new NativeString(sl + 1)
ns[sl] = '\0'
i = 0
var off = 0
module ab
var words = gets.split(" ")
+if words.length != 2 then
+ print "Expected two numbers"
+ return
+end
print words[0].to_i + words[1].to_i
class DummyArray
super Set[Int]
- super ArrayCapable[Int]
private var capacity: Int
redef var length: Int
private var keys: NativeArray[Int]
init(capacity: Int)
do
_capacity = capacity
- _keys = calloc_array(capacity)
- _values = calloc_array(capacity)
+ _keys = new NativeArray[Int](capacity)
+ _values = new NativeArray[Int](capacity)
end
end
super
end
-redef class HashCollection[K,N]
+redef class HashCollection[K]
redef fun node_at_idx(i,k)
do
sys.gt_count += 1
# 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
# 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.
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
_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
# 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
# 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
# High-level abstraction for all text representations
abstract class Text
super Comparable
- super StringCapable
redef type OTHER: Text
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'
# 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
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
# 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
fun [](index: Int): Char is intern
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
- protected fun calloc_string(size: Int): NativeString is intern
-end
-
redef class Sys
private var args_cache: nullable Sequence[String]
redef fun to_cstring
do
if real_items != null then return real_items.as(not null)
- var new_items = calloc_string(bytelen + 1)
+ var new_items = new NativeString(bytelen + 1)
self.items.copy_to(new_items, bytelen, index[index_from].pos, 0)
new_items[bytelen] = '\0'
self.real_items = new_items
redef fun reversed
do
- var native = calloc_string(self.bytelen + 1)
+ var native = new NativeString(self.bytelen + 1)
var length = self.length
var index = self.index
var pos = 0
var my_real_len = length
var my_real_fin_len = my_real_len * i
- var target_string = calloc_string((finlen) + 1)
+ var target_string = new NativeString((finlen) + 1)
var my_index = index
var new_index = new StringIndex(my_real_fin_len)
redef fun to_upper
do
- var outstr = calloc_string(self.bytelen + 1)
+ var outstr = new NativeString(self.bytelen + 1)
var out_index = 0
var index = self.index
redef fun to_lower
do
- var outstr = calloc_string(self.bytelen + 1)
+ var outstr = new NativeString(self.bytelen + 1)
var out_index = 0
var index = self.index
var real_len = new Container[Int](0)
var length = cstring_length
var x = make_index(length, real_len)
- var new_self = calloc_string(length + 1)
+ var new_self = new NativeString(length + 1)
copy_to(new_self, length, 0, 0)
return new FlatString.with_infos_index(new_self, real_len.item, 0, real_len.item - 1, x, length)
end
end
redef fun reversed do
- var new_str = calloc_string(bytelen)
+ var new_str = new NativeString(bytelen)
var s_pos = bytelen
var my_pos = index_from
var its = items
end
redef fun to_upper do
- var ns = calloc_string(bytelen)
+ var ns = new NativeString(bytelen)
var offset = 0
for i in [0 .. length[
do
end
redef fun to_lower do
- var ns = calloc_string(bytelen)
+ var ns = new NativeString(bytelen)
var offset = 0
for i in [0 .. length[
do
redef fun +(o) do
if o isa Buffer then o = o.to_s
if o isa FlatString then
- var new_str = calloc_string(bytelen + o.bytelen + 1)
+ var new_str = new NativeString(bytelen + o.bytelen + 1)
var new_bytelen = bytelen + o.bytelen
new_str[new_bytelen] = '\0'
var newlen = length + o.length
var new_bytelen = mybtlen * i
var mylen = length
var newlen = mylen * i
- var ns = calloc_string(new_bytelen + 1)
+ var ns = new NativeString(new_bytelen + 1)
ns[new_bytelen] = '\0'
var offset = 0
while i > 0 do
redef fun to_cstring do
if real_items != null then return real_items.as(not null)
- var new_items = calloc_string(bytelen + 1)
+ var new_items = new NativeString(bytelen + 1)
self.items.copy_to(new_items, bytelen, index_from, 0)
new_items[bytelen] = '\0'
self.real_items = new_items
with_capacity(50)
for i in s.substrings do self.append(i)
end
- items = calloc_string(s.bytelen)
+ items = new NativeString(s.bytelen)
if s isa FlatString then
s.items.copy_to(items, s.bytelen, s.index_from, 0)
else
var c = capacity
if cap <= c then return
while c <= cap do c = c * 2 + 2
- var a = calloc_string(c+1)
+ var a = new NativeString(c+1)
if bytelen > 0 then items.copy_to(a, bytelen, 0, 0)
items = a
capacity = c
redef fun reverse
do
- var nns = calloc_string(bytelen)
+ var nns = new NativeString(bytelen)
var ns = items
var btlen = bytelen
var myp = 0
end
redef fun to_cstring do
- var ns = calloc_string(bytelen)
+ var ns = new NativeString(bytelen)
items.copy_to(ns, bytelen, 0, 0)
return ns
end
redef fun to_s_with_copy
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)
return new FlatString.with_bytelen(new_self, 0, length - 1, length)
end
redef fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
do
var ret_type = self.get_class("NativeArray").get_mtype([elttype])
+ ret_type = anchor(ret_type).as(MClassType)
return self.new_expr("NEW_{ret_type.c_name}({length})", ret_type)
end
-Runtime error: Assert 'index' failed (../lib/standard/collection/array.nit:258)
+Expected two numbers
-Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/standard/collection/array.nit:789)
+Runtime error: Cast failed. Expected `E`, got `Bool` (../lib/standard/collection/array.nit:782)
NativeString
N
Nit
end
class TestNative
- super ArrayCapable[Int]
+
init
do
a[1] = 2
print(a[0])
print(a[1])
- b = calloc_array(5)
+ b = new NativeArray[Int](5)
b[0]=200
b[1]=300
print(b[0])
# limitations under the License.
class Toto
- super ArrayCapable[Int]
+
fun toto
do
- var a = calloc_array(3)
+ var a = new NativeArray[Int](3)
a[0] = 10
a[1] = 20
a[2] = 30