Skip to content

Commit 4d6545a

Browse files
docs: add libgee collection samples (#71)
* docs: add vala collections libgee * correct equality function name; remove iterable * remove iterable.vala
1 parent ae6ba6f commit 4d6545a

File tree

8 files changed

+164
-2
lines changed

8 files changed

+164
-2
lines changed

source/developer-guides/documentation/vala-for-csharp-devs/33-collections.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ IList List
2929
Deque
3030
========================== ===
3131

32-
See `Gee Examples <https://wiki.gnome.org/Projects/Vala/GeeSamples>`_
32+
See :doc:`Gee Examples </developer-guides/gee-samples>`
3333

3434
You can access and assign Gee collection items via indexers (e.g.
3535
``my_map[key]`` is equivalent to ``my_map.get (key)``). Vala supports an ``in``
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Vala Collections: libgee
2+
========================
3+
4+
You should install the Vala collection library, libgee, from your distribution.
5+
More information about libgee is available from
6+
`https://wiki.gnome.org/Projects/Libgee <https://wiki.gnome.org/Projects/Libgee>`_
7+
8+
`Gee API Documentation <https://valadoc.org/gee-0.8/index.htm>`_
9+
10+
.. toctree::
11+
:glob:
12+
:maxdepth: 1
13+
14+
gee-samples/*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
List Sample
2+
===========
3+
4+
A dynamic length `ArrayList <https://valadoc.org/gee-0.8/Gee.ArrayList.html>`_
5+
6+
.. code-block:: vala
7+
8+
using Gee;
9+
10+
void main () {
11+
var list = new ArrayList<int> ();
12+
list.add (1);
13+
list.add (2);
14+
list.add (5);
15+
list.add (4);
16+
list.insert (2, 3);
17+
list.remove_at (3);
18+
foreach (int i in list) {
19+
stdout.printf ("%d\n", i);
20+
}
21+
list[2] = 10; // same as list.set (2, 10)
22+
stdout.printf ("%d\n", list[2]); // same as list.get (2)
23+
}
24+
25+
Compile and Run
26+
---------------
27+
28+
.. code-block:: bash
29+
30+
$ valac --pkg gee-0.8 gee-list.vala
31+
$ ./gee-list
32+
33+
You can use any type fitting into the size of a pointer (e.g. int, bool,
34+
reference types) directly as generic type argument: ``bool``, ``int``, ``string``,
35+
``MyObject``. Other types must be "boxed" by appending a question mark: ``float?``,
36+
``double?``, ``MyStruct?``. The compiler will tell you this if necessary.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Set Sample
2+
==========
3+
4+
`Sets <https://valadoc.org/gee-0.8/Gee.HashSet.html>`_ are unordered and do not
5+
contain duplicate elements.
6+
7+
.. code-block:: vala
8+
9+
using Gee;
10+
11+
void main () {
12+
var my_set = new HashSet<string> ();
13+
my_set.add ("one");
14+
my_set.add ("two");
15+
my_set.add ("three");
16+
my_set.add ("two"); // will not be added because it's a duplicate
17+
foreach (string s in my_set) {
18+
stdout.printf ("%s\n", s);
19+
}
20+
}
21+
22+
Compile and Run
23+
---------------
24+
25+
.. code-block:: bash
26+
27+
$ valac --pkg gee-0.8 gee-set.vala
28+
$ ./gee-set
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Map Example
2+
===========
3+
4+
`Maps <https://valadoc.org/gee-0.8/Gee.HashMap.html>`_ work like a dictionary.
5+
They store key - value pairs.
6+
7+
.. code-block:: vala
8+
9+
using Gee;
10+
11+
void main () {
12+
13+
var map = new HashMap<string, int> ();
14+
15+
// Setting values
16+
map.set ("one", 1);
17+
map.set ("two", 2);
18+
map.set ("three", 3);
19+
map["four"] = 4; // same as map.set ("four", 4)
20+
map["five"] = 5;
21+
22+
// Getting values
23+
int a = map.get ("four");
24+
int b = map["four"]; // same as map.get ("four")
25+
assert (a == b);
26+
27+
// Iteration
28+
29+
stdout.printf ("Iterating over entries\n");
30+
foreach (var entry in map.entries) {
31+
stdout.printf ("%s => %d\n", entry.key, entry.value);
32+
}
33+
34+
stdout.printf ("Iterating over keys only\n");
35+
foreach (string key in map.keys) {
36+
stdout.printf ("%s\n", key);
37+
}
38+
39+
stdout.printf ("Iterating over values only\n");
40+
foreach (int value in map.values) {
41+
stdout.printf ("%d\n", value);
42+
}
43+
44+
stdout.printf ("Iterating via 'for' statement\n");
45+
var it = map.map_iterator ();
46+
for (var has_next = it.next (); has_next; has_next = it.next ()) {
47+
stdout.printf ("%d\n", it.get_value ());
48+
}
49+
}
50+
51+
Compile and Run
52+
---------------
53+
54+
.. code-block:: bash
55+
56+
$ valac --pkg gee-0.8 gee-map.vala
57+
$ ./gee-map
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Syntactic Sugar
2+
===============
3+
4+
There's syntactic sugar for testing if a collection contains an element:
5+
6+
.. code-block:: vala
7+
8+
if ("three" in my_set) { // same as my_set.contains ("three")
9+
stdout.printf ("heureka\n");
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Customizing the equality function
2+
=================================
3+
4+
`ArrayList <https://valadoc.org/gee-0.8/Gee.ArrayList.html>`_ supports the
5+
``contains (G item)`` its equality function can be customized by providing an
6+
implementation of ``EqualDataFunc``.
7+
8+
.. code-block:: vala
9+
10+
bool same_book (Book a, Book b) {
11+
return a.isbn == b.isbn;
12+
}
13+
14+
var books = new Gee.ArrayList<Book> ((EqualDataFunc) same_book);
15+
16+
Other `delegates <https://valadoc.org/gee-0.8/Gee.Functions.html>`_ such as
17+
``CompareDataFunc`` and ``HashDataFunc`` apply to other Collections.

source/tutorials/programming-language/main/04-00-advanced-features/04-04-collections.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Also common to every *Collection* type is the *Iterable* interface. This means t
1717

1818
All classes and interfaces use the generics system. This means that they must be instantiated with a particular type or set of types that they will contain. The system will ensure that only the intended types can be put into the collections, and that when objects are retrieved they are returned as the correct type.
1919

20-
`Gee API documentation <http://valadoc.org/gee-0.8/index.htm>`_, `Gee Examples <https://wiki.gnome.org/Projects/Vala/GeeSamples>`_.
20+
`Gee API documentation <http://valadoc.org/gee-0.8/index.htm>`_, :doc:`Gee Examples </developer-guides/gee-samples>`.
2121

2222
Some important Gee classes are:
2323

0 commit comments

Comments
 (0)