...

Source file src/github.com/klauspost/compress/flate/huffman_sortByLiteral.go

Documentation: github.com/klauspost/compress/flate

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package flate
     6  
     7  // Sort sorts data.
     8  // It makes one call to data.Len to determine n, and O(n*log(n)) calls to
     9  // data.Less and data.Swap. The sort is not guaranteed to be stable.
    10  func sortByLiteral(data []literalNode) {
    11  	n := len(data)
    12  	quickSort(data, 0, n, maxDepth(n))
    13  }
    14  
    15  func quickSort(data []literalNode, a, b, maxDepth int) {
    16  	for b-a > 12 { // Use ShellSort for slices <= 12 elements
    17  		if maxDepth == 0 {
    18  			heapSort(data, a, b)
    19  			return
    20  		}
    21  		maxDepth--
    22  		mlo, mhi := doPivot(data, a, b)
    23  		// Avoiding recursion on the larger subproblem guarantees
    24  		// a stack depth of at most lg(b-a).
    25  		if mlo-a < b-mhi {
    26  			quickSort(data, a, mlo, maxDepth)
    27  			a = mhi // i.e., quickSort(data, mhi, b)
    28  		} else {
    29  			quickSort(data, mhi, b, maxDepth)
    30  			b = mlo // i.e., quickSort(data, a, mlo)
    31  		}
    32  	}
    33  	if b-a > 1 {
    34  		// Do ShellSort pass with gap 6
    35  		// It could be written in this simplified form cause b-a <= 12
    36  		for i := a + 6; i < b; i++ {
    37  			if data[i].literal < data[i-6].literal {
    38  				data[i], data[i-6] = data[i-6], data[i]
    39  			}
    40  		}
    41  		insertionSort(data, a, b)
    42  	}
    43  }
    44  func heapSort(data []literalNode, a, b int) {
    45  	first := a
    46  	lo := 0
    47  	hi := b - a
    48  
    49  	// Build heap with greatest element at top.
    50  	for i := (hi - 1) / 2; i >= 0; i-- {
    51  		siftDown(data, i, hi, first)
    52  	}
    53  
    54  	// Pop elements, largest first, into end of data.
    55  	for i := hi - 1; i >= 0; i-- {
    56  		data[first], data[first+i] = data[first+i], data[first]
    57  		siftDown(data, lo, i, first)
    58  	}
    59  }
    60  
    61  // siftDown implements the heap property on data[lo, hi).
    62  // first is an offset into the array where the root of the heap lies.
    63  func siftDown(data []literalNode, lo, hi, first int) {
    64  	root := lo
    65  	for {
    66  		child := 2*root + 1
    67  		if child >= hi {
    68  			break
    69  		}
    70  		if child+1 < hi && data[first+child].literal < data[first+child+1].literal {
    71  			child++
    72  		}
    73  		if data[first+root].literal > data[first+child].literal {
    74  			return
    75  		}
    76  		data[first+root], data[first+child] = data[first+child], data[first+root]
    77  		root = child
    78  	}
    79  }
    80  func doPivot(data []literalNode, lo, hi int) (midlo, midhi int) {
    81  	m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow.
    82  	if hi-lo > 40 {
    83  		// Tukey's ``Ninther,'' median of three medians of three.
    84  		s := (hi - lo) / 8
    85  		medianOfThree(data, lo, lo+s, lo+2*s)
    86  		medianOfThree(data, m, m-s, m+s)
    87  		medianOfThree(data, hi-1, hi-1-s, hi-1-2*s)
    88  	}
    89  	medianOfThree(data, lo, m, hi-1)
    90  
    91  	// Invariants are:
    92  	//	data[lo] = pivot (set up by ChoosePivot)
    93  	//	data[lo < i < a] < pivot
    94  	//	data[a <= i < b] <= pivot
    95  	//	data[b <= i < c] unexamined
    96  	//	data[c <= i < hi-1] > pivot
    97  	//	data[hi-1] >= pivot
    98  	pivot := lo
    99  	a, c := lo+1, hi-1
   100  
   101  	for ; a < c && data[a].literal < data[pivot].literal; a++ {
   102  	}
   103  	b := a
   104  	for {
   105  		for ; b < c && data[pivot].literal > data[b].literal; b++ { // data[b] <= pivot
   106  		}
   107  		for ; b < c && data[pivot].literal < data[c-1].literal; c-- { // data[c-1] > pivot
   108  		}
   109  		if b >= c {
   110  			break
   111  		}
   112  		// data[b] > pivot; data[c-1] <= pivot
   113  		data[b], data[c-1] = data[c-1], data[b]
   114  		b++
   115  		c--
   116  	}
   117  	// If hi-c<3 then there are duplicates (by property of median of nine).
   118  	// Let's be a bit more conservative, and set border to 5.
   119  	protect := hi-c < 5
   120  	if !protect && hi-c < (hi-lo)/4 {
   121  		// Lets test some points for equality to pivot
   122  		dups := 0
   123  		if data[pivot].literal > data[hi-1].literal { // data[hi-1] = pivot
   124  			data[c], data[hi-1] = data[hi-1], data[c]
   125  			c++
   126  			dups++
   127  		}
   128  		if data[b-1].literal > data[pivot].literal { // data[b-1] = pivot
   129  			b--
   130  			dups++
   131  		}
   132  		// m-lo = (hi-lo)/2 > 6
   133  		// b-lo > (hi-lo)*3/4-1 > 8
   134  		// ==> m < b ==> data[m] <= pivot
   135  		if data[m].literal > data[pivot].literal { // data[m] = pivot
   136  			data[m], data[b-1] = data[b-1], data[m]
   137  			b--
   138  			dups++
   139  		}
   140  		// if at least 2 points are equal to pivot, assume skewed distribution
   141  		protect = dups > 1
   142  	}
   143  	if protect {
   144  		// Protect against a lot of duplicates
   145  		// Add invariant:
   146  		//	data[a <= i < b] unexamined
   147  		//	data[b <= i < c] = pivot
   148  		for {
   149  			for ; a < b && data[b-1].literal > data[pivot].literal; b-- { // data[b] == pivot
   150  			}
   151  			for ; a < b && data[a].literal < data[pivot].literal; a++ { // data[a] < pivot
   152  			}
   153  			if a >= b {
   154  				break
   155  			}
   156  			// data[a] == pivot; data[b-1] < pivot
   157  			data[a], data[b-1] = data[b-1], data[a]
   158  			a++
   159  			b--
   160  		}
   161  	}
   162  	// Swap pivot into middle
   163  	data[pivot], data[b-1] = data[b-1], data[pivot]
   164  	return b - 1, c
   165  }
   166  
   167  // Insertion sort
   168  func insertionSort(data []literalNode, a, b int) {
   169  	for i := a + 1; i < b; i++ {
   170  		for j := i; j > a && data[j].literal < data[j-1].literal; j-- {
   171  			data[j], data[j-1] = data[j-1], data[j]
   172  		}
   173  	}
   174  }
   175  
   176  // maxDepth returns a threshold at which quicksort should switch
   177  // to heapsort. It returns 2*ceil(lg(n+1)).
   178  func maxDepth(n int) int {
   179  	var depth int
   180  	for i := n; i > 0; i >>= 1 {
   181  		depth++
   182  	}
   183  	return depth * 2
   184  }
   185  
   186  // medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1].
   187  func medianOfThree(data []literalNode, m1, m0, m2 int) {
   188  	// sort 3 elements
   189  	if data[m1].literal < data[m0].literal {
   190  		data[m1], data[m0] = data[m0], data[m1]
   191  	}
   192  	// data[m0] <= data[m1]
   193  	if data[m2].literal < data[m1].literal {
   194  		data[m2], data[m1] = data[m1], data[m2]
   195  		// data[m0] <= data[m2] && data[m1] < data[m2]
   196  		if data[m1].literal < data[m0].literal {
   197  			data[m1], data[m0] = data[m0], data[m1]
   198  		}
   199  	}
   200  	// now data[m0] <= data[m1] <= data[m2]
   201  }
   202  

View as plain text