# Utilities and performant structure for the FFI with C
module c
+import standard
+intrude import standard::collection::array
# A thin wrapper around a `NativeCArray` adding length information
abstract class CArray[E]
class DummyArray
super Set[Int]
super ArrayCapable[Int]
- var _capacity: Int
- var _length: Int
- redef fun length do return _length
- var _keys: NativeArray[Int]
- var _values: NativeArray[Int]
+ private var capacity: Int
+ redef var length: Int
+ private var keys: NativeArray[Int]
+ private var values: NativeArray[Int]
redef fun add(value: Int)
do
class DummyIterator
super Iterator[Int]
- var _array: DummyArray
- var _pos: Int
+ private var array: DummyArray
+ private var pos: Int
redef fun item: Int
do
class StreamCat
super FilterIStream
- var _streams: Iterator[IStream]
+ private var streams: Iterator[IStream]
redef fun eof: Bool
do
class StreamDemux
super FilterOStream
- var _streams: Array[OStream]
+ private var streams: Array[OStream]
redef fun is_writable: Bool
do
redef fun node_at_idx(i,k)
do
sys.gt_count += 1
- sys.gt_tot_length += _length
+ sys.gt_tot_length += _the_length
sys.gt_tot_cap += _capacity
var c = _array[i]
if c != null and c._next_in_bucklet != null then gt_collide(i,k)
do
sys.st_count += 1
if _array[i] != null then st_collide(i,n)
- sys.st_tot_length += _length
+ sys.st_tot_length += _the_length
sys.st_tot_cap += _capacity
super
#
# Provides PNaCl support for Nit.
module pnacl is platform
+
+import standard
+intrude import standard::stream
+
`{
#include <unistd.h>
#include <stddef.h>
module socket
import socket_c
+intrude import standard::stream
# Portal for communication between two machines
class Socket
redef var is_ok: Bool = true
- var _container: Container[E]
+ private var container: Container[E]
end
# Items can be removed from this collection
_iter.next
end
- var _iter: Iterator[Couple[K,E]]
+ private var iter: Iterator[Couple[K,E]]
init(i: Iterator[Couple[K,E]]) do _iter = i
end
abstract class AbstractArrayRead[E]
super SequenceRead[E]
- var _length: Int = 0
- redef fun length do return _length
+ redef var length = 0
redef fun is_empty do return _length == 0
end
# The internal storage.
- var _items: nullable NativeArray[E] = null
+ private var items: nullable NativeArray[E] = null
# Do not use this method
# FIXME: Remove it once modules can intrude non local modules
fun intern_items: NativeArray[E] do return _items.as(not null)
# The size of `_items`.
- var _capacity: Int = 0
+ private var capacity: Int = 0
redef fun ==(o)
do
_index = 0
end
- var _index: Int = 0
- redef fun index do return _index
- var _array: AbstractArrayRead[E]
+ redef var index = 0
+
+ private var array: AbstractArrayRead[E]
end
private class ArrayReverseIterator[E]
super Set[E]
# The stored elements.
- var _array: Array[E]
+ private var array: Array[E]
redef fun has(e) do return _array.has(e)
init(iter: ArrayIterator[E]) do _iter = iter
- var _iter: ArrayIterator[E]
+ private var iter: ArrayIterator[E]
end
end
# Internal storage.
- var _items = new Array[Couple[K,E]]
+ private var items = new Array[Couple[K,E]]
# fast remove the ith element of the array
private fun remove_at_index(i: Int)
end
# The last positive result given by a index(1) call
- var _last_index: Int = 0
+ private var last_index: Int = 0
# Where is the `key` in `_item`?
# return -1 if not found
private abstract class HashCollection[K: Object, N: HashNode[Object]]
super ArrayCapable[nullable N]
- var _array: nullable NativeArray[nullable N] = null # Used to store items
- var _capacity: Int = 0 # Size of _array
- var _length: Int = 0 # Number of items in the map
+ 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 _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)
+ 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)
# The last key accessed (used for cache)
- var _last_accessed_key: nullable K = null
+ private var last_accessed_key: nullable K = null
# The last node accessed (used for cache)
- var _last_accessed_node: nullable N = null
+ private var last_accessed_node: nullable N = null
# Return the index of the key k
fun index_at(k: K): Int
_last_accessed_node = node
# Enlarge if needed
- var l = _length
- _length = l + 1
+ var l = _the_length
+ _the_length = l + 1
# Magic values determined empirically
# We do not want to enlarge too much
end
# Remove the item in the array
- _length -= 1
+ _the_length -= 1
prev = node._prev_in_bucklet
next = node._next_in_bucklet
if prev != null then
_array[i] = null
i -= 1
end
- _length = 0
+ _the_length = 0
_first_item = null
_last_item = null
_last_accessed_key = null
do
var old_cap = _capacity
# get a new capacity
- if cap < _length + 1 then cap = _length + 1
+ if cap < _the_length + 1 then cap = _the_length + 1
if cap <= _capacity then return
_capacity = cap
_last_accessed_key = null
end
private abstract class HashNode[K: Object]
- var _key: K
+ private var key: K
type N: HashNode[K]
- 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
+ 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
init(k: K)
do
_key = k
redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
- redef fun length do return _length
+ redef fun length do return _the_length
- redef fun is_empty do return _length == 0
+ redef fun is_empty do return _the_length == 0
redef fun []=(key, v)
do
init
do
_capacity = 0
- _length = 0
+ _the_length = 0
enlarge(0)
end
private class HashMapNode[K: Object, V]
super HashNode[K]
redef type N: HashMapNode[K, V]
- var _value: V
+ private var value: V
init(k: K, v: V)
do
end
# The map to iterate on
- var _map: HashMap[K, V]
+ private var map: HashMap[K, V]
# The current node
- var _node: nullable HashMapNode[K, V]
+ private var node: nullable HashMapNode[K, V]
init(map: HashMap[K, V])
do
super Set[E]
super HashCollection[E, HashSetNode[E]]
- redef fun length do return _length
+ redef fun length do return _the_length
- redef fun is_empty do return _length == 0
+ redef fun is_empty do return _the_length == 0
redef fun first
do
- assert _length > 0
+ assert _the_length > 0
return _first_item._key
end
init
do
_capacity = 0
- _length = 0
+ _the_length = 0
enlarge(0)
end
end
# The set to iterate on
- var _set: HashSet[E]
+ private var set: HashSet[E]
# The position in the internal map storage
- var _node: nullable HashSetNode[E]
+ private var node: nullable HashSetNode[E]
init(set: HashSet[E])
do
init from(coll: Collection[E]) do append(coll)
# The first node of the list
- var _head: nullable ListNode[E]
+ private var head: nullable ListNode[E]
# The last node of the list
- var _tail: nullable ListNode[E]
+ private var tail: nullable ListNode[E]
# Get the `i`th node. get `null` otherwise.
private fun get_node(i: Int): nullable ListNode[E]
end
# The current list
- var _list: List[E]
+ private var list: List[E]
# The current node of the list
- var _node: nullable ListNode[E]
+ private var node: nullable ListNode[E]
# The index of the current node
- var _index: Int
-
- redef fun index do return _index
+ redef var index
# Remove the current item
fun delete
private class IteratorRange[E: Discrete]
# Iterator on ranges.
super Iterator[E]
- var _range: Range[E]
- var _item: E
- redef fun item do return _item
+ private var range: Range[E]
+ redef var item
redef fun is_ok do return _item < _range.after
var path: nullable String = null
# The FILE *.
- var _file: nullable NativeFile = null
+ private var file: nullable NativeFile = null
fun file_stat: FileStat do return _file.file_stat
redef fun write(s)
do
- assert _writable
+ assert _is_writable
if s isa FlatText then
write_native(s.to_cstring, s.length)
else
end
end
- redef fun is_writable do return _writable
-
redef fun close
do
var i = _file.io_close
- _writable = false
+ _is_writable = false
end
- # Is the file open in write mode
- var _writable: Bool
+ redef var is_writable = false
# Write `len` bytes from `native`.
private fun write_native(native: NativeString, len: Int)
do
- assert _writable
+ assert _is_writable
var err = _file.io_write(native, len)
if err != len then
# Big problem
print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
end
self.path = path
- _writable = true
+ _is_writable = true
end
private init do end
private init do
_file = new NativeFile.native_stdout
path = "/dev/stdout"
- _writable = true
+ _is_writable = true
end
end
private init do
_file = new NativeFile.native_stderr
path = "/dev/stderr"
- _writable = true
+ _is_writable = true
end
end
redef fun eof do return _buffer_pos >= _buffer.length and end_reached
# The buffer
- var _buffer: nullable FlatBuffer = null
+ private var buffer: nullable FlatBuffer = null
# The current position in the buffer
- var _buffer_pos: Int = 0
+ private var buffer_pos: Int = 0
# Fill the buffer
protected fun fill_buffer is abstract
import math
import collection
+intrude import collection::array
`{
#include <stdio.h>
end
redef class Sys
- var _args_cache: nullable Sequence[String]
+ private var args_cache: nullable Sequence[String]
# The arguments of the program as given by the OS
fun program_args: Sequence[String]
end
# searched motif
- var _motif: String
+ private var motif: String
# length of the motif
- var _length: Int
+ private var length: Int
private fun bc(e: Char): Int
do
end
# good shifts
- var _gs: Array[Int]
+ private var gs: Array[Int]
# bad characters
- var _bc_table: Map[Char, Int]
+ private var bc_table: Map[Char, Int]
private fun compute_bc
do
redef class OFStream
redef fun write(s)
do
- assert _writable
+ assert is_writable
if s isa FlatText then
if s isa FlatString then
write_native(s.to_cstring, s.bytelen)
redef class OFStream
redef fun write(s)
do
- assert _writable
+ assert is_writable
if s isa FlatText then
write_native(s.to_cstring, s.bytelen)
else for i in s.substrings do write_native(i.to_cstring, i.length)
# A symbol is a unique immutable string
class Symbol
- var _string: String
+ private var string: String
redef fun to_s do return _string.to_s
# Only used by String::to_symbol
import sha1
import base64
+intrude import standard::stream
+
# Websocket compatible server, works as an extra layer to the original Sockets
class WebSocket
super BufferedIStream