tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Sort.h (3949B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef ds_Sort_h
      8 #define ds_Sort_h
      9 
     10 #include "jstypes.h"
     11 
     12 namespace js {
     13 
     14 namespace detail {
     15 
     16 template <typename T>
     17 MOZ_ALWAYS_INLINE void CopyNonEmptyArray(T* dst, const T* src, size_t nelems) {
     18  MOZ_ASSERT(nelems != 0);
     19  const T* end = src + nelems;
     20  do {
     21    *dst++ = *src++;
     22  } while (src != end);
     23 }
     24 
     25 /* Helper function for MergeSort. */
     26 template <typename T, typename Comparator>
     27 MOZ_ALWAYS_INLINE bool MergeArrayRuns(T* dst, const T* src, size_t run1,
     28                                      size_t run2, Comparator c) {
     29  MOZ_ASSERT(run1 >= 1);
     30  MOZ_ASSERT(run2 >= 1);
     31 
     32  /* Copy runs already in sorted order. */
     33  const T* b = src + run1;
     34  bool lessOrEqual;
     35  if (!c(b[-1], b[0], &lessOrEqual)) {
     36    return false;
     37  }
     38 
     39  if (!lessOrEqual) {
     40    /* Runs are not already sorted, merge them. */
     41    for (const T* a = src;;) {
     42      if (!c(*a, *b, &lessOrEqual)) {
     43        return false;
     44      }
     45      if (lessOrEqual) {
     46        *dst++ = *a++;
     47        if (!--run1) {
     48          src = b;
     49          break;
     50        }
     51      } else {
     52        *dst++ = *b++;
     53        if (!--run2) {
     54          src = a;
     55          break;
     56        }
     57      }
     58    }
     59  }
     60  CopyNonEmptyArray(dst, src, run1 + run2);
     61  return true;
     62 }
     63 
     64 } /* namespace detail */
     65 
     66 /*
     67 * Sort the array using the merge sort algorithm. The scratch should point to
     68 * a temporary storage that can hold nelems elements.
     69 *
     70 * The comparator must provide the () operator with the following signature:
     71 *
     72 *     bool operator()(const T& a, const T& a, bool* lessOrEqualp);
     73 *
     74 * It should return true on success and set *lessOrEqualp to the result of
     75 * a <= b operation. If it returns false, the sort terminates immediately with
     76 * the false result. In this case the content of the array and scratch is
     77 * arbitrary.
     78 *
     79 * Note: The merge sort algorithm is a stable sort, preserving relative ordering
     80 * of entries that compare equal. This makes it a useful substitute for
     81 * |std::stable_sort|, which can't be used in SpiderMonkey because it internally
     82 * allocates memory without using SpiderMonkey's allocator.
     83 */
     84 template <typename T, typename Comparator>
     85 [[nodiscard]] bool MergeSort(T* array, size_t nelems, T* scratch,
     86                             Comparator c) {
     87  const size_t INS_SORT_LIMIT = 3;
     88 
     89  if (nelems <= 1) {
     90    return true;
     91  }
     92 
     93  /*
     94   * Apply insertion sort to small chunks to reduce the number of merge
     95   * passes needed.
     96   */
     97  for (size_t lo = 0; lo < nelems; lo += INS_SORT_LIMIT) {
     98    size_t hi = lo + INS_SORT_LIMIT;
     99    if (hi >= nelems) {
    100      hi = nelems;
    101    }
    102    for (size_t i = lo + 1; i != hi; i++) {
    103      for (size_t j = i;;) {
    104        bool lessOrEqual;
    105        if (!c(array[j - 1], array[j], &lessOrEqual)) {
    106          return false;
    107        }
    108        if (lessOrEqual) {
    109          break;
    110        }
    111        T tmp = array[j - 1];
    112        array[j - 1] = array[j];
    113        array[j] = tmp;
    114        if (--j == lo) {
    115          break;
    116        }
    117      }
    118    }
    119  }
    120 
    121  T* vec1 = array;
    122  T* vec2 = scratch;
    123  for (size_t run = INS_SORT_LIMIT; run < nelems; run *= 2) {
    124    for (size_t lo = 0; lo < nelems; lo += 2 * run) {
    125      size_t hi = lo + run;
    126      if (hi >= nelems) {
    127        detail::CopyNonEmptyArray(vec2 + lo, vec1 + lo, nelems - lo);
    128        break;
    129      }
    130      size_t run2 = (run <= nelems - hi) ? run : nelems - hi;
    131      if (!detail::MergeArrayRuns(vec2 + lo, vec1 + lo, run, run2, c)) {
    132        return false;
    133      }
    134    }
    135    T* swap = vec1;
    136    vec1 = vec2;
    137    vec2 = swap;
    138  }
    139  if (vec1 == scratch) {
    140    detail::CopyNonEmptyArray(array, scratch, nelems);
    141  }
    142  return true;
    143 }
    144 
    145 } /* namespace js */
    146 
    147 #endif /* ds_Sort_h */