-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,079 additions
and
551 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
(* Copyright (C) 2017 Authors of BuckleScript | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* In addition to the permissions granted to you by the LGPL, you may combine | ||
* or link a "work that uses the Library" with a publicly distributed version | ||
* of this file to produce a combined library or application, then distribute | ||
* that combined work under the terms of your choosing, with no requirement | ||
* to comply with the obligations normally placed on you by section 4 of the | ||
* LGPL version 3 (or the corresponding section of a later version of the LGPL | ||
* should you choose to use a later version). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) | ||
|
||
|
||
type 'a t = { mutable root : 'a opt_cell} | ||
and 'a opt_cell = 'a cell Js.null | ||
and 'a cell = { | ||
head : 'a ; | ||
tail : 'a opt_cell | ||
} [@@bs.deriving abstract] | ||
|
||
|
||
let create () = t ~root:Js.null | ||
|
||
let clear s = rootSet s Js.null | ||
|
||
let copy (s : _ t) : _ t = t ~root:(root s) | ||
|
||
let push s x = | ||
rootSet s (Js.Null.return @@ cell ~head:x ~tail:(root s)) | ||
|
||
let topNull (s : 'a t) : 'a Js.null = | ||
match Js.nullToOption (root s) with | ||
| None -> Js.null | ||
| Some x -> Js.Null.return (head x) | ||
|
||
let topOpt s = | ||
match Js.nullToOption (root s) with | ||
| None -> None | ||
| Some x -> Some (head x) | ||
|
||
let isEmpty s = Js.Null.test (root s) | ||
|
||
let popNull s = | ||
match Js.nullToOption (root s) with | ||
| None -> Js.null | ||
| Some x -> | ||
rootSet s (tail x); | ||
Js.Null.return (head x) | ||
|
||
let popOpt s = | ||
match Js.nullToOption (root s) with | ||
| None -> None | ||
| Some x -> | ||
rootSet s (tail x); | ||
Some (head x) | ||
|
||
|
||
|
||
let rec lengthAux (x : _ cell) acc = | ||
match Js.nullToOption (tail x ) with | ||
| None -> acc + 1 | ||
| Some x -> lengthAux x (acc + 1) | ||
|
||
let length s = | ||
match Js.nullToOption (root s) with | ||
| None -> 0 | ||
| Some x -> lengthAux x 0 | ||
|
||
let rec iterAux (s : _ opt_cell) f = | ||
match Js.nullToOption s with | ||
| None -> () | ||
| Some x -> | ||
f (head x) [@bs]; | ||
iterAux (tail x) f | ||
|
||
let iter s f = | ||
iterAux (root s) f | ||
|
||
let dynamicPopIter s f = | ||
let cursor = ref (root s) in | ||
while !cursor != Js.null do | ||
let v = Js.Null.castUnsafe !cursor in | ||
rootSet s (tail v); | ||
f (head v) [@bs]; | ||
cursor := root s (* using root, [f] may change it*) | ||
done | ||
|
||
|
||
|
||
let dynamicPopForAll2Unsafe s1 s2 b = | ||
let cursor1 = ref (root s1) in | ||
let cursor2 = ref (root s2) in | ||
let sofar = ref 0 in | ||
while | ||
!cursor1 != Js.null && | ||
!sofar = 0 do | ||
let v1 = Js.Null.castUnsafe !cursor1 in | ||
rootSet s1 (tail v1); | ||
let v2 = Js.Null.castUnsafe !cursor2 in | ||
rootSet s2 (tail v2); | ||
sofar := b (head v1) (head v2) [@bs]; | ||
cursor1 := root s1; | ||
cursor2 := root s2 | ||
done; | ||
!sofar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
(* Copyright (C) 2017 Authors of BuckleScript | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* In addition to the permissions granted to you by the LGPL, you may combine | ||
* or link a "work that uses the Library" with a publicly distributed version | ||
* of this file to produce a combined library or application, then distribute | ||
* that combined work under the terms of your choosing, with no requirement | ||
* to comply with the obligations normally placed on you by section 4 of the | ||
* LGPL version 3 (or the corresponding section of a later version of the LGPL | ||
* should you choose to use a later version). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) | ||
|
||
|
||
type 'a t | ||
|
||
|
||
val create : unit -> 'a t | ||
|
||
val clear : 'a t -> unit | ||
|
||
val copy : 'a t -> 'a t | ||
(** [copy x] O(1) *) | ||
|
||
val push : 'a t -> 'a -> unit | ||
|
||
val popNull : 'a t -> 'a Js.null | ||
|
||
val popOpt : 'a t -> 'a option | ||
|
||
val topNull : 'a t -> 'a Js.null | ||
|
||
val topOpt : 'a t -> 'a option | ||
|
||
val isEmpty : 'a t -> bool | ||
|
||
val length : 'a t -> int | ||
|
||
val iter : 'a t -> ('a -> unit [@bs]) -> unit | ||
|
||
val dynamicPopIter : 'a t -> ('a -> unit [@bs]) -> unit | ||
(** [dynamicPopIter s f ] | ||
apply [f] to each element of [s]. The item is poped | ||
before applying [f], [s] will be empty after this opeartion | ||
*) | ||
|
||
val dynamicPopForAll2Unsafe: | ||
'a t -> 'b t -> ('a -> 'b -> int [@bs]) -> int | ||
(** [dynamicPopForAll2Unsafe s1 s2 f] assume | ||
the dynamic length of [s1] and [s2] is equal | ||
*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.