|
1 | 1 | # Containers-OrderedSet |
2 | 2 |
|
3 | | -[](https://travis-ci.org/olekscode/Containers-OrderedSet) |
4 | | -[](https://ci.appveyor.com/project/olekscode/containers-orderedset) |
5 | | -[](https://coveralls.io/github/olekscode/Containers-OrderedSet?branch=master) |
6 | | -[](https://raw.githubusercontent.com/olekscode/Containers-OrderedSet/master/LICENSE) |
7 | | -[](https://pharo.org/download) |
8 | | -[](https://pharo.org/download) |
9 | | -[](https://pharo.org/download) |
| 3 | + |
| 4 | +[](LICENSE) |
10 | 5 |
|
11 | | -A `Set` where an order of elements matters or an `OrderedCollection` with no duplicates. Supports the complete API of `Set` and `OrderedCollection`. |
| 6 | +A `CTOrderedSet` is a collection that combines the properties of a `Set` and an `OrderedCollection`. It maintains the order of elements while ensuring that there are no duplicates, similar to a `Set`. This makes it a useful data structure when you need to keep track of the order in which items were added, while also enforcing uniqueness. |
| 7 | + |
| 8 | +### Key Features |
| 9 | +- **Maintains Insertion Order**: Elements are stored in the order they were added. |
| 10 | +- **Ensures Uniqueness**: No duplicate elements are allowed. |
| 11 | +- **Indexed Access**: Elements can be accessed by their index, similar to an array. |
| 12 | +- **Rich API**: Combines methods from both `Set` and `OrderedCollection`, allowing for flexible usage. |
| 13 | +- **Developer Productivity**: Simplifies code by eliminating the need for manual duplicate checks and order management. |
12 | 14 |
|
13 | 15 | ## Installation |
14 | | -To install `CTOrderedSet`, go to the Playground (`Ctrl+OW`) in your [Pharo](https://pharo.org/) image and execute the following Metacello script (select it and press Do-it button or `Ctrl+D`): |
| 16 | +To install `CTOrderedSet`, go to the Playground in your [Pharo](https://pharo.org/) image and execute the following Metacello script (select it and press Do-it button or `Ctrl+D`): |
15 | 17 |
|
16 | 18 | ```Smalltalk |
17 | 19 | Metacello new |
|
33 | 35 | To read more about baselines and Metacello, check out the [Baselines](https://github.com/pharo-open-documentation/pharo-wiki/blob/master/General/Baselines.md) article on [Pharo Wiki](https://github.com/pharo-open-documentation/pharo-wiki). |
34 | 36 |
|
35 | 37 | ## How to use it? |
| 38 | +You can create an `OrderedSet` and use it like this: |
| 39 | +```smalltalk |
| 40 | +| orderedSet | |
| 41 | +orderedSet := CTOrderedSet new. |
| 42 | +orderedSet add: 'Apple'. |
| 43 | +orderedSet add: 'Banana'. |
| 44 | +orderedSet add: 'Apple'. "This will not add a duplicate" |
| 45 | +orderedSet add: 'Cherry'. |
| 46 | +orderedSet do: [ :each | Transcript show: each; cr ]. |
| 47 | +``` |
| 48 | +This will output: |
| 49 | +``` |
| 50 | +Apple |
| 51 | +Banana |
| 52 | +Cherry |
| 53 | +``` |
36 | 54 |
|
37 | | -`CTOrderedSet` has the same API as `OrderedCollection`, extended with the methods of `Set`. So instance creation looks the same: |
| 55 | +### Real World Use Case: Blog Tag Management |
| 56 | +Let's explore a comprehensive example that demonstrates the power of `OrderedSet` in a real-world scenario - managing tags for a blog post. This example illustrates how to maintain order, ensure uniqueness, and perform efficient operations on tags. |
38 | 57 |
|
39 | | -```Smalltalk |
40 | | -firstBasket := CTOrderedSet withAll: #(apple apple orange banana orange banana). |
41 | | -secondBasket := CTOrderedSet withAll: #(banana apple banana banana). |
42 | | -``` |
| 58 | +```smalltalk |
| 59 | +"Blog post tag management - Maintain order, Ensure uniqueness" |
43 | 60 |
|
44 | | -Or you can use the `asOrderedSet` method: |
| 61 | +"Traditional approach with OrderedCollection - problematic" |
| 62 | +blogTags := OrderedCollection new. |
| 63 | +blogTags |
| 64 | + add: 'Programming'; |
| 65 | + add: 'Pharo'; |
| 66 | + add: 'Tutorial'; |
| 67 | + add: 'Programming'; "Oops! Duplicate" |
| 68 | + add: 'Beginner'; |
| 69 | + add: 'Pharo'. "Another duplicate" |
| 70 | +"Manual cleanup required - inefficient and error-prone" |
| 71 | +blogTags := blogTags asSet asOrderedCollection. "Loses order!" |
| 72 | +"Result: Unpredictable order, Manual management needed" |
45 | 73 |
|
46 | | -```Smalltalk |
47 | | -firstBasket := #(apple apple orange banana orange banana) asOrderedSet. |
48 | | -secondBasket := #(banana apple banana banana) asOrderedSet. |
| 74 | +"OrderedSet approach - Elegant and Automatic" |
| 75 | +blogTags := CTOrderedSet new. |
| 76 | +blogTags |
| 77 | + add: 'Programming'; |
| 78 | + add: 'Pharo'; |
| 79 | + add: 'Tutorial'; |
| 80 | + add: 'Programming'; "Automatically ignored - No duplicate" |
| 81 | + add: 'Beginner'; |
| 82 | + add: 'Pharo'. "Automatically ignored - No duplicate" |
| 83 | +
|
| 84 | +"Result: #('Programming' 'Pharo' 'Tutorial' 'Beginner') - Perfect order, No duplicates" |
| 85 | +
|
| 86 | +"Adding tags from user input or external sources" |
| 87 | +userInputTags := #('Advanced' 'Programming' 'Smalltalk' 'Tutorial'). |
| 88 | +blogTags addAll: userInputTags. |
| 89 | +"Result: #('Programming' 'Pharo' 'Tutorial' 'Beginner' 'Advanced' 'Smalltalk') - Maintains order, no duplicates" |
| 90 | +"Notice: Duplicates automatically filtered, new tags appended in order" |
| 91 | +
|
| 92 | +blogTags includes: 'Pharo'. "Returns: True" |
| 93 | +blogTags includes: 'JavaScript'. "Returns: False" |
| 94 | +
|
| 95 | +firstTag := blogTags first. "Access first tag - 'Programming'" |
| 96 | +lastTag := blogTags last. "Access last tag - 'Smalltalk'" |
| 97 | +thirdTag := blogTags at: 3. "Access third tag - 'Tutorial'" |
49 | 98 | ``` |
50 | 99 |
|
| 100 | +### Comparison with Alternatives |
| 101 | + |
| 102 | +| Feature | OrderedCollection | Set | CTOrderedSet | |
| 103 | +|---------|------------------|-----|-------------| |
| 104 | +| Maintains order | Yes | No | Yes | |
| 105 | +| Prevents duplicates | No | Yes | Yes | |
| 106 | +| Indexed access | Yes | No | Yes | |
| 107 | +| Manual Duplicate Check | Required | Not Required | Not Required | |
| 108 | + |
| 109 | +## Contributing |
| 110 | + |
| 111 | +This is part of the Pharo Containers project. Feel free to contribute by implementing additional methods, improving tests, or enhancing documentation. |
0 commit comments