Skip to content

Instantly share code, notes, and snippets.

@hath995
Created November 6, 2015 23:47
Show Gist options
  • Save hath995/3229d5bdae23cea8c76b to your computer and use it in GitHub Desktop.
Save hath995/3229d5bdae23cea8c76b to your computer and use it in GitHub Desktop.
OASISFormat: 0.4
OCamlVersion: >= 4.02.0
Name: phonedb
Version: 0.1
Maintainers: test
Homepage: test
Synopsis: Some short description
Authors: hathlesstroubles@gmail.com
License: BSD-3-clause
Plugins: META (0.4), DevFiles (0.4)
AlphaFeatures: ocamlbuild_more_args
Description:
Some cool description
# This is a comment and this below creates an binary program
Executable phonedb
Path: src
BuildTools:ocamlbuild
install: true
MainIs: phonedb.ml
CompiledObject: native
Executable "test_phonedb"
Path: test
BuildTools:ocamlbuild
install: false
Build$: flag(tests)
MainIs: phonedb_test.ml
CompiledObject: native
BuildDepends: oUnit
Test "test_phonedb"
Run$: flag(tests)
TestTools: test_phonedb
Command: $test_phonedb
WorkingDirectory: test
(* A phone number is a sequence of four integers. *)
type phone_number = int * int * int * int;;
(* A contact has a name and a phone number. *)
type contact = {
name : string;
phone_number : phone_number
};;
(* Here is a dumb contact. *)
let nobody = { name = ""; phone_number = (0, 0, 0, 0) };;
(* A database is a collection of contacts. *)
type database = {
number_of_contacts : int;
contacts : contact array;
};;
(* [make n] is the database with no contact and at most [n] contacts
stored inside. *)
let make max_number_of_contacts =
{
number_of_contacts = 0;
contacts = Array.make max_number_of_contacts nobody
};;
(* Queries are represented by a code and a contact.
- If the code is 0 then the contact must be inserted.
- If the code is 1 then the contact must be deleted.
- If the code is 2 then we are looking for a contact
with the same name in the database. *)
type query = {
code : int;
contact : contact;
}
let search db contact =
let rec aux idx =
if idx >= db.number_of_contacts then
(false, db, nobody)
else if db.contacts.(idx).name = contact.name then
(true, db, db.contacts.(idx))
else
aux (idx + 1)
in
aux 0;;
let insert db contact =
if db.number_of_contacts >= Array.length db.contacts then
(false, db, nobody)
else
let (status, db, _) = search db contact in
if status then (false, db, contact) else
let cells i =
if i = db.number_of_contacts then contact else db.contacts.(i)
in
let db' = {
number_of_contacts = db.number_of_contacts + 1;
contacts = Array.init (Array.length db.contacts) cells
}
in
(true, db', contact);;
let delete db contact =
let (status, db, contact) = search db contact in
if not status then (false, db, contact)
else
let cells i =
if db.contacts.(i).name = contact.name then
nobody
else
db.contacts.(i) in
let db' = {
number_of_contacts = db.number_of_contacts - 1;
contacts = Array.init (Array.length db.contacts) cells
}
in
(true, db', contact);;
(* Engine parses and interprets the query. *)
let engine db { code ; contact } =
if code = 0 then insert db contact
else if code = 1 then delete db contact
else if code = 2 then search db contact
else (false, db, nobody);;
(* A phone number is a sequence of four integers. *)
type phone_number = int * int * int * int;;
(* A contact has a name and a phone number. *)
type contact = {
name : string;
phone_number : phone_number
};;
(* Here is a dumb contact. *)
let nobody = { name = ""; phone_number = (0, 0, 0, 0) };;
(* A database is a collection of contacts. *)
type database = {
number_of_contacts : int;
contacts : contact array;
};;
(* [make n] is the database with no contact and at most [n] contacts
stored inside. *)
let make max_number_of_contacts =
{
number_of_contacts = 0;
contacts = Array.make max_number_of_contacts nobody
};;
(* Queries are represented by a code and a contact.
- If the code is 0 then the contact must be inserted.
- If the code is 1 then the contact must be deleted.
- If the code is 2 then we are looking for a contact
with the same name in the database. *)
type query = {
code : int;
contact : contact;
}
let search db contact =
let rec aux idx =
if idx >= db.number_of_contacts then
(false, db, nobody)
else if db.contacts.(idx).name = contact.name then
(true, db, db.contacts.(idx))
else
aux (idx + 1)
in
aux 0;;
let insert db contact =
if db.number_of_contacts >= Array.length db.contacts then
(false, db, nobody)
else
let (status, db, _) = search db contact in
if status then (false, db, contact) else
let cells i =
if i = db.number_of_contacts then contact else db.contacts.(i)
in
let db' = {
number_of_contacts = db.number_of_contacts + 1;
contacts = Array.init (Array.length db.contacts) cells
}
in
(true, db', contact);;
let delete db contact =
let (status, db, contact) = search db contact in
if not status then (false, db, contact)
else
let cells i =
if db.contacts.(i).name = contact.name then
nobody
else
db.contacts.(i) in
let db' = {
number_of_contacts = db.number_of_contacts - 1;
contacts = Array.init (Array.length db.contacts) cells
}
in
(true, db', contact);;
(* Engine parses and interprets the query. *)
let engine db { code ; contact } =
if code = 0 then insert db contact
else if code = 1 then delete db contact
else if code = 2 then search db contact
else (false, db, nobody);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment