core: add cmp interface; src: better sort

This commit is contained in:
hellerve 2018-01-15 16:10:11 +01:00
parent c26d39e43c
commit f93eb16615
7 changed files with 16 additions and 10 deletions

View File

@ -61,4 +61,7 @@
(defn add-ref [x y]
(+ @x @y))
(defn cmp [x y]
(if (= (the Double @x) @y) 0 (if (< @x @y) -1 1)))
)

View File

@ -62,4 +62,6 @@
(defn add-ref [x y]
(+ @x @y))
(defn cmp [x y]
(if (= (the Float @x) @y) 0 (if (< @x @y) -1 1)))
)

View File

@ -42,4 +42,6 @@
(defn add-ref [x y]
(+ @x @y))
(defn cmp [x y]
(if (= (the Int @x) @y) 0 (if (< @x @y) -1 1)))
)

View File

@ -26,6 +26,7 @@
(definterface random (Fn [] a))
(definterface random-between (Fn [a a] a))
(definterface cmp (Fn [&a &a] Int))
(definterface pi a)

View File

@ -39,4 +39,7 @@
(defn even? [a] (= (mod a 2l) 0l))
(defn odd? [a] (not (even? a)))
(defn cmp [x y]
(if (= (the Long @x) @y) 0 (if (< @x @y) -1 1)))
)

View File

@ -171,17 +171,17 @@ templateSort :: (String, Binder)
templateSort = defineTypeParameterizedTemplate templateCreator path t
where path = (SymPath ["Array"] "sort")
vt = VarTy "t"
t = (FuncTy [RefTy (StructTy "Array" [vt]), FuncTy [RefTy vt, RefTy vt] IntTy] (RefTy (StructTy "Array" [vt])))
t = (FuncTy [StructTy "Array" [vt], FuncTy [RefTy vt, RefTy vt] IntTy] (StructTy "Array" [vt]))
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "Array* $NAME (Array *a, $(Fn [(Ref t), (Ref t)] Int) f)"))
(const (toTemplate "Array $NAME (Array a, $(Fn [(Ref t), (Ref t)] Int) f)"))
(const (toTemplate $ unlines ["$DECL {"
," qsort(a->data, a->len, sizeof($t), (int(*)(const void*, const void*))f);"
," qsort(a.data, a.len, sizeof($t), (int(*)(const void*, const void*))f);"
," return a;"
,"}"]))
(\(FuncTy [(RefTy arrayType), sortType] _) ->
(\(FuncTy [arrayType, sortType] _) ->
[defineFunctionTypeAlias sortType
,defineArrayTypeAlias arrayType] ++
depsForDeleteFunc typeEnv env arrayType)

View File

@ -12,11 +12,6 @@
(defn inc-ref [x] (+ @x 1))
(defn cmp [a b]
(if (= @a @b)
0
(if (> @a @b) 1 -1)))
(defn main []
(let [a (range 0 9 1)
b (Array.replicate 5 "Hi")]
@ -98,7 +93,7 @@
)
(assert-equal test
&[1 2 3 4 5 6 7 8 9]
(sort &(range 9 1 -1) cmp)
&(sort (range 9 1 -1) cmp)
"sort works as expected"
)
(assert-equal test