Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "utils/codepoint-range.h"
     18 
     19 #include <algorithm>
     20 
     21 namespace libtextclassifier3 {
     22 
     23 // Returns a sorted list of the codepoint ranges.
     24 void SortCodepointRanges(
     25     const std::vector<const CodepointRange*>& codepoint_ranges,
     26     std::vector<CodepointRangeStruct>* sorted_codepoint_ranges) {
     27   sorted_codepoint_ranges->clear();
     28   sorted_codepoint_ranges->reserve(codepoint_ranges.size());
     29   for (const CodepointRange* range : codepoint_ranges) {
     30     sorted_codepoint_ranges->push_back(
     31         CodepointRangeStruct(range->start(), range->end()));
     32   }
     33 
     34   std::sort(sorted_codepoint_ranges->begin(), sorted_codepoint_ranges->end(),
     35             [](const CodepointRangeStruct& a, const CodepointRangeStruct& b) {
     36               return a.start < b.start;
     37             });
     38 }
     39 
     40 // Returns true if given codepoint is covered by the given sorted vector of
     41 // codepoint ranges.
     42 bool IsCodepointInRanges(
     43     int codepoint, const std::vector<CodepointRangeStruct>& codepoint_ranges) {
     44   auto it = std::lower_bound(
     45       codepoint_ranges.begin(), codepoint_ranges.end(), codepoint,
     46       [](const CodepointRangeStruct& range, int codepoint) {
     47         // This function compares range with the
     48         // codepoint for the purpose of finding the first
     49         // greater or equal range. Because of the use of
     50         // std::lower_bound it needs to return true when
     51         // range < codepoint; the first time it will
     52         // return false the lower bound is found and
     53         // returned.
     54         //
     55         // It might seem weird that the condition is
     56         // range.end <= codepoint here but when codepoint
     57         // == range.end it means it's actually just
     58         // outside of the range, thus the range is less
     59         // than the codepoint.
     60         return range.end <= codepoint;
     61       });
     62   if (it != codepoint_ranges.end() && it->start <= codepoint &&
     63       it->end > codepoint) {
     64     return true;
     65   } else {
     66     return false;
     67   }
     68 }
     69 
     70 }  // namespace libtextclassifier3
     71