parser/html/jArray.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/jArray.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008-2010 Mozilla Foundation
     1.6 + *
     1.7 + * Permission is hereby granted, free of charge, to any person obtaining a
     1.8 + * copy of this software and associated documentation files (the "Software"),
     1.9 + * to deal in the Software without restriction, including without limitation
    1.10 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    1.11 + * and/or sell copies of the Software, and to permit persons to whom the
    1.12 + * Software is furnished to do so, subject to the following conditions:
    1.13 + *
    1.14 + * The above copyright notice and this permission notice shall be included in
    1.15 + * all copies or substantial portions of the Software.
    1.16 + *
    1.17 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.18 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.19 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    1.20 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.21 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    1.22 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    1.23 + * DEALINGS IN THE SOFTWARE.
    1.24 + */
    1.25 +
    1.26 +#ifndef jArray_h
    1.27 +#define jArray_h
    1.28 +
    1.29 +#include "mozilla/NullPtr.h"
    1.30 +#include "nsDebug.h"
    1.31 +
    1.32 +template<class T, class L>
    1.33 +struct staticJArray {
    1.34 +  const T* arr;
    1.35 +  const L length;
    1.36 +  operator T*() { return arr; }
    1.37 +  T& operator[] (L const index) { return ((T*)arr)[index]; }
    1.38 +  L binarySearch(T const elem) {
    1.39 +    L lo = 0;
    1.40 +    L hi = length - 1;
    1.41 +    while (lo <= hi) {
    1.42 +      L mid = (lo + hi) / 2;
    1.43 +      if (arr[mid] > elem) {
    1.44 +        hi = mid - 1;
    1.45 +      } else if (arr[mid] < elem) {
    1.46 +        lo = mid + 1;
    1.47 +      } else {
    1.48 +        return mid;
    1.49 +      }
    1.50 +    }
    1.51 +    return -1;
    1.52 +  }
    1.53 +};
    1.54 +
    1.55 +template<class T, class L>
    1.56 +struct jArray {
    1.57 +  T* arr;
    1.58 +  L length;
    1.59 +  static jArray<T,L> newJArray(L const len) {
    1.60 +    NS_ASSERTION(len >= 0, "Bad length.");
    1.61 +    jArray<T,L> newArray = { new T[len], len };
    1.62 +    return newArray;
    1.63 +  }
    1.64 +  operator T*() { return arr; }
    1.65 +  T& operator[] (L const index) { return arr[index]; }
    1.66 +  void operator=(staticJArray<T,L>& other) {
    1.67 +    arr = (T*)other.arr;
    1.68 +    length = other.length;
    1.69 +  }
    1.70 +};
    1.71 +
    1.72 +template<class T, class L>
    1.73 +class autoJArray {
    1.74 +  private:
    1.75 +    T* arr;
    1.76 +  public:
    1.77 +    L length;
    1.78 +    autoJArray()
    1.79 +     : arr(0)
    1.80 +     , length(0)
    1.81 +    {
    1.82 +    }
    1.83 +    autoJArray(const jArray<T,L>& other)
    1.84 +     : arr(other.arr)
    1.85 +     , length(other.length)
    1.86 +    {
    1.87 +    }
    1.88 +    ~autoJArray()
    1.89 +    {
    1.90 +      delete[] arr;
    1.91 +    }
    1.92 +    operator T*() { return arr; }
    1.93 +    T& operator[] (L const index) { return arr[index]; }
    1.94 +    operator jArray<T,L>() {
    1.95 +      // WARNING! This makes it possible to goof with buffer ownership!
    1.96 +      // This is needed for the getStack and getListOfActiveFormattingElements
    1.97 +      // methods to work sensibly.
    1.98 +      jArray<T,L> newArray = { arr, length };
    1.99 +      return newArray;
   1.100 +    }
   1.101 +    void operator=(const jArray<T,L>& other) {
   1.102 +      delete[] arr;
   1.103 +      arr = other.arr;
   1.104 +      length = other.length;
   1.105 +    }
   1.106 +    void operator=(mozilla::NullptrT n) {
   1.107 +      // Make assigning null to an array in Java delete the buffer in C++
   1.108 +      MOZ_ASSERT(n == nullptr);
   1.109 +      delete[] arr;
   1.110 +      arr = nullptr;
   1.111 +      length = 0;
   1.112 +    }
   1.113 +};
   1.114 +
   1.115 +#endif // jArray_h

mercurial