aboutsummaryrefslogtreecommitdiffstats
path: root/reference/csharp/eina/src/eina_hash.cs
blob: 7b155e705190429b9a102861daa545778c7c216a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Eina.Hash examples.
 *
 * These examples demonstrate how to work with Eina.Hash data and methods.
 *
 * We have two main hash objects here, firstly an int keyed hash with some
 * dummy content.
 * The second example is that of a simple telephone book. The names are the
 * string keys to the hash with phone numbers in the data field.
 */

using System;
using System.Collections.Generic;

public class Example
{
    static Eina.Hash<Int32, string> CreateHash()
    {
        // Let's create a simple hash with integers as keys
        var hash = new Eina.Hash<Int32, string>();

        // Add initial entries to our hash
        for (int i = 0; i < 10; ++i)
            hash.Add(i, $"The number {i}");

        return hash;
    }

    static void HashDemo()
    {
        var hash = CreateHash();

        // Get an iterator of the keys so we can print a line per entry
        var iter = hash.Keys();

        Console.WriteLine("Print contents of int hash");
        foreach (int key in iter)
        {
            // Look up the value for the key so we can print both
            string value = hash.Find(key);
            Console.WriteLine($"  Item found with id {key} has value {value}");
        }
        iter.Dispose();
        Console.WriteLine("");

        hash.Dispose();
    }

    // Here we begin the phone book example

    static void PrintPhonebookEntry(string key, string data)
    {
        Console.WriteLine($"  Name: {key}\tNumber {data}\n");
    }

    static void PrintPhonebook(Eina.Hash<string, string> book)
    {
        int count = book.Population();
        Console.WriteLine($"Complete phone book ({count}):");

        // As an enumerator, iterate over the key and value for each entry
        foreach (KeyValuePair<string, string> kvp in book)
            PrintPhonebookEntry(kvp.Key, kvp.Value);

        Console.WriteLine("");
    }

    static Eina.Hash<string, string> CreatePhonebook()
    {
        string[] names =
        {
            "Wolfgang Amadeus Mozart", "Ludwig van Beethoven",
            "Richard Georg Strauss", "Heitor Villa-Lobos"
        };
        string[] numbers =
        {
            "+01 23 456-78910", "+12 34 567-89101",
            "+23 45 678-91012", "+34 56 789-10123"
        };

        // Create hash of strings to strings
        var hash = new Eina.Hash<string, string>();

        // Add initial entries to our hash
        for (int i = 0; i < 4; ++i)
            hash.Add(names[i], numbers[i]);

        return hash;
    }

    static void PhonebookDemo()
    {
        string lookup_name = "Ludwig van Beethoven";

        var phone_book = CreatePhonebook();
        PrintPhonebook(phone_book);

        string number = phone_book.Find(lookup_name);
        Console.WriteLine("Found entry:");
        PrintPhonebookEntry(lookup_name, number);
        Console.WriteLine("");

        // Let's first add a new entry
        phone_book["Raul Seixas"] = "+55 01 234-56789";

        // Change the phone number for an entry
        string old_num = phone_book[lookup_name];
        phone_book[lookup_name] = "+12 34 222-22222";
        Console.WriteLine($"Old number for {lookup_name} was {old_num}\n");

        // Change the name (key) on an entry
        phone_book.Move("Raul Seixas", "Alceu Valenca");

        Console.WriteLine("After modifications");
        PrintPhonebook(phone_book);

        phone_book.Dispose();
    }

    public static void Main()
    {
        Efl.All.Init();

        HashDemo();
        PhonebookDemo();

        Efl.All.Shutdown();
    }
}