Documentation

Complexity.Algorithms.WordRAM.Histogram

Charged word-RAM histograms #

A fixed program clears a histogram and inserts the keys of a disjoint input array. An insertion into a bucket of size c creates 2c ordered collisions. Every address calculation, load, increment, store and loop test is charged.

Main definitions #

Main results #

References #

Number of occurrences of a key among the first k input positions.

Instances For
    @[simp]
    theorem Cslib.Algorithms.WordRAM.Histogram.count_zero (keys : ℕ → ℕ) (b : ℕ) :
    count keys 0 b = 0
    theorem Cslib.Algorithms.WordRAM.Histogram.count_succ (keys : ℕ → ℕ) (k b : ℕ) :
    count keys (k + 1) b = count keys k b + if keys k = b then 1 else 0

    Inserting one key changes just its bucket.

    theorem Cslib.Algorithms.WordRAM.Histogram.count_le (keys : ℕ → ℕ) (k b : ℕ) :
    count keys k b ≤ k

    A bucket cannot contain more positions than the prefix.

    Ordered collisions accumulated while inserting successive keys.

    Instances For
      theorem Cslib.Algorithms.WordRAM.Histogram.pairs_le_sq (keys : ℕ → ℕ) (k : ℕ) :
      pairs keys k ≤ k * k

      The prefix collision counter is at most the square of the prefix length.

      theorem Cslib.Algorithms.WordRAM.Histogram.pairs_eq_card (keys : ℕ → ℕ) (k : ℕ) :
      pairs keys k = {p ∈ (Finset.range k).offDiag | keys p.1 = keys p.2}.card

      The insertion counter counts exactly the ordered pairs of different positions with the same key, including when equal keys occur at different input positions.

      def Cslib.Algorithms.WordRAM.Histogram.memory (mem keys : ℕ → ℕ) (dst bins k : ℕ) :
      ℕ → ℕ

      The original memory, with the histogram block replaced by prefix bucket counts.

      Instances For
        theorem Cslib.Algorithms.WordRAM.Histogram.memory_read (mem keys : ℕ → ℕ) (dst bins k b : ℕ) (hb : b < bins) :
        memory mem keys dst bins k (dst + b) = count keys k b

        Reading a histogram bucket.

        theorem Cslib.Algorithms.WordRAM.Histogram.memory_outside (mem keys : ℕ → ℕ) (dst bins k a : ℕ) (ha : ¬(dst ≤ a ∧ a < dst + bins)) :
        memory mem keys dst bins k a = mem a

        Histogram writes preserve all other cells.

        theorem Cslib.Algorithms.WordRAM.Histogram.memory_succ (mem keys : ℕ → ℕ) (dst bins k : ℕ) (hk : keys k < bins) :
        memory mem keys dst bins (k + 1) = Function.update (memory mem keys dst bins k) (dst + keys k) (count keys k (keys k) + 1)

        One insertion is a single histogram-cell update.

        @[reducible, inline]

        Input base.

        Instances For
          @[reducible, inline]

          Histogram base.

          Instances For
            @[reducible, inline]

            Number of input keys.

            Instances For
              @[reducible, inline]

              Number of buckets.

              Instances For
                @[reducible, inline]

                Prefix length.

                Instances For
                  @[reducible, inline]

                  Temporary address.

                  Instances For
                    @[reducible, inline]

                    Current key.

                    Instances For
                      @[reducible, inline]

                      Current bucket count.

                      Instances For
                        @[reducible, inline]

                        Number of ordered collisions.

                        Instances For
                          @[reducible, inline]

                          Constant one.

                          Instances For
                            @[reducible, inline]

                            Constant zero.

                            Instances For

                              Clear one histogram cell and advance the cursor.

                              Instances For

                                Insert one key and add twice its old bucket count.

                                Instances For

                                  Clear the histogram, retaining the input and all four parameters.

                                  Instances For

                                    Insert all keys into a cleared histogram.

                                    Instances For

                                      Fixed code, with all bases and lengths supplied in registers.

                                      Instances For
                                        theorem Cslib.Algorithms.WordRAM.Histogram.exec {w : ℕ} (x : WordRAM.Data 11) (keys : ℕ → ℕ) (h1 : WordRAM.Fits w 1) (hs : WordRAM.Fits w (x.regs src + x.regs len)) (hd : WordRAM.Fits w (x.regs dst + x.regs bins)) (ht : WordRAM.Fits w (x.regs len * x.regs len + x.regs len + 1)) (hdisj : x.regs src + x.regs len ≤ x.regs dst) (hkeys : ∀ i < x.regs len, keys i < x.regs bins) (hinput : ∀ i < x.regs len, x.mem (x.regs src + i) = keys i) :
                                        ∃ (y : WordRAM.Data 11), WordRAM.ExecWithin w program x y (5 * x.regs bins + 11 * x.regs len + 9) ∧ y.mem = memory x.mem keys (x.regs dst) (x.regs bins) (x.regs len) ∧ y.regs total = pairs keys (x.regs len) ∧ ∀ (i : Fin 11), ↑i < 4 → y.regs i = x.regs i

                                        Build a histogram and count ordered collisions within 5M + 11n + 9 native instructions. The input keys lie in [0,M). The input precedes the output block. Both one-past-end addresses and the quadratic counter bound must fit. These are explicit local contracts; constructing the keys and deriving a logarithmic width are separate client obligations.