Merge: lib: add Float::is_approx to compare floats
authorJean Privat <jean@pryen.org>
Fri, 21 Nov 2014 02:25:15 +0000 (21:25 -0500)
committerJean Privat <jean@pryen.org>
Fri, 21 Nov 2014 02:25:15 +0000 (21:25 -0500)
Pull-Request: #922
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

17 files changed:
benchmarks/strings/array_to_s_vars/array_to_s_manual.nit
examples/rosettacode/ab.nit
lib/counter.nit
lib/dummy_array.nit
lib/hash_debug.nit
lib/markdown/markdown.nit
lib/standard/collection/array.nit
lib/standard/collection/hash_collection.nit
lib/standard/string.nit
lib/string_experimentations/utf8.nit
lib/string_experimentations/utf8_noindex.nit
src/compiler/global_compiler.nit
src/doc/doc_pages.nit
tests/sav/ab.res
tests/sav/test_new_native_alt1.res
tests/test_gen.nit
tests/test_native_array.nit

index 843eb93..da6b667 100644 (file)
@@ -21,8 +21,6 @@ redef class NativeArray[E]
 end
 
 redef class Array[E]
-       super StringCapable
-
        redef fun to_s: String do
                var l = length
                var its = _items
@@ -35,7 +33,7 @@ redef class Array[E]
                        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
index 9138c0c..ce18fc0 100644 (file)
@@ -10,4 +10,8 @@
 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
index 670fc59..3d1555b 100644 (file)
@@ -19,10 +19,33 @@ import poset
 
 # A counter counts occurrences of things
 # Use this instead of a `HashMap[E, Int]`
+#
+# ~~~
+# var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+# assert c["a"]   == 2
+# assert c["b"]   == 3
+# assert c["c"]   == 1
+# assert c["d"]   == 0
+# ~~~
+#
+# The counter class can also be used to gather statistical informations.
+#
+# ~~~~
+# assert c.length == 3   # because 3 distinct values
+# assert c.max    == "b" # because "b" has the most count (3)
+# assert c.avg    == 2.0 # because it is the mean of the counts
+# ~~~~
 class Counter[E: Object]
        super Map[E, Int]
 
        # Total number of counted occurrences
+       #
+       # ~~~
+       # var c = new Counter[String]
+       # assert c.sum == 0
+       # c.inc_all(["a", "a", "b", "b", "b", "c"])
+       # assert c.sum == 6
+       # ~~~
        var sum: Int = 0
 
        private var map = new HashMap[E, Int]
@@ -64,7 +87,24 @@ class Counter[E: Object]
                sum += 1
        end
 
+       # Count one more for each element of `es`
+       fun inc_all(es: Collection[E])
+       do
+               for e in es do inc(e)
+       end
+
+       # A new Counter initialized with `inc_all`.
+       init from(es: Collection[E])
+       do
+               inc_all(es)
+       end
+
        # Return an array of elements sorted by occurrences
+       #
+       # ~~~
+       # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+       # assert c.sort == ["c", "a", "b"]
+       # ~~~
        fun sort: Array[E]
        do
                var res = map.keys.to_a
@@ -77,7 +117,7 @@ class Counter[E: Object]
        # @toimplement by default just call `to_s` on the element
        protected fun element_to_s(e: E): String
        do
-               do return e.to_s
+               return e.to_s
        end
 
        # Display statistical information
@@ -130,7 +170,14 @@ class Counter[E: Object]
                end
        end
 
-       # Return the element with the highest value
+       # Return the element with the highest value (aka. the mode)
+       #
+       # ~~~
+       # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+       # assert c.max == "b"
+       # ~~~
+       #
+       # If more than one max exists, the first one is returned.
        fun max: nullable E do
                var max: nullable Int = null
                var elem: nullable E = null
@@ -144,6 +191,13 @@ class Counter[E: Object]
        end
 
        # Return the couple with the lowest value
+       #
+       # ~~~
+       # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+       # assert c.min == "c"
+       # ~~~
+       #
+       # If more than one min exists, the first one is returned.
        fun min: nullable E do
                var min: nullable Int = null
                var elem: nullable E = null
@@ -156,13 +210,24 @@ class Counter[E: Object]
                return elem
        end
 
-       # Values average
+       # Values average (aka. arithmetic mean)
+       #
+       # ~~~
+       # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+       # assert c.avg == 2.0
+       # ~~~
        fun avg: Float do
                if values.is_empty then return 0.0
                return (sum / values.length).to_f
        end
 
        # The standard derivation of the counter values
+       #
+       # ~~~
+       # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+       # assert c.std_dev > 0.81
+       # assert c.std_dev < 0.82
+       # ~~~
        fun std_dev: Float do
                var avg = self.avg
                var sum = 0.0
index 21da4bd..88e7008 100644 (file)
@@ -12,7 +12,6 @@
 
 class DummyArray
        super Set[Int]
-       super ArrayCapable[Int]
        private var capacity: Int
        redef var length: Int
        private var keys: NativeArray[Int]
@@ -79,8 +78,8 @@ class DummyArray
        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
 
index b618729..8426097 100644 (file)
@@ -121,7 +121,7 @@ do
        super
 end
 
-redef class HashCollection[K,N]
+redef class HashCollection[K]
        redef fun node_at_idx(i,k)
        do
                sys.gt_count += 1
index 5b73d3b..add43e1 100644 (file)
@@ -30,6 +30,7 @@ import template
 # SEE: `String::md_to_html` for a shortcut.
 class MarkdownProcessor
 
+       # `MarkdownEmitter` used for ouput.
        var emitter: MarkdownEmitter is noinit
 
        init do self.emitter = new MarkdownEmitter(self)
@@ -261,6 +262,101 @@ class MarkdownProcessor
                return new LineOther
        end
 
+       # Get the token kind at `pos`.
+       fun token_at(text: Text, pos: Int): Token do
+               var c0: Char
+               var c1: Char
+               var c2: Char
+
+               if pos > 0 then
+                       c0 = text[pos - 1]
+               else
+                       c0 = ' '
+               end
+               var c = text[pos]
+
+               if pos + 1 < text.length then
+                       c1 = text[pos + 1]
+               else
+                       c1 = ' '
+               end
+               if pos + 2 < text.length then
+                       c2 = text[pos + 2]
+               else
+                       c2 = ' '
+               end
+
+               if c == '*' then
+                       if c1 == '*' then
+                               if c0 != ' ' or c2 != ' ' then
+                                       return new TokenStrongStar(pos, c)
+                               else
+                                       return new TokenEmStar(pos, c)
+                               end
+                       end
+                       if c0 != ' ' or c1 != ' ' then
+                               return new TokenEmStar(pos, c)
+                       else
+                               return new TokenNone(pos, c)
+                       end
+               else if c == '_' then
+                       if c1 == '_' then
+                               if c0 != ' ' or c2 != ' 'then
+                                       return new TokenStrongUnderscore(pos, c)
+                               else
+                                       return new TokenEmUnderscore(pos, c)
+                               end
+                       end
+                       if c0 != ' ' or c1 != ' ' then
+                               return new TokenEmUnderscore(pos, c)
+                       else
+                               return new TokenNone(pos, c)
+                       end
+               else if c == '!' then
+                       if c1 == '[' then return new TokenImage(pos, c)
+                       return new TokenNone(pos, c)
+               else if c == '[' then
+                       return new TokenLink(pos, c)
+               else if c == ']' then
+                       return new TokenNone(pos, c)
+               else if c == '`' then
+                       if c1 == '`' then
+                               return new TokenCodeDouble(pos, c)
+                       else
+                               return new TokenCodeSingle(pos, c)
+                       end
+               else if c == '\\' then
+                       if c1 == '\\' or c1 == '[' or c1 == ']' or c1 == '(' or c1 == ')' or c1 == '{' or c1 == '}' or c1 == '#' or c1 == '"' or c1 == '\'' or c1 == '.' or c1 == '<' or c1 == '>' or c1 == '*' or c1 == '+' or c1 == '-' or c1 == '_' or c1 == '!' or c1 == '`' or c1 == '~' or c1 == '^' then
+                               return new TokenEscape(pos, c)
+                       else
+                               return new TokenNone(pos, c)
+                       end
+               else if c == '<' then
+                       return new TokenHTML(pos, c)
+               else if c == '&' then
+                       return new TokenEntity(pos, c)
+               else if c == '^' then
+                       if c0 == '^' or c1 == '^' then
+                               return new TokenNone(pos, c)
+                       else
+                               return new TokenSuper(pos, c)
+                       end
+               else
+                       return new TokenNone(pos, c)
+               end
+       end
+
+       # Find the position of a `token` in `self`.
+       fun find_token(text: Text, start: Int, token: Token): Int do
+               var pos = start
+               while pos < text.length do
+                       if token_at(text, pos).is_same_type(token) then
+                               return pos
+                       end
+                       pos += 1
+               end
+               return -1
+       end
 end
 
 # Emit output corresponding to blocks content.
@@ -276,11 +372,6 @@ class MarkdownEmitter
        # Default is `HTMLDecorator`
        var decorator: Decorator = new HTMLDecorator is writable
 
-       # Create a new `MardownEmitter` using the default `HTMLDecorator`
-       init(processor: MarkdownProcessor) do
-               self.processor = processor
-       end
-
        # Create a new `MarkdownEmitter` using a custom `decorator`.
        init with_decorator(processor: MarkdownProcessor, decorator: Decorator) do
                init processor
@@ -312,7 +403,7 @@ class MarkdownEmitter
                current_text = text
                current_pos = start
                while current_pos < text.length do
-                       var mt = text.token_at(current_pos)
+                       var mt = processor.token_at(text, current_pos)
                        if (token != null and not token isa TokenNone) and
                        (mt.is_same_type(token) or
                        (token isa TokenEmStar and mt isa TokenStrongStar) or
@@ -1112,8 +1203,7 @@ class MDLine
        var next_empty: Bool = false is writable
 
        # Initialize a new MDLine from its string value
-       init(value: String) do
-               self.value = value
+       init do
                self.leading = process_leading
                if leading != value.length then
                        self.is_empty = false
@@ -1675,7 +1765,7 @@ abstract class TokenCode
 
        redef fun emit(v) do
                var a = pos + next_pos + 1
-               var b = v.current_text.find_token(a, self)
+               var b = v.processor.find_token(v.current_text.as(not null), a, self)
                if b > 0 then
                        v.current_pos = b + next_pos
                        while a < b and v.current_text[a] == ' ' do a += 1
@@ -1967,102 +2057,6 @@ end
 
 redef class Text
 
-       # Get the token kind at `pos`.
-       private fun token_at(pos: Int): Token do
-               var c0: Char
-               var c1: Char
-               var c2: Char
-
-               if pos > 0 then
-                       c0 = self[pos - 1]
-               else
-                       c0 = ' '
-               end
-               var c = self[pos]
-
-               if pos + 1 < length then
-                       c1 = self[pos + 1]
-               else
-                       c1 = ' '
-               end
-               if pos + 2 < length then
-                       c2 = self[pos + 2]
-               else
-                       c2 = ' '
-               end
-
-               if c == '*' then
-                       if c1 == '*' then
-                               if c0 != ' ' or c2 != ' ' then
-                                       return new TokenStrongStar(pos, c)
-                               else
-                                       return new TokenEmStar(pos, c)
-                               end
-                       end
-                       if c0 != ' ' or c1 != ' ' then
-                               return new TokenEmStar(pos, c)
-                       else
-                               return new TokenNone(pos, c)
-                       end
-               else if c == '_' then
-                       if c1 == '_' then
-                               if c0 != ' ' or c2 != ' 'then
-                                       return new TokenStrongUnderscore(pos, c)
-                               else
-                                       return new TokenEmUnderscore(pos, c)
-                               end
-                       end
-                       if c0 != ' ' or c1 != ' ' then
-                               return new TokenEmUnderscore(pos, c)
-                       else
-                               return new TokenNone(pos, c)
-                       end
-               else if c == '!' then
-                       if c1 == '[' then return new TokenImage(pos, c)
-                       return new TokenNone(pos, c)
-               else if c == '[' then
-                       return new TokenLink(pos, c)
-               else if c == ']' then
-                       return new TokenNone(pos, c)
-               else if c == '`' then
-                       if c1 == '`' then
-                               return new TokenCodeDouble(pos, c)
-                       else
-                               return new TokenCodeSingle(pos, c)
-                       end
-               else if c == '\\' then
-                       if c1 == '\\' or c1 == '[' or c1 == ']' or c1 == '(' or c1 == ')' or c1 == '{' or c1 == '}' or c1 == '#' or c1 == '"' or c1 == '\'' or c1 == '.' or c1 == '<' or c1 == '>' or c1 == '*' or c1 == '+' or c1 == '-' or c1 == '_' or c1 == '!' or c1 == '`' or c1 == '~' or c1 == '^' then
-                               return new TokenEscape(pos, c)
-                       else
-                               return new TokenNone(pos, c)
-                       end
-               else if c == '<' then
-                       return new TokenHTML(pos, c)
-               else if c == '&' then
-                       return new TokenEntity(pos, c)
-               else if c == '^' then
-                       if c0 == '^' or c1 == '^' then
-                               return new TokenNone(pos, c)
-                       else
-                               return new TokenSuper(pos, c)
-                       end
-               else
-                       return new TokenNone(pos, c)
-               end
-       end
-
-       # Find the position of a `token` in `self`.
-       private fun find_token(start: Int, token: Token): Int do
-               var pos = start
-               while pos < length do
-                       if token_at(pos).is_same_type(token) then
-                               return pos
-                       end
-                       pos += 1
-               end
-               return -1
-       end
-
        # Get the position of the next non-space character.
        private fun skip_spaces(start: Int): Int do
                var pos = start
index 7d710bd..ca6e9fa 100644 (file)
@@ -251,7 +251,6 @@ end
 #     assert a == b
 class Array[E]
        super AbstractArray[E]
-       super ArrayCapable[E]
 
        redef fun [](index)
        do
@@ -286,7 +285,7 @@ class Array[E]
                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
@@ -317,7 +316,7 @@ class Array[E]
        init with_capacity(cap: Int)
        do
                assert positive: cap >= 0
-               _items = calloc_array(cap)
+               _items = new NativeArray[E](cap)
                _capacity = cap
                _length = 0
        end
@@ -326,7 +325,7 @@ class Array[E]
        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
@@ -769,12 +768,6 @@ 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.
index 6490024..92b8d5c 100644 (file)
@@ -16,8 +16,8 @@ module hash_collection
 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
@@ -163,7 +163,7 @@ private abstract class HashCollection[K: Object, N: HashNode[Object]]
                _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
@@ -203,7 +203,9 @@ end
 # 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
@@ -389,7 +391,9 @@ end
 # 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
 
index 0ffb303..48cb238 100644 (file)
@@ -30,7 +30,6 @@ intrude import collection::array
 # High-level abstraction for all text representations
 abstract class Text
        super Comparable
-       super StringCapable
 
        redef type OTHER: Text
 
@@ -992,7 +991,7 @@ class FlatString
 
        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
@@ -1030,7 +1029,7 @@ class FlatString
 
        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
@@ -1050,7 +1049,7 @@ class FlatString
 
        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
@@ -1095,7 +1094,7 @@ class FlatString
                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
@@ -1173,7 +1172,7 @@ class FlatString
 
                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
@@ -1204,7 +1203,7 @@ class FlatString
 
                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'
 
@@ -1502,7 +1501,7 @@ class FlatBuffer
                # 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
@@ -1518,7 +1517,7 @@ class FlatBuffer
        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
@@ -1534,7 +1533,7 @@ class FlatBuffer
        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
@@ -1554,7 +1553,7 @@ class FlatBuffer
        do
                assert cap >= 0
                # _items = new NativeString.calloc(cap)
-               items = calloc_string(cap+1)
+               items = new NativeString(cap+1)
                capacity = cap
                length = 0
        end
@@ -1611,7 +1610,7 @@ class FlatBuffer
        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
@@ -1682,7 +1681,6 @@ end
 
 private class FlatBufferCharView
        super BufferCharView
-       super StringCapable
 
        redef type SELFTYPE: FlatBuffer
 
@@ -2118,7 +2116,6 @@ 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
        fun [](index: Int): Char is intern
@@ -2150,7 +2147,7 @@ extern class NativeString `{ char* `}
        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'
@@ -2159,11 +2156,6 @@ extern class NativeString `{ char* `}
        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]
 
index 5feef35..b7a74ce 100644 (file)
@@ -214,7 +214,7 @@ redef class FlatString
        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
@@ -245,7 +245,7 @@ redef class FlatString
 
        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
@@ -278,7 +278,7 @@ redef class FlatString
                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)
@@ -300,7 +300,7 @@ redef class FlatString
 
        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
@@ -322,7 +322,7 @@ redef class FlatString
 
        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
@@ -406,7 +406,7 @@ redef class NativeString
                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
index c473a3c..db51f0a 100644 (file)
@@ -401,7 +401,7 @@ redef class FlatString
        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
@@ -415,7 +415,7 @@ redef class FlatString
        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
@@ -427,7 +427,7 @@ redef class FlatString
        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
@@ -441,7 +441,7 @@ redef class FlatString
        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
@@ -461,7 +461,7 @@ redef class FlatString
                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
@@ -499,7 +499,7 @@ redef class FlatString
 
        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
@@ -523,7 +523,7 @@ redef class FlatBuffer
                        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
@@ -611,7 +611,7 @@ redef class FlatBuffer
                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
@@ -635,7 +635,7 @@ redef class FlatBuffer
 
        redef fun reverse
        do
-               var nns = calloc_string(bytelen)
+               var nns = new NativeString(bytelen)
                var ns = items
                var btlen = bytelen
                var myp = 0
@@ -701,7 +701,7 @@ redef class FlatBuffer
        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
@@ -723,7 +723,7 @@ redef class NativeString
        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
index 510b1ac..94dda37 100644 (file)
@@ -396,6 +396,7 @@ class GlobalCompilerVisitor
        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
 
index 9c04688..78ccd7f 100644 (file)
@@ -183,9 +183,7 @@ end
 # All entities are grouped by name to make the research easier.
 class QuickSearch
 
-       private var mmodules = new HashSet[MModule]
-       private var mclasses = new HashSet[MClass]
-       private var mpropdefs = new HashMap[String, Set[MPropDef]]
+       private var table = new QuickSearchTable
 
        var ctx: ToolContext
        var model: Model
@@ -193,51 +191,72 @@ class QuickSearch
        init do
                for mmodule in model.mmodules do
                        if mmodule.is_fictive then continue
-                       mmodules.add mmodule
+                       add_result_for(mmodule.name, mmodule.full_name, mmodule.nitdoc_url)
                end
                for mclass in model.mclasses do
                        if mclass.visibility < ctx.min_visibility then continue
-                       mclasses.add mclass
+                       add_result_for(mclass.name, mclass.full_name, mclass.nitdoc_url)
                end
                for mproperty in model.mproperties do
                        if mproperty.visibility < ctx.min_visibility then continue
                        if mproperty isa MAttribute then continue
-                       if not mpropdefs.has_key(mproperty.name) then
-                               mpropdefs[mproperty.name] = new HashSet[MPropDef]
+                       for mpropdef in mproperty.mpropdefs do
+                               var full_name = mpropdef.mclassdef.mclass.full_name
+                               var cls_url = mpropdef.mclassdef.mclass.nitdoc_url
+                               var def_url = "{cls_url}#{mpropdef.mproperty.nitdoc_id}"
+                               add_result_for(mproperty.name, full_name, def_url)
                        end
-                       mpropdefs[mproperty.name].add_all(mproperty.mpropdefs)
                end
        end
 
+       private fun add_result_for(query: String, txt: String, url: String) do
+               table[query].add new QuickSearchResult(txt, url)
+       end
+
        fun render: Template do
                var tpl = new Template
-               tpl.add "var nitdocQuickSearchRawList=\{ "
-               for mmodule in mmodules do
-                       tpl.add "{mmodule.name.to_json}:["
-                       tpl.add "\{txt:{mmodule.full_name.to_json},url:{mmodule.nitdoc_url.to_json}\},"
-                       tpl.add "],"
-               end
-               for mclass in mclasses do
-                       var full_name = mclass.intro.mmodule.full_name
-                       tpl.add "{mclass.name.to_json}:["
-                       tpl.add "\{txt:{full_name.to_json},url:{mclass.nitdoc_url.to_json}\},"
-                       tpl.add "],"
-               end
-               for mproperty, mprops in mpropdefs do
-                       tpl.add "{mproperty.to_json}:["
-                       for mpropdef in mprops do
-                               var full_name = mpropdef.mclassdef.mclass.full_name
-                               var cls_url = mpropdef.mclassdef.mclass.nitdoc_url
-                               var def_url = "{cls_url}#{mpropdef.mproperty.nitdoc_id}"
-                               tpl.add "\{txt:{full_name.to_json},url:{def_url.to_json}\},"
-                       end
-                       tpl.add "],"
-               end
-               tpl.add " \};"
+               var buffer = new RopeBuffer
+               tpl.add buffer
+               buffer.append "var nitdocQuickSearchRawList="
+               table.append_json buffer
+               buffer.append ";"
                return tpl
        end
 end
 
+# The result map for QuickSearch.
+private class QuickSearchTable
+       super JsonMapRead[String, QuickSearchResultList]
+       super HashMap[String, QuickSearchResultList]
+
+       redef fun provide_default_value(key) do
+               var v = new QuickSearchResultList
+               self[key] = v
+               return v
+       end
+end
+
+# A QuickSearch result list.
+private class QuickSearchResultList
+       super JsonSequenceRead[QuickSearchResult]
+       super Array[QuickSearchResult]
+end
+
+# A QuickSearch result.
+private class QuickSearchResult
+       super Jsonable
+
+       # The text of the link.
+       var txt: String
+
+       # The destination of the link.
+       var url: String
+
+       redef fun to_json do
+               return "\{\"txt\":{txt.to_json},\"url\":{url.to_json}\}"
+       end
+end
+
 # Nitdoc base page
 # Define page structure and properties
 abstract class NitdocPage
index a9dbe22..a5dab0a 100644 (file)
@@ -1 +1 @@
-Runtime error: Assert 'index' failed (../lib/standard/collection/array.nit:258)
+Expected two numbers
index bf79e4b..ba6c5fe 100644 (file)
@@ -1,4 +1,4 @@
-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
index 0d15f98..019f202 100644 (file)
@@ -28,7 +28,7 @@ class Toto[E]
 end
 
 class TestNative
-       super ArrayCapable[Int]
+
 
 init
 do
@@ -39,7 +39,7 @@ 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])
index aac7a83..467fa78 100644 (file)
 # 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