summaryrefslogtreecommitdiff
path: root/reference/csharp/eina/src/eina_hash.cs
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousasilva@gmail.com>2018-02-16 15:24:11 -0200
committerVitor Sousa <vitorsousasilva@gmail.com>2018-02-16 15:24:11 -0200
commitbc174ae1159bf024877b84bbfad2465ab7c9bf64 (patch)
tree7c00c6f1099876501642175287f02fd109674e0c /reference/csharp/eina/src/eina_hash.cs
parent66dd51dc4baa3db406a751d024ec4c812b9b30d7 (diff)
eina_mono: add first reference examples for Eina C# binding
Diffstat (limited to 'reference/csharp/eina/src/eina_hash.cs')
-rw-r--r--reference/csharp/eina/src/eina_hash.cs129
1 files changed, 129 insertions, 0 deletions
diff --git a/reference/csharp/eina/src/eina_hash.cs b/reference/csharp/eina/src/eina_hash.cs
new file mode 100644
index 0000000..b85bfaf
--- /dev/null
+++ b/reference/csharp/eina/src/eina_hash.cs
@@ -0,0 +1,129 @@
1/*
2 * Eina Hash examples.
3 *
4 * These examples demonstrate how to work with eina_hash data and methods.
5 *
6 * We have two main hash objects here, firstly an int keyed hash with some
7 * dummy content.
8 * The second example is that of a simple telephone book. The names are the
9 * string keys to the hash with phone numbers in the data field.
10 */
11
12using System;
13using System.Collections.Generic;
14
15public class Example
16{
17 static eina.Hash<Int32, string> CreateHash()
18 {
19 // let's create a simple hash with integers as keys
20 var hash = new eina.Hash<Int32, string>();
21
22 // Add initial entries to our hash
23 for (int i = 0; i < 10; ++i)
24 hash.Add(i, $"The number {i}");
25
26 return hash;
27 }
28
29 static void HashDemo()
30 {
31 var hash = CreateHash();
32
33 // get an iterator of the keys so we can print a line per entry
34 var iter = hash.Keys();
35
36 Console.WriteLine("Print contents of int hash");
37 foreach (int key in iter)
38 {
39 // look up the value for the key so we can print both
40 string value = hash.Find(key);
41 Console.WriteLine($" Item found with id {key} has value {value}");
42 }
43 iter.Dispose();
44 Console.WriteLine("");
45
46 hash.Dispose();
47 }
48
49 // here we begin the phone book example
50
51 static void PrintPhonebookEntry(string key, string data)
52 {
53 Console.WriteLine($" Name: {key}\tNumber {data}\n");
54 }
55
56 static void PrintPhonebook(eina.Hash<string, string> book)
57 {
58 int count = book.Population();
59 Console.WriteLine($"Complete phone book ({count}):");
60
61 // as an enumerator, iterate over the key and value for each entry
62 foreach (KeyValuePair<string, string> kvp in book)
63 PrintPhonebookEntry(kvp.Key, kvp.Value);
64
65 Console.WriteLine("");
66 }
67
68 static eina.Hash<string, string> CreatePhonebook()
69 {
70 string[] names =
71 {
72 "Wolfgang Amadeus Mozart", "Ludwig van Beethoven",
73 "Richard Georg Strauss", "Heitor Villa-Lobos"
74 };
75 string[] numbers =
76 {
77 "+01 23 456-78910", "+12 34 567-89101",
78 "+23 45 678-91012", "+34 56 789-10123"
79 };
80
81 // create hash of strings to strings
82 var hash = new eina.Hash<string, string>();
83
84 // Add initial entries to our hash
85 for (int i = 0; i < 4; ++i)
86 hash.Add(names[i], numbers[i]);
87
88 return hash;
89 }
90
91 static void PhonebookDemo()
92 {
93 string lookup_name = "Ludwig van Beethoven";
94
95 var phone_book = CreatePhonebook();
96 PrintPhonebook(phone_book);
97
98 string number = phone_book.Find(lookup_name);
99 Console.WriteLine("Found entry:");
100 PrintPhonebookEntry(lookup_name, number);
101 Console.WriteLine("");
102
103 // Let's first add a new entry
104 phone_book["Raul Seixas"] = "+55 01 234-56789";
105
106 // Change the phone number for an entry
107 string old_num = phone_book[lookup_name];
108 phone_book[lookup_name] = "+12 34 222-22222";
109 Console.WriteLine($"Old number for {lookup_name} was {old_num}\n");
110
111 // Change the name (key) on an entry
112 phone_book.Move("Raul Seixas", "Alceu Valenca");
113
114 Console.WriteLine("After modifications");
115 PrintPhonebook(phone_book);
116
117 phone_book.Dispose();
118 }
119
120 public static void Main()
121 {
122 efl.All.Init();
123
124 HashDemo();
125 PhonebookDemo();
126
127 efl.All.Shutdown();
128 }
129}