|
1 | 1 | # Containers-KeyedTree |
2 | | -An implementation of KeyedTree |
| 2 | +A hierarchical data structure that provides path-based access to nested elements with dictionary-like functionality. Perfect for configuration management, file system structures & hierarchical data organization. |
3 | 3 |
|
4 | | -[](https://travis-ci.com/Ducasse/Containers-KeyedTree) |
5 | | -[](https://coveralls.io/github//Ducasse/Containers-KeyedTree?branch=master) |
6 | | -[]() |
7 | | -[](https://pharo.org/download) |
8 | | -[](https://pharo.org/download) |
9 | | -<!-- [](https://ci.appveyor.com/project/olekscode/dataframe) --> |
| 4 | + |
| 5 | +[](LICENSE) |
10 | 6 |
|
| 7 | +## What is a KeyedTree? |
| 8 | +A KeyedTree is a specialized dictionary that allows nested structures where values can be accessed through path-based keys. Each node can contain both direct values and subtrees, enabling hierarchical data organization similar to file systems or nested configurations. |
11 | 9 |
|
| 10 | +## Loading |
| 11 | +The following script installs Containers-KeyedTree in Pharo. |
12 | 12 |
|
13 | | -## Example |
14 | | -To have an overview of the features this datastructure provide, have a look at the following code snippet (extracted from a unit test): |
| 13 | +```smalltalk |
| 14 | +Metacello new |
| 15 | + baseline: 'ContainersKeyedTree'; |
| 16 | + repository: 'github://pharo-containers/Containers-KeyedTree/src'; |
| 17 | + load. |
| 18 | +``` |
15 | 19 |
|
16 | | -```st |
17 | | -firstLevelOneSubTree := CTKeyedTree new. |
18 | | -firstLevelOneSubTree |
19 | | - at: #two put: 'One-Two'; |
20 | | - at: #three put: 'One-Three'. |
21 | | - |
22 | | -tree := CTKeyedTree new. |
23 | | -tree |
24 | | - at: 1 put: firstLevelOneSubTree; |
25 | | - at: 2 put: 'Two'. |
26 | | - |
27 | | -self assert: (tree atPath: #(1)) equals: firstLevelOneSubTree. |
28 | | -self assert: (tree atPath: #(1 two)) equals: 'One-Two'. |
29 | | -self assert: (tree atPath: #(1 three)) equals: 'One-Three'. |
30 | | -self assert: (tree atPath: #(2)) equals: 'Two'. |
31 | | -self should: [ tree atPath: #(2 4) ] raise: self defaultTestError. |
32 | | -self should: [ tree atPath: #(1 two three) ] raise: self defaultTestError. |
33 | | -self should: [ tree atPath: #(3) ] raise: self defaultTestError |
| 20 | +## If you want to depend on it |
| 21 | +Add the following code to your Metacello baseline or configuration: |
| 22 | + |
| 23 | +```smalltalk |
| 24 | +spec |
| 25 | + baseline: 'ContainersKeyedTree' |
| 26 | + with: [ spec repository: 'github://pharo-containers/Containers-KeyedTree/src' ]. |
34 | 27 | ``` |
35 | 28 |
|
36 | | -## Loading |
| 29 | +## Why use Containers-KeyedTree? |
37 | 30 |
|
38 | | -The following script installs Containers-Stack in Pharo. |
| 31 | +KeyedTrees solve the problem of **organizing hierarchical data with efficient path-based access**. Perfect for configuration files, menu systems, and any data that naturally forms a tree structure. |
39 | 32 |
|
40 | | -```st |
41 | | -Metacello new |
42 | | - baseline: 'ContainersKeyedTree'; |
43 | | - repository: 'github://pharo-containers/Containers-KeyedTree:v1.0/src'; |
44 | | - load. |
45 | | -``` |
| 33 | +### Key Benefits |
| 34 | +- **Hierarchical Organization**: Natural tree structure for nested data |
| 35 | +- **Path-based Access**: Access deep elements with simple path arrays |
| 36 | +- **Flexible Values**: Store any object type at any level |
| 37 | +- **Merge Capability**: Combine trees intelligently |
| 38 | +- **Dictionary Compatibility**: Inherits from Dictionary for familiar API |
| 39 | + |
| 40 | +## Basic Usage |
| 41 | + |
| 42 | +```smalltalk |
| 43 | +"Create a hierarchical structure" |
| 44 | +tree := CTKeyedTree new. |
46 | 45 |
|
47 | | -## If you want to depend on it |
| 46 | +"Add simple values" |
| 47 | +tree at: #name put: 'MyApp'. |
| 48 | +tree at: #version put: '1.0.0'. |
48 | 49 |
|
49 | | -Add the following code to your Metacello baseline or configuration |
| 50 | +"Create nested structures" |
| 51 | +config := CTKeyedTree new. |
| 52 | +config at: #host put: 'localhost'. |
| 53 | +config at: #port put: 8080. |
| 54 | +tree at: #server put: config. |
50 | 55 |
|
| 56 | +"Access with paths" |
| 57 | +tree atPath: #(server host). "=> 'localhost'" |
| 58 | +tree atPath: #(server port). "=> 8080" |
| 59 | +tree atPath: #(version). "=> '1.0.0'" |
51 | 60 | ``` |
52 | | -spec |
53 | | - baseline: 'ContainersKeyedTree' |
54 | | - with: [ spec repository: 'github://pharo-containers/Containers-KeyedTree:v1.0/src' ] |
| 61 | + |
| 62 | +## Real-World Use Cases |
| 63 | + |
| 64 | +```smalltalk |
| 65 | +"Build a hierarchical menu structure for GUI Applications" |
| 66 | +mainMenu := CTKeyedTree new. |
| 67 | +
|
| 68 | +"File menu" |
| 69 | +fileMenu := CTKeyedTree new |
| 70 | + at: #new put: 'Create New Document'; |
| 71 | + at: #open put: 'Open Document'; |
| 72 | + at: #recent put: (CTKeyedTree new |
| 73 | + at: #doc1 put: '/path/to/recent1.txt'; |
| 74 | + at: #doc2 put: '/path/to/recent2.txt'; |
| 75 | + yourself); |
| 76 | + at: #save put: 'Save Document'; |
| 77 | + at: #exit put: 'Exit Application'; |
| 78 | + yourself. |
| 79 | +
|
| 80 | +"Edit menu" |
| 81 | +editMenu := CTKeyedTree new |
| 82 | + at: #undo put: 'Undo Last Action'; |
| 83 | + at: #redo put: 'Redo Last Action'; |
| 84 | + at: #copy put: 'Copy Selection'; |
| 85 | + at: #paste put: 'Paste Content'; |
| 86 | + yourself. |
| 87 | +
|
| 88 | +"Tools menu with nested submenus" |
| 89 | +toolsMenu := CTKeyedTree new |
| 90 | + at: #preferences put: (CTKeyedTree new |
| 91 | + at: #general put: 'General Settings'; |
| 92 | + at: #appearance put: 'Theme & UI'; |
| 93 | + at: #shortcuts put: 'Keyboard Shortcuts'; |
| 94 | + yourself); |
| 95 | + at: #plugins put: 'Manage Plugins'; |
| 96 | + yourself. |
| 97 | +
|
| 98 | +mainMenu |
| 99 | + at: #file put: fileMenu; |
| 100 | + at: #edit put: editMenu; |
| 101 | + at: #tools put: toolsMenu. |
| 102 | +
|
| 103 | +"Access menu items by path" |
| 104 | +newAction := mainMenu atPath: #(file new). "=> 'Create New Document'" |
| 105 | +recentDoc := mainMenu atPath: #(file recent doc1). "=> '/path/to/recent1.txt'" |
| 106 | +themeSettings := mainMenu atPath: #(tools preferences appearance). "=> 'Theme & UI'" |
55 | 107 | ``` |
| 108 | + |
| 109 | +## Contributing |
| 110 | +This is part of the Pharo Containers project. Feel free to contribute by implementing additional methods, improving tests, or enhancing documentation. |
0 commit comments