Skip to content

Commit 5320679

Browse files
committed
Add generic_dictionary_constructor_with_idicationary example
1 parent 6466835 commit 5320679

File tree

7 files changed

+142
-72
lines changed

7 files changed

+142
-72
lines changed

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

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

66
add_projects(
77
generic_dictionary
8+
generic_dictionary_constructor_with_idicationary
89
generic_icollection
910
generic_ienumerable
1011
generic_ienumerable2

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,34 @@ xtdc run
1919
## Output
2020

2121
```
22-
An element with Key = "txt" already exists.
22+
An element with key = "txt" already exists.
2323
For key = "rtf", value = wordpad.exe.
2424
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
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
4949
5050
Remove("doc")
51-
Key "doc" is not found.
51+
key "doc" is not found.
5252
```

examples/xtd.core.examples/generic_collections/generic_dictionary/src/generic_dictionary.cpp

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class example {
88
static auto main() -> void {
99
// Create a new dictionary of strings, with string keys.
1010
//
11-
auto open_with = dictionary<string, string>();
11+
auto open_with = dictionary<string, string> {};
1212

1313
// Add some elements to the dictionary. There are no
1414
// duplicate keys, but some of the values are duplicates.
@@ -22,15 +22,14 @@ class example {
2222
try {
2323
open_with.add("txt", "winword.exe");
2424
} catch (const argument_exception&) {
25-
console::write_line("An element with Key = \"txt\" already exists.");
25+
console::write_line("An element with key = \"txt\" already exists.");
2626
}
2727

28-
// The Item property is another name for the indexer, so you
28+
// The operator [] is another name for the indexer, so you
2929
// can omit its name when accessing elements.
3030
console::write_line("For key = \"rtf\", value = {0}.", xtd::as_const(open_with)["rtf"]);
3131

32-
// The indexer can be used to change the value associated
33-
// with a key.
32+
// The indexer can be used to change the value associated with a key.
3433
open_with["rtf"] = "winword.exe";
3534
console::write_line("For key = \"rtf\", value = {0}.", xtd::as_const(open_with)["rtf"]);
3635

@@ -42,89 +41,88 @@ class example {
4241
try {
4342
console::write_line("For key = \"tif\", value = {0}.", xtd::as_const(open_with)["tif"]);
4443
} catch (const key_not_found_exception&) {
45-
console::write_line("Key = \"tif\" is not found.");
44+
console::write_line("key = \"tif\" is not found.");
4645
}
4746

4847
// When a program often has to try keys that turn out not to
49-
// be in the dictionary, TryGetValue can be a more efficient
48+
// be in the dictionary, try_get_value can be a more efficient
5049
// way to retrieve values.
5150
auto value = ""_s;
5251
if (open_with.try_get_value("tif", value))
5352
console::write_line("For key = \"tif\", value = {0}.", value);
5453
else
55-
console::write_line("Key = \"tif\" is not found.");
54+
console::write_line("key = \"tif\" is not found.");
5655

57-
// ContainsKey can be used to test keys before inserting
58-
// them.
56+
// contains_key can be used to test keys before inserting them.
5957
if (!open_with.contains_key("ht")) {
6058
open_with.add("ht", "hypertrm.exe");
61-
console::write_line("Value added for key = \"ht\": {0}", xtd::as_const(open_with)["ht"]);
59+
console::write_line("value added for key = \"ht\": {0}", xtd::as_const(open_with)["ht"]);
6260
}
6361

64-
// When you use foreach to enumerate dictionary elements,
65-
// the elements are retrieved as KeyValuePair objects.
62+
// When you use for each to enumerate dictionary elements,
63+
// the elements are retrieved as key_value_pair objects.
6664
console::write_line();
6765
for (const key_value_pair<string, string>& kvp : open_with)
68-
console::write_line("Key = {0}, Value = {1}", kvp.key(), kvp.value());
66+
console::write_line("key = {0}, value = {1}", kvp.key(), kvp.value());
6967

70-
// To get the values alone, use the Values property.
68+
// To get the values alone, use the values property.
7169
dictionary<string, string>::value_collection values = open_with.values();
7270

73-
// The elements of the ValueCollection are strongly typed
71+
// The elements of the value_collection are strongly typed
7472
// with the type that was specified for dictionary values.
7573
console::write_line();
76-
for(const string& s : values)
77-
console::write_line("Value = {0}", s);
74+
for(const auto s : values)
75+
console::write_line("value = {0}", s);
7876

79-
// To get the keys alone, use the Keys property.
77+
// To get the keys alone, use the keys property.
8078
dictionary<string, string>::key_collection keys = open_with.keys();
8179

82-
// The elements of the KeyCollection are strongly typed
80+
// The elements of the key_collection are strongly typed
8381
// with the type that was specified for dictionary keys.
8482
console::write_line();
8583
for(const string& s : keys)
86-
console::write_line("Key = {0}", s);
84+
console::write_line("key = {0}", s);
8785

88-
// Use the Remove method to remove a key/value pair.
86+
// Use the remove method to remove a key/value pair.
8987
console::write_line("\nRemove(\"doc\")");
9088
open_with.remove("doc");
9189

9290
if (!open_with.contains_key("doc"))
93-
console::write_line("Key \"doc\" is not found.");
91+
console::write_line("key \"doc\" is not found.");
9492
}
9593
};
9694

9795
startup_(example::main);
9896

9997
// This code produces the following output :
10098
//
101-
// An element with Key = "txt" already exists.
99+
// An element with key = "txt" already exists.
102100
// For key = "rtf", value = wordpad.exe.
103101
// 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
102+
// key = "tif" is not found.
103+
// key = "tif" is not found.
104+
// value added for key = "ht": hypertrm.exe
107105
//
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
106+
// key = doc, value = winword.exe
107+
// key = rtf, value = winword.exe
108+
// key = bmp, value = paint.exe
109+
// key = ht, value = hypertrm.exe
110+
// key = dib, value = paint.exe
111+
// key = txt, value = notepad.exe
114112
//
115-
// Value = winword.exe
116-
// Value = winword.exe
117-
// Value = paint.exe
118-
// Value = hypertrm.exe
119-
// Value = paint.exe
120-
// Value = notepad.exe
113+
// value = winword.exe
114+
// value = winword.exe
115+
// value = paint.exe
116+
// value = hypertrm.exe
117+
// value = paint.exe
118+
// value = notepad.exe
121119
//
122-
// Key = doc
123-
// Key = rtf
124-
// Key = bmp
125-
// Key = ht
126-
// Key = dib
127-
// Key = txt
120+
// key = doc
121+
// key = rtf
122+
// key = bmp
123+
// key = ht
124+
// key = dib
125+
// key = txt
128126
//
129127
// Remove("doc")
130-
// Key "doc" is not found.
128+
// key "doc" is not found.
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_constructor_with_idicationary)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/generic_dictionary_constructor_with_idicationary.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# generic_dictionary_constructor_with_idicationary
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_constructor_with_idicationary.cpp](src/generic_dictionary_constructor_with_idicationary.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+
key = txt, value = notepad.exe
23+
key = bmp, value = paint.exe
24+
key = dib, value = paint.exe
25+
key = rtf, value = wordpad.exe
26+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 sorted dictionary of strings, with string keys.
10+
auto open_with = ::sorted_dictionary<string, string> {};
11+
12+
// Add some elements to the dictionary.
13+
open_with.add("txt", "notepad.exe");
14+
open_with.add("bmp", "paint.exe");
15+
open_with.add("dib", "paint.exe");
16+
open_with.add("rtf", "wordpad.exe");
17+
18+
// Create a dictionary of strings with string keys, and
19+
// initialize it with the contents of the sorted dictionary.
20+
auto copy = dictionary<string, string>(open_with);
21+
22+
// List the contents of the copy.
23+
console::write_line();
24+
for(const key_value_pair<string, string>& kvp : copy)
25+
console::write_line("key = {0}, value = {1}", kvp.key(), kvp.value());
26+
}
27+
};
28+
29+
startup_(example::main);
30+
31+
// This code produces the following output :
32+
//
33+
// key = txt, value = notepad.exe
34+
// key = bmp, value = paint.exe
35+
// key = dib, value = paint.exe
36+
// key = rtf, value = wordpad.exe

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ namespace xtd {
246246
/// @remarks The initial capacity of the new xtd::collections::generic::dictionary <key_t, value_t> is large enough to contain all the elements in dictionary.
247247
/// @remarks xtd::collections::generic::dictionary <key_t, value_t> requires an equality implementation to determine whether keys are equal. This constructor uses the default generic equality comparer, xtd::collections::generic::equality_comparer::default_equality_comparer. If type `key_t` implements the xtd::iequatable <type_t> generic interface, the default equality comparer uses that implementation. Alternatively, you can specify an implementation of the xtd::collections::generic::iequality_comparer <type_t> generic interface by using a constructor that accepts a comparer parameter.
248248
/// @remarks This constructor is an O(n) operation, where n is the number of elements in dictionary.
249+
/// @par Examples
250+
/// The following code example shows how to use the xtd::collections::generic::dictionary <key_t, value_t>(xtd::collections::generic::iequality_comparer <key_t>) constructor to initialize a xtd::collections::generic::dictionary <key_t, value_t> with sorted content from another dictionary. The code example creates a xtd::collections::generic::sorted_dictionary <key_t, value_t> and populates it with data in random order, then passes the xtd::collections::generic::sorted_dictionary <key_t, value_t> to the xtd::collections::generic::dictionary <key_t, value_t>(xtd::collections::generic::iequality_comparer <key_t>) constructor, creating a xtd::collections::generic::dictionary <key_t, value_t> that is sorted. This is useful if you need to build a sorted dictionary that at some point becomes static; copying the data from a xtd::collections::generic::sorted_dictionary <key_t, value_t> to a xtd::collections::generic::dictionary <key_t, value_t> improves retrieval speed.
251+
/// @include generic_dictionary_constructor_with_idicationary.cpp
249252
dictionary(const idictionary<key_t, value_t>& dictionary) {
250253
ensure_capacity(dictionary.count());
251254
for (const auto& item : dictionary)

0 commit comments

Comments
 (0)