Skip to content

Commit 6466835

Browse files
committed
Add generic_dictionary example
1 parent 1e9c2d8 commit 6466835

File tree

8 files changed

+206
-4
lines changed

8 files changed

+206
-4
lines changed

examples/xtd.core.examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197

198198
## [generic_Collections](generic_Collections/README.md)
199199

200+
* [generic_dictionary](generic_Collections/generic_dictionary/README.md) shows how to use [xtd::collections::generic::dictionary](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1dictionary.html) class.
200201
* [generic_icollection](generic_Collections/generic_icollection/README.md) shows how to use [xtd::collections::generic::icollection](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1icollection.html) interface.
201202
* [generic_ienumerable](generic_Collections/generic_ienumerable/README.md) shows how to use [xtd::collections::generic::ienumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ienumerable.html) interface.
202203
* [generic_ienumerable2](generic_Collections/generic_ienumerable2/README.md) shows how to use [xtd::collections::generic::ienumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ienumerable.html) interface.

examples/xtd.core.examples/generic_collections/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(generic_collections)
44
find_package(xtd REQUIRED)
55

66
add_projects(
7+
generic_dictionary
78
generic_icollection
89
generic_ienumerable
910
generic_ienumerable2

examples/xtd.core.examples/generic_collections/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[This folder](.) contains collections examples used by [Reference Guide](https://gammasoft71.github.io/xtd/reference_guides/latest/) and more.
44

5+
* [generic_dictionary](generic_dictionary/README.md) shows how to use [xtd::collections::generic::dictionary](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1dictionary.html) class.
56
* [generic_icollection](generic_icollection/README.md) shows how to use [xtd::collections::generic::icollection](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1icollection.html) interface.
67
* [generic_ienumerable](generic_ienumerable/README.md) shows how to use [xtd::collections::generic::ienumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ienumerable.html) interface.
78
* [generic_ienumerable2](generic_ienumerable2/README.md) shows how to use [xtd::collections::generic::ienumerable](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ienumerable.html) interface.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(generic_dictionary)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/generic_dictionary.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# generic_dictionary
2+
3+
Shows how to use [xtd::collections::generic::dictionary](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1dictionary.html) class.
4+
5+
## Sources
6+
7+
[src/generic_dictionary.cpp](src/generic_dictionary.cpp)
8+
9+
[CMakeLists.txt](CMakeLists.txt)
10+
11+
## Build and run
12+
13+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
14+
15+
```cmake
16+
xtdc run
17+
```
18+
19+
## Output
20+
21+
```
22+
An element with Key = "txt" already exists.
23+
For key = "rtf", value = wordpad.exe.
24+
For key = "rtf", value = winword.exe.
25+
Key = "tif" is not found.
26+
Key = "tif" is not found.
27+
Value added for key = "ht": hypertrm.exe
28+
29+
Key = doc, Value = winword.exe
30+
Key = rtf, Value = winword.exe
31+
Key = bmp, Value = paint.exe
32+
Key = ht, Value = hypertrm.exe
33+
Key = dib, Value = paint.exe
34+
Key = txt, Value = notepad.exe
35+
36+
Value = winword.exe
37+
Value = winword.exe
38+
Value = paint.exe
39+
Value = hypertrm.exe
40+
Value = paint.exe
41+
Value = notepad.exe
42+
43+
Key = doc
44+
Key = rtf
45+
Key = bmp
46+
Key = ht
47+
Key = dib
48+
Key = txt
49+
50+
Remove("doc")
51+
Key "doc" is not found.
52+
```
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <xtd/xtd>
2+
3+
using namespace xtd;
4+
using namespace xtd::collections::generic;
5+
6+
class example {
7+
public:
8+
static auto main() -> void {
9+
// Create a new dictionary of strings, with string keys.
10+
//
11+
auto open_with = dictionary<string, string>();
12+
13+
// Add some elements to the dictionary. There are no
14+
// duplicate keys, but some of the values are duplicates.
15+
open_with.add("txt", "notepad.exe");
16+
open_with.add("bmp", "paint.exe");
17+
open_with.add("dib", "paint.exe");
18+
open_with.add("rtf", "wordpad.exe");
19+
20+
// The Add method throws an exception if the new key is
21+
// already in the dictionary.
22+
try {
23+
open_with.add("txt", "winword.exe");
24+
} catch (const argument_exception&) {
25+
console::write_line("An element with Key = \"txt\" already exists.");
26+
}
27+
28+
// The Item property is another name for the indexer, so you
29+
// can omit its name when accessing elements.
30+
console::write_line("For key = \"rtf\", value = {0}.", xtd::as_const(open_with)["rtf"]);
31+
32+
// The indexer can be used to change the value associated
33+
// with a key.
34+
open_with["rtf"] = "winword.exe";
35+
console::write_line("For key = \"rtf\", value = {0}.", xtd::as_const(open_with)["rtf"]);
36+
37+
// If a key does not exist, setting the indexer for that key
38+
// adds a new key/value pair.
39+
open_with["doc"] = "winword.exe";
40+
41+
// The indexer throws an exception if the requested key is not in the dictionary.
42+
try {
43+
console::write_line("For key = \"tif\", value = {0}.", xtd::as_const(open_with)["tif"]);
44+
} catch (const key_not_found_exception&) {
45+
console::write_line("Key = \"tif\" is not found.");
46+
}
47+
48+
// When a program often has to try keys that turn out not to
49+
// be in the dictionary, TryGetValue can be a more efficient
50+
// way to retrieve values.
51+
auto value = ""_s;
52+
if (open_with.try_get_value("tif", value))
53+
console::write_line("For key = \"tif\", value = {0}.", value);
54+
else
55+
console::write_line("Key = \"tif\" is not found.");
56+
57+
// ContainsKey can be used to test keys before inserting
58+
// them.
59+
if (!open_with.contains_key("ht")) {
60+
open_with.add("ht", "hypertrm.exe");
61+
console::write_line("Value added for key = \"ht\": {0}", xtd::as_const(open_with)["ht"]);
62+
}
63+
64+
// When you use foreach to enumerate dictionary elements,
65+
// the elements are retrieved as KeyValuePair objects.
66+
console::write_line();
67+
for (const key_value_pair<string, string>& kvp : open_with)
68+
console::write_line("Key = {0}, Value = {1}", kvp.key(), kvp.value());
69+
70+
// To get the values alone, use the Values property.
71+
dictionary<string, string>::value_collection values = open_with.values();
72+
73+
// The elements of the ValueCollection are strongly typed
74+
// with the type that was specified for dictionary values.
75+
console::write_line();
76+
for(const string& s : values)
77+
console::write_line("Value = {0}", s);
78+
79+
// To get the keys alone, use the Keys property.
80+
dictionary<string, string>::key_collection keys = open_with.keys();
81+
82+
// The elements of the KeyCollection are strongly typed
83+
// with the type that was specified for dictionary keys.
84+
console::write_line();
85+
for(const string& s : keys)
86+
console::write_line("Key = {0}", s);
87+
88+
// Use the Remove method to remove a key/value pair.
89+
console::write_line("\nRemove(\"doc\")");
90+
open_with.remove("doc");
91+
92+
if (!open_with.contains_key("doc"))
93+
console::write_line("Key \"doc\" is not found.");
94+
}
95+
};
96+
97+
startup_(example::main);
98+
99+
// This code produces the following output :
100+
//
101+
// An element with Key = "txt" already exists.
102+
// For key = "rtf", value = wordpad.exe.
103+
// For key = "rtf", value = winword.exe.
104+
// Key = "tif" is not found.
105+
// Key = "tif" is not found.
106+
// Value added for key = "ht": hypertrm.exe
107+
//
108+
// Key = doc, Value = winword.exe
109+
// Key = rtf, Value = winword.exe
110+
// Key = bmp, Value = paint.exe
111+
// Key = ht, Value = hypertrm.exe
112+
// Key = dib, Value = paint.exe
113+
// Key = txt, Value = notepad.exe
114+
//
115+
// Value = winword.exe
116+
// Value = winword.exe
117+
// Value = paint.exe
118+
// Value = hypertrm.exe
119+
// Value = paint.exe
120+
// Value = notepad.exe
121+
//
122+
// Key = doc
123+
// Key = rtf
124+
// Key = bmp
125+
// Key = ht
126+
// Key = dib
127+
// Key = txt
128+
//
129+
// Remove("doc")
130+
// Key "doc" is not found.

src/xtd.core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ add_sources(
4444
include/xtd/array_abstract_object.hpp
4545
include/xtd/as.hpp
4646
include/xtd/as
47+
include/xtd/as_const.hpp
48+
include/xtd/as_const
4749
include/xtd/async_callback.hpp
4850
include/xtd/async_callback
4951
include/xtd/async_result.hpp

src/xtd.core/include/xtd/collections/generic/dictionary.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ namespace xtd {
5656
/// @par Library
5757
/// xtd.core
5858
/// @ingroup xtd_core generic_collections
59-
/// @remarks The xtd::collections::generic::dictionary class is same as [std::unordered_map](https://en.cppreference.com/w/cpp/container/unordered_map).
6059
/// @par Examples
61-
/// The following shows how to use xtd::collections::generic::dictionary.
62-
/// @include dictionary.cpp
60+
/// The following code example creates an empty xtd::collections::generic::dictionary <key_t, value_t> of strings with string keys and uses the Add method to add some elements. The example demonstrates that the xtd::collections::generic::dictionary::add method throws an xtd::argument_exception when attempting to add a duplicate key.
61+
///
62+
/// The example uses the xtd::collections::generic::dictionary::operator [] to retrieve values, demonstrating that a xtd::collections::generic::key_not_found_exception is thrown when a requested key is not present, and showing that the value associated with a key can be replaced.
63+
///
64+
/// The example shows how to use the xtd::collections::generic::dictionary::try_get_vValue method as a more efficient way to retrieve values if a program often must try key values that are not in the dictionary, and it shows how to use the xtd::collections::generic::dictionary::contains_key method to test whether a key exists before calling the xtd::collections::generic::dictionary::add method.
65+
///
66+
/// The example shows how to enumerate the keys and values in the dictionary and how to enumerate the keys and values alone using the xtd::collections::generic::dictionary::keys property and the xtd::collections::generic::dictionary::values property.
67+
///
68+
/// Finally, the example demonstrates the xtd::collections::generic::dictionary::remove method.
69+
/// @include generic_dictionary.cpp
6370
/// @remarks The xtd::collections::generic::dictionary <key_t, value_t> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the xtd::collections::generic::dictionary <key_t, value_t> class is implemented as a hash table.
6471
/// @note The speed of retrieval depends on the quality of the hashing algorithm of the type specified for `key_t`.
6572
/// @remarks As long as an object is used as a key in the xtd::collections::generic::dictionary <key_t, value_t>, it must not change in any way that affects its hash value. Every key in a xtd::collections::generic::dictionary <key_t, value_t> must be unique according to the dictionary's equality comparer.
@@ -1542,7 +1549,9 @@ namespace xtd {
15421549
/// @exception xtd::not_supported_exception The property is set and the xtd::collections::generic::dictionary <key_t, value_t> is read-only.
15431550
/// @remarks This property provides the ability to access a specific element in the collection by using the following syntax: `my_collection[key]`.
15441551
/// @remarks You can also use the `operator []` to add new elements by setting the value of a key that does not exist in the dictionary; for example, `my_collection["my_nonexistent_key"] = my_value`. However, if the specified key already exists in the dictionary, setting the `operator []` overwrites the old value. In contrast, the xtd::collections::generic::dictionary::add method does not modify existing elements.
1545-
value_t& operator [](const key_t& key) override {return data_->items[key];}
1552+
value_t& operator [](const key_t& key) override {
1553+
++data_->version;
1554+
return data_->items[key];}
15461555

15471556
/// @brief Returns a reference to the underlying base type.
15481557
/// @return Reference to the underlying base type.

0 commit comments

Comments
 (0)