From 743a5cee465d1dfa049dc9521bc78b8ce87684f1 Mon Sep 17 00:00:00 2001 From: Daniel Lucraft Date: Fri, 21 Nov 2008 06:58:38 +0000 Subject: [PATCH] Added sort method to ArrayList --- gee/arraylist.vala | 16 +++++++++++ tests/testarraylist.vala | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 0 deletions(-) diff --git a/gee/arraylist.vala b/gee/arraylist.vala index 97ce929..89dcc7e 100644 --- a/gee/arraylist.vala +++ b/gee/arraylist.vala @@ -68,6 +68,22 @@ public class Gee.ArrayList : Object, Iterable, Collection, List { return -1; } + public void sort (GLib.CompareFunc compare_func) { + GLib.qsort(_items, _size, sizeof(G), compare_func); + } + + public static int intcmp(void *a, void *b) { + int *ia = (int *) a; + int *ib = (int *) b; + return *ia - *ib; + } + + public static int strcmp(void *a, void *b) { + char **sa = (char *) a; + char **sb = (char *) b; + return GLib.strcmp(*sa, *sb); + } + public G? get (int index) { assert (index >= 0 && index < _size); diff --git a/tests/testarraylist.vala b/tests/testarraylist.vala index 9850a6a..45f842e 100644 --- a/tests/testarraylist.vala +++ b/tests/testarraylist.vala @@ -276,6 +276,70 @@ void test_arraylist_index_of () { } +class SortableThing : Object { + public int a {get; set;} + + public SortableThing(int ab) { + this.a = ab; + } + + public static int compare(void* x, void* y) { + SortableThing **x1 = (SortableThing **) x; + SortableThing **y1 = (SortableThing **) y; + return (*x1)->a - (*y1)->a; + } + + public static bool equal(SortableThing x, SortableThing y) { + return x.a == y.a; + } +} + +void test_arraylist_sort () { + var arraylistOfInt = new ArrayList (); + arraylistOfInt.add (10); + arraylistOfInt.add (4); + arraylistOfInt.add (30); + arraylistOfInt.add (2); + arraylistOfInt.add (5); + arraylistOfInt.add (100); + arraylistOfInt.sort(ArrayList.intcmp); + assert (arraylistOfInt[0] == 2); + assert (arraylistOfInt[1] == 4); + assert (arraylistOfInt[2] == 5); + assert (arraylistOfInt[3] == 10); + assert (arraylistOfInt[4] == 30); + assert (arraylistOfInt[5] == 100); + + var arraylistOfString = new ArrayList (str_equal); + + arraylistOfString.add ("one"); + arraylistOfString.add ("two"); + arraylistOfString.add ("three"); + arraylistOfString.add ("zzzz"); + arraylistOfString.add ("four"); + arraylistOfString.add ("five"); + arraylistOfString.sort(ArrayList.strcmp); + assert (arraylistOfString[0] == "five"); + assert (arraylistOfString[1] == "four"); + assert (arraylistOfString[2] == "one"); + assert (arraylistOfString[3] == "three"); + assert (arraylistOfString[4] == "two"); + assert (arraylistOfString[5] == "zzzz"); + + var arraylistOfThings = new ArrayList((GLib.EqualFunc) SortableThing.equal); + arraylistOfThings.add(new SortableThing(10)); + arraylistOfThings.add(new SortableThing(1)); + arraylistOfThings.add(new SortableThing(100)); + arraylistOfThings.add(new SortableThing(4)); + arraylistOfThings.add(new SortableThing(0)); + arraylistOfThings.sort((GLib.CompareFunc) SortableThing.compare); + assert (arraylistOfThings[0].a == 0); + assert (arraylistOfThings[1].a == 1); + assert (arraylistOfThings[2].a == 4); + assert (arraylistOfThings[3].a == 10); + assert (arraylistOfThings[4].a == 100); +} + void test_arraylist_add () { var arraylistOfString = new ArrayList (str_equal); @@ -524,6 +588,7 @@ void main (string[] args) { Test.add_func ("/Arraylist/List/insert", test_arraylist_insert); Test.add_func ("/Arraylist/List/remove_at", test_arraylist_remove_at); Test.add_func ("/Arraylist/List/index_of", test_arraylist_index_of); + Test.add_func ("/Arraylist/List/sort", test_arraylist_sort); // Methods of Collection interface Test.add_func ("/Arraylist/Collection/add", test_arraylist_add); -- 1.6.0