Skip to content

Instantly share code, notes, and snippets.

@hackwaly
hackwaly / avltree2.ml
Last active August 25, 2023 14:56
Treap map
type ('a, 'b) tree =
| Empty
| Node of
{ left : ('a, 'b) tree
; right : ('a, 'b) tree
; key : 'a
; value : 'b
; height : int
}
@hackwaly
hackwaly / HAMT.ml
Created August 8, 2023 02:57 — forked from Guest0x0/HAMT.ml
Hash Array Mapped Trie implementation & benchmark in OCaml
(* HAMT v.s. AVL benchmark.
test for their usage as string map.
usage:
> ocamlopt HAMT.ml -o <executable-name>
> <executable-name> (avl|hamt) (random|ordered) <key-length>
time data of the form:
<tree-size> <add-time> <find-time>
@hackwaly
hackwaly / dune
Last active August 6, 2023 04:13
ppx for polymorphism gadt
(library
(name ppx_poly_gadt)
(kind ppx_rewriter)
(preprocess
(pps ppxlib.metaquot))
(libraries ppxlib))
@hackwaly
hackwaly / MutList.re
Created June 10, 2023 19:09
Efficent mutable list in reasonml
let dummy_arr = [||];
module type ELT = {
type t;
let uninitialized: t;
};
module Make = (Elt: ELT) => {
type t('a) = {
type ('data, 'index) node =
{ left : ('data, 'index) node option
; right : ('data, 'index) node option
; priority : int
; mutable data : 'data
; mutable index : 'index
}
type ('a, 'b) t = ('a, 'b) node option
@hackwaly
hackwaly / unify.ml
Last active May 4, 2023 00:11
Unify algorithm implemented in ocaml according to Alin Suciu's "Yet Another Efficient Unification Algorithm"
module type TERM = sig
type t
type tvar
val var_opt : t -> tvar option
val compare_var : tvar -> tvar -> int
val same_functor : t -> t -> bool
val args : t -> t list
end
module Make (Term : TERM) = struct
@hackwaly
hackwaly / Dllist.ml
Created August 18, 2022 07:21
Doubly linked list in OCaml
(* Doubly linked list *)
type 'a node = { mutable prev : 'a node; mutable next : 'a node; data : 'a }
type 'a list = List of 'a node [@@unboxed]
let create () =
let rec root = { prev = root; next = root; data = Obj.magic `Dummy } in
List root
let iter (List root) f =
@hackwaly
hackwaly / Treap.ts
Last active January 26, 2022 13:33
Generic persistent treap
import { assert } from "./Errors";
export type treap<a, b> = node<a, b> | undefined;
export type node<a, b> = {
left: treap<a, b>;
right: treap<a, b>;
priority: number;
data: a;
subtreeData: b;
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Caml_array from "rescript/lib/es6/caml_array.js";
function cons(_record, _map) {
while(true) {
var map = _map;
var record = _record;
if (!map) {
return {
@hackwaly
hackwaly / red_black_tree.ts
Created March 10, 2015 15:26
Red black tree implemented in typescript
enum Color {
RED,
BLACK
}
class Node {
public parent: Node;
public left: Node;
public right: Node;