modular_tree 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/modular_tree/algorithms.rb +24 -12
- data/lib/modular_tree/implementations.rb +11 -0
- data/lib/modular_tree/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: a78d523f964aaf9e819f8620dbaafbad5facc1848f1baa511bf3e71625a1b0c9
         | 
| 4 | 
            +
              data.tar.gz: 59e201fe0e71be312e3f1b2545b1dbcfbccb72c7fa04465d33d6da6ed8c14acb
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 55c87a6034b013fe0f424dee4d7b2192eb1cd863c9d96085beebfbfbace4efcdaf4ac1137dcd8a1941679479502e4ef3f6b29f2d26ced05aa82f872a28c568cd
         | 
| 7 | 
            +
              data.tar.gz: f34c14d5cfc770cd2ad502370e17411396fc290aef2988bcccdeb931456add73819a8d7d7a1c5b75565c3565549859e2cbcaeae28960353a2a567bd843841cbf
         | 
| @@ -69,11 +69,18 @@ module Tree | |
| 69 69 |  | 
| 70 70 | 
             
                # Implementation of Enumerable#select extended with a single filter. As
         | 
| 71 71 | 
             
                # #each, the block is called with value, key, and parent arguments
         | 
| 72 | 
            -
                 | 
| 72 | 
            +
                #
         | 
| 73 | 
            +
                # The match expression can also be a list of classes (instead of an array of classes)
         | 
| 74 | 
            +
                #
         | 
| 75 | 
            +
                def select(*expr, this: true, &block)
         | 
| 76 | 
            +
                  !block_given? || expr.empty? or raise ArgumentError, "Can't use both match expression and block"
         | 
| 73 77 | 
             
                  if block_given?
         | 
| 74 | 
            -
                    each | 
| 78 | 
            +
                    each.select { |branch| yield branch }
         | 
| 79 | 
            +
                  elsif !expr.empty?
         | 
| 80 | 
            +
                    matcher = Matcher.new(*expr, &block)
         | 
| 81 | 
            +
                    each.select { |branch| matcher.match? branch }
         | 
| 75 82 | 
             
                  else
         | 
| 76 | 
            -
                    each | 
| 83 | 
            +
                    each
         | 
| 77 84 | 
             
                  end
         | 
| 78 85 | 
             
                end
         | 
| 79 86 |  | 
| @@ -83,14 +90,15 @@ module Tree | |
| 83 90 | 
             
                # The match expression can also be a list of classes (instead of an array of classes)
         | 
| 84 91 | 
             
                #
         | 
| 85 92 | 
             
                # TODO: Maybe make #children an Array extended with filtering
         | 
| 86 | 
            -
                def choose(* | 
| 87 | 
            -
                   | 
| 93 | 
            +
                def choose(*expr, &block)
         | 
| 94 | 
            +
                  !block_given? || expr.empty? or raise ArgumentError, "Can't use both match expression and block"
         | 
| 88 95 | 
             
                  if block_given?
         | 
| 89 | 
            -
                     | 
| 90 | 
            -
             | 
| 91 | 
            -
                     | 
| 96 | 
            +
                    each_branch.select { |branch| yield branch }
         | 
| 97 | 
            +
                  elsif !expr.empty?
         | 
| 98 | 
            +
                    matcher = Matcher.new(*expr, &block)
         | 
| 99 | 
            +
                    each_branch.select { |branch| matcher.match? branch }
         | 
| 92 100 | 
             
                  else
         | 
| 93 | 
            -
                     | 
| 101 | 
            +
                    each
         | 
| 94 102 | 
             
                  end
         | 
| 95 103 | 
             
                end
         | 
| 96 104 |  | 
| @@ -104,10 +112,14 @@ module Tree | |
| 104 112 | 
             
                # Post-order enumerator of selected nodes
         | 
| 105 113 | 
             
                def postorder(*filter, this: true) = common_each(*filter, :node_value, :each_postorder, this)
         | 
| 106 114 |  | 
| 115 | 
            +
                # TODO IDEA: 
         | 
| 116 | 
            +
                #   edges -> parent/child nodes
         | 
| 117 | 
            +
                #   pairs -> parent/.../descendant nodes
         | 
| 118 | 
            +
             | 
| 107 119 | 
             
                # Enumerator of edges in the tree. Edges are [previous-matching-node,
         | 
| 108 120 | 
             
                # matching-node] tuples. Top-level nodes have previous-matching-node set to
         | 
| 109 | 
            -
                # nil. If the filter matches all nodes the value | 
| 110 | 
            -
                # of the tree
         | 
| 121 | 
            +
                # nil (the result is a forrest). If the filter matches all nodes the value
         | 
| 122 | 
            +
                # is an edge-representation of the tree
         | 
| 111 123 | 
             
                def edges(*filter, this: true, &block)
         | 
| 112 124 | 
             
                  if block_given?
         | 
| 113 125 | 
             
                    each(*filter, this: this) { |node, _, parent| yield parent, node }
         | 
| @@ -117,7 +129,7 @@ module Tree | |
| 117 129 | 
             
                end
         | 
| 118 130 |  | 
| 119 131 | 
             
                # Return array of [previous-matching-node, matching-node] tuples. Returns
         | 
| 120 | 
            -
                # the empty array  | 
| 132 | 
            +
                # the empty array iff there is no matching set of nodes
         | 
| 121 133 | 
             
                def pairs(first_match_expr, second_match_expr, this: true)
         | 
| 122 134 | 
             
                  first_matcher = Matcher.new(first_match_expr)
         | 
| 123 135 | 
             
                  second_matcher = Matcher.new(second_match_expr)
         | 
| @@ -53,6 +53,12 @@ module Tree | |
| 53 53 | 
             
                include ParentImplementation
         | 
| 54 54 | 
             
              end
         | 
| 55 55 |  | 
| 56 | 
            +
              module InternalRootImplementation
         | 
| 57 | 
            +
                include RootProperty
         | 
| 58 | 
            +
                include InternalParentImplementation
         | 
| 59 | 
            +
                def root = @root ||= parent&.root || self
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 56 62 | 
             
              module ChildrenImplementation
         | 
| 57 63 | 
             
                include ChildrenProperty
         | 
| 58 64 | 
             
                include BranchesProperty
         | 
| @@ -153,6 +159,9 @@ module Tree | |
| 153 159 | 
             
                attr_writer :next_sibling
         | 
| 154 160 | 
             
              end
         | 
| 155 161 |  | 
| 162 | 
            +
              # TODO: A ChildrenArrayImplementation that defines #insert and #append and
         | 
| 163 | 
            +
              # adds an optional index argument to #attach
         | 
| 164 | 
            +
             | 
| 156 165 | 
             
              module InternalChildrenArrayImplementation
         | 
| 157 166 | 
             
                include InternalChildrenImplementation
         | 
| 158 167 |  | 
| @@ -177,6 +186,8 @@ module Tree | |
| 177 186 | 
             
                # index or an object
         | 
| 178 187 | 
             
                #
         | 
| 179 188 | 
             
                # TODO: Rename #attach. #insert & #append are Enumerable operations
         | 
| 189 | 
            +
                #
         | 
| 190 | 
            +
                # TODO: Default where argument - insert: 0, append: -1
         | 
| 180 191 | 
             
                def insert(where, child) = insert_append(:insert, where, child)
         | 
| 181 192 | 
             
                def append(where, child) = insert_append(:append, where, child)
         | 
| 182 193 |  | 
    
        data/lib/modular_tree/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: modular_tree
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.12.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Claus Rasmussen
         | 
| 8 8 | 
             
            autorequire:
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2023-01-05 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: constrain
         |