Compare commits
3 commits
cf6e1b8d14
...
dfdd65cd27
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | dfdd65cd27 | ||
Bruno BELANYI | 3226918995 | ||
Bruno BELANYI | eb2245db21 |
|
@ -16,7 +16,7 @@ favorite: false
|
||||||
The flyweight is a well-known
|
The flyweight is a well-known
|
||||||
[GoF](https://en.wikipedia.org/wiki/Design_Patterns) design pattern.
|
[GoF](https://en.wikipedia.org/wiki/Design_Patterns) design pattern.
|
||||||
|
|
||||||
It's intent is to minimize memory usage by reducing the number of instantiations
|
Its intent is to minimize memory usage by reducing the number of instantiations
|
||||||
of a given object.
|
of a given object.
|
||||||
|
|
||||||
I will show you how to implement a robust flyweight in C++, as well as a way to
|
I will show you how to implement a robust flyweight in C++, as well as a way to
|
||||||
|
|
|
@ -68,7 +68,7 @@ public:
|
||||||
const std::type_index lhs_i(lhs);
|
const std::type_index lhs_i(lhs);
|
||||||
const std::type_index rhs_i(rhs);
|
const std::type_index rhs_i(rhs);
|
||||||
if (lhs_i != rhs_i)
|
if (lhs_i != rhs_i)
|
||||||
returh lhs_i < rhs_i;
|
return lhs_i < rhs_i;
|
||||||
// We are now assured that both classes have the same type
|
// We are now assured that both classes have the same type
|
||||||
return less_than(rhs);
|
return less_than(rhs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,7 @@ A naive Implementation of `find(...)` is simple enough to write:
|
||||||
```python
|
```python
|
||||||
def find(self, elem: int) -> int:
|
def find(self, elem: int) -> int:
|
||||||
# If `elem` is its own parent, then it is the root of the tree
|
# If `elem` is its own parent, then it is the root of the tree
|
||||||
if (parent: = self._parent[elem]) == elem:
|
if (parent := self._parent[elem]) == elem:
|
||||||
return elem
|
return elem
|
||||||
# Otherwise, recurse on the parent
|
# Otherwise, recurse on the parent
|
||||||
return self.find(parent)
|
return self.find(parent)
|
||||||
|
@ -94,11 +94,11 @@ def find(self, elem: int) -> int:
|
||||||
|
|
||||||
However, going back up the chain of parents each time we want to find the root
|
However, going back up the chain of parents each time we want to find the root
|
||||||
node (an `O(n)` operation) would make for disastrous performance. Instead we can
|
node (an `O(n)` operation) would make for disastrous performance. Instead we can
|
||||||
do a small optimization called _path splitting.
|
do a small optimization called _path splitting_.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def find(self, elem: int) -> int:
|
def find(self, elem: int) -> int:
|
||||||
while (parent: = self._parent[elem]) != elem:
|
while (parent := self._parent[elem]) != elem:
|
||||||
# Replace each parent link by a link to the grand-parent
|
# Replace each parent link by a link to the grand-parent
|
||||||
elem, self._parent[elem] = parent, self._parent[parent]
|
elem, self._parent[elem] = parent, self._parent[parent]
|
||||||
return elem
|
return elem
|
||||||
|
|
Loading…
Reference in a new issue