xref: /aosp_15_r20/external/libtextclassifier/native/utils/codepoint-range.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
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.
SortCodepointRanges(const std::vector<const CodepointRange * > & codepoint_ranges,std::vector<CodepointRangeStruct> * sorted_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::stable_sort(
35       sorted_codepoint_ranges->begin(), sorted_codepoint_ranges->end(),
36       [](const CodepointRangeStruct& a, const CodepointRangeStruct& b) {
37         return a.start < b.start;
38       });
39 }
40 
41 // Returns true if given codepoint is covered by the given sorted vector of
42 // codepoint ranges.
IsCodepointInRanges(int codepoint,const std::vector<CodepointRangeStruct> & codepoint_ranges)43 bool IsCodepointInRanges(
44     int codepoint, const std::vector<CodepointRangeStruct>& codepoint_ranges) {
45   auto it = std::lower_bound(
46       codepoint_ranges.begin(), codepoint_ranges.end(), codepoint,
47       [](const CodepointRangeStruct& range, int codepoint) {
48         // This function compares range with the
49         // codepoint for the purpose of finding the first
50         // greater or equal range. Because of the use of
51         // std::lower_bound it needs to return true when
52         // range < codepoint; the first time it will
53         // return false the lower bound is found and
54         // returned.
55         //
56         // It might seem weird that the condition is
57         // range.end <= codepoint here but when codepoint
58         // == range.end it means it's actually just
59         // outside of the range, thus the range is less
60         // than the codepoint.
61         return range.end <= codepoint;
62       });
63   if (it != codepoint_ranges.end() && it->start <= codepoint &&
64       it->end > codepoint) {
65     return true;
66   } else {
67     return false;
68   }
69 }
70 
71 }  // namespace libtextclassifier3
72