# General graph node
class Node
+ # Type of the others nodes in the `graph`
type N: Node
# parent graph
var graph: Graph[N, Link]
- init(graph: Graph[N, Link])
+ init
do
- self.graph = graph
graph.add_node(self)
end
# Link between two nodes and associated to a graph
class Link
+ # Type of the nodes in `graph`
type N: Node
+
+ # Type of the other links in `graph`
type L: Link
+ # The graph to which belongs `self`
var graph: Graph[N, L]
+ # Origin of this link
var from: N
+
+ # Endpoint of this link
var to: N
- init(graph: Graph[N, L], from, to: N)
+ init
do
- self.graph = graph
- self.from = from
- self.to = to
-
graph.add_link(self)
end
end
# General graph
class Graph[N: Node, L: Link]
+ # Nodes in this graph
var nodes: Set[N] = new HashSet[N]
+
+ # Links in this graph
var links: Set[L] = new HashSet[L]
+ # Add a `node` to this graph
fun add_node(node: N): N
do
nodes.add(node)
return node
end
+ # Add a `link` to this graph
fun add_link(link: L): L
do
links.add(link)
return link
end
- # used to check if nodes have been searched in one pathfinding
- var pathfinding_current_evocation: Int = 0
+ # Used to check if nodes have been searched in one pathfinding
+ private var pathfinding_current_evocation: Int = 0
end
-# Result from pathfinding, a walkable path
+# Result from path finding and a walkable path
class Path[N]
+ # The total cost of this path
var total_cost: Int
+ # The list of nodes composing this path
var nodes = new List[N]
- init (cost: Int) do total_cost = cost
+ private var at: Int = 0
- var at: Int = 0
+ # Step on the path and get the next node to travel
fun step: N
do
assert nodes.length >= at else print "a_star::Path::step failed, is at_end_of_path"
return s
end
+ # Peek at the next step of the path
fun peek_step: N do return nodes[at]
+ # Are we at the end of this path?
fun at_end_of_path: Bool do return at >= nodes.length
end
# Context related to an evocation of pathfinding
class PathContext
+ # Type of the nodes in `graph`
type N: Node
+
+ # Type of the links in `graph`
type L: Link
+ # Graph to which is associated `self`
var graph: Graph[N, L]
# Worst cost of all the link's costs
# Heuristic
fun heuristic_cost(a, b: N): Int is abstract
+ # The worst cost suggested by the heuristic
fun worst_heuristic_cost: Int is abstract
end
redef fun worst_heuristic_cost do return 0
end
+# A `PathContext` for graphs with `WeightedLink`
class WeightedPathContext
super PathContext
redef type L: WeightedLink
- init(graph: Graph[N, L])
+ init
do
super
self.worst_cost = worst_cost
end
- redef var worst_cost: Int
+ redef var worst_cost: Int is noinit
redef fun cost(l) do
return l.weight
redef fun worst_heuristic_cost do return 0
end
+# A `Link` with a `weight`
class WeightedLink
super Link
+ # The `weight`, or cost, of this link
var weight: Int
-
- init(graph: Graph[N, L], from, to: N, weight: Int)
- do
- super
-
- self.weight = weight
- end
end
# Advanced path conditions with customizable accept states
extern class CppString in "C++" `{ std::string* `}
end
-redef class String
- fun to_cpp_string: CppString do return to_cstring.to_cpp_string
+redef class Text
+ # Get `self` as a `CppString`
+ fun to_cpp_string: CppString do return to_cstring.to_cpp_string(length)
end
redef class NativeString
- fun to_cpp_string: CppString in "C++" `{
- return new std::string(recv);
+ # Get `self` as a `CppString`
+ fun to_cpp_string(length: Int): CppString in "C++" `{
+ return new std::string(recv, length);
`}
end
# See the License for the specific language governing permissions and
# limitations under the License.
+# Platform for the _emscripten_ framework
+#
+# Importing this module from your project will tell _nitg_ to compile
+# to JavaScript for the _emscripten_ framework.
module emscripten is platform
`{
#include <gc.h>
`}
-redef class String
+redef class Text
+ # Run `self` as JavaScript code
fun run_js do run_js_native(self.escape_to_js.to_cstring)
+
private fun run_js_native(script: NativeString) `{ emscripten_run_script(script); `}
- fun escape_to_js: String do return self.replace('\n', "\\n")
+ # Escape the content of `self` to pass to JavaScript code
+ fun escape_to_js: Text do return replace('\n', "\\n")
+ # Raise a JavaScript alert
fun alert do "alert('{self.escape_to_js}')".run_js
end
# GPIO related functionnalities
module gpio
+# A physical binary pin
interface Pin
+ # Set the output of this pin
fun write(high: Bool) is abstract
end
# Create a new event_base to use with the rest of Libevent
new `{ return event_base_new(); `}
+
+ # Has `self` been correctly initialized?
fun is_valid: Bool do return not address_is_null
- #fun creation_ok
# Event dispatching loop
#
# Write a string to the connection
fun write(str: String)
do
- var res = native_buffer_event.write(str.to_cstring, str.length)
+ native_buffer_event.write(str.to_cstring, str.length)
end
# Write a file to the connection
# A buffer event structure, strongly associated to a connection, an input buffer and an output_buffer
extern class NativeBufferEvent `{ struct bufferevent * `}
+ # Write `length` bytes of `line`
fun write(line: NativeString, length: Int): Int `{
return bufferevent_write(recv, line, length);
`}
fun length: Int `{ return evbuffer_get_length(recv); `}
end
+# An input buffer
extern class InputNativeEvBuffer
super NativeEvBuffer
fun drain(length: Int) `{ evbuffer_drain(recv, length); `}
end
+# An output buffer
extern class OutputNativeEvBuffer
super NativeEvBuffer
# Factory to listen on sockets and create new `Connection`
class ConnectionFactory
+ # The `NativeEventBase` for the dispatch loop of this factory
var event_base: NativeEventBase
# On new connection, create the handler `Connection` object
return deserialized
end
+ # Send an empty buffer, only for the `tag`
fun send_empty(dest: Rank, tag: Tag, comm: Comm): SuccessOrError
`{
return MPI_Send(NULL, 0, MPI_CHAR, dest, tag, comm);
`}
+ # Receive an empty buffer, only for the `tag`
fun recv_empty(dest: Rank, tag: Tag, comm: Comm): SuccessOrError
`{
return MPI_Recv(NULL, 0, MPI_CHAR, dest, tag, comm, MPI_STATUS_IGNORE);
`}
- fun native_send(data: NativeCArray, count: Int, data_type: DataType, dest: Rank, tag: Tag, comm: Comm): SuccessOrError
+ # Send a `NativeCArray` `buffer` with a given `count` of `data_type`
+ fun native_send(buffer: NativeCArray, count: Int, data_type: DataType, dest: Rank, tag: Tag, comm: Comm): SuccessOrError
`{
- return MPI_Send(data, count, data_type, dest, tag, comm);
+ return MPI_Send(buffer, count, data_type, dest, tag, comm);
`}
- fun native_recv(data: NativeCArray, count: Int, data_type: DataType, dest: Rank, tag: Tag, comm: Comm, status: Status): SuccessOrError
+ # Receive into a `NativeCArray` `buffer` with a given `count` of `data_type`
+ fun native_recv(buffer: NativeCArray, count: Int, data_type: DataType, dest: Rank, tag: Tag, comm: Comm, status: Status): SuccessOrError
`{
- return MPI_Recv(data, count, data_type, dest, tag, comm, status);
+ return MPI_Recv(buffer, count, data_type, dest, tag, comm, status);
`}
+ # Probe for the next data to receive, store the result in `status`
+ #
+ # Note: If you encounter an error where the next receive does not correspond
+ # to the last `probe`, call this method twice to ensure a correct result.
fun probe(source: Rank, tag: Tag, comm: Comm, status: Status): SuccessOrError
`{
return MPI_Probe(source, tag, comm, status);
# An MPI communicator
extern class Comm `{ MPI_Comm `}
+ # The _null_ communicator, targeting no processors
new null_ `{ return MPI_COMM_NULL; `}
+
+ # The _world_ communicator, targeting all processors
new world `{ return MPI_COMM_WORLD; `}
+
+ # The _self_ communicator, targeting this processor only
new self_ `{ return MPI_COMM_SELF; `}
# Number of processors in this communicator
# An MPI rank within a communcator
extern class Rank `{ int `}
+ # Special rank accepting any processor
new any `{ return MPI_ANY_SOURCE; `}
# This Rank as an `Int`
# An MPI tag, can be defined using `Int::tag`
extern class Tag `{ int `}
+ # Special tag accepting any tag
new any `{ return MPI_ANY_TAG; `}
# This tag as an `Int`
redef type VALUE: nullable UserGroup
- init for_dropping_privileges do init("Drop privileges to user:group or simply user", "-u", "--usergroup")
- init(help: String, names: String...) do super(help, null, names)
+ # Create an `OptionUserAndGroup` for dropping privileges
+ init for_dropping_privileges
+ do
+ init("Drop privileges to user:group or simply user", null, ["-u", "--usergroup"])
+ end
redef fun convert(str)
do
return new UserGroup(words[0], words[1])
else
errors.add("Option {names.join(", ")} expected parameter in the format \"user:group\" or simply \"user\".\n")
- abort # FIXME only for nitc, remove and replace with next line when FFI is working in nitg
- #return null
+ return null
end
end
end
class Statement
private var native_statement: NativeStatement
- private init(ns: NativeStatement) do self.native_statement = ns
-
# Is this statement usable?
var is_open = true
end
end
+# A row from a `Statement`
class StatementRow
# Statement linked to `self`
var statement: Statement
- private init(s: Statement) do self.statement = s
-
# Number of entries in this row
#
# require: `self.statement.is_open`
private var index: Int
- private init(s: Statement, i: Int)
- do
- self.statement = s
- self.index = i
- end
-
# Name of the column
#
# require: `self.statement.is_open`
# Statement linked to `self`
var statement: Statement
- private init(s: Statement)
+ init
do
- self.statement = s
- self.item = new StatementRow(s)
-
+ self.item = new StatementRow(statement)
self.is_ok = statement.native_statement.step.is_row
end
- redef var item: StatementRow
+ redef var item: StatementRow is noinit
- redef var is_ok: Bool
+ redef var is_ok: Bool is noinit
# require: `self.statement.is_open`
redef fun next
class Blob
super Sqlite3Data
- private init(pointer: Pointer, length: Int)
- do
- self.pointer = pointer
- self.length = length
- end
-
+ # Pointer to the beginning of the blob
var pointer: Pointer
+
+ # Size of the blob
var length: Int
end