1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/compiler/base/depend_chain_helper.h"
17 
18 namespace panda::ecmascript::kungfu {
19 
Merge(DependChains* that)20 void DependChains::Merge(DependChains* that)
21 {
22     // find common sub list
23     while (size_ > that->size_) {
24         head_ = head_->next;
25         size_--;
26     }
27 
28     auto lhs = this->head_;
29     auto rhs = that->head_;
30     size_t rhsSize = that->size_;
31     while (rhsSize > size_) {
32         rhs = rhs->next;
33         rhsSize--;
34     }
35     while (lhs != rhs) {
36         ASSERT(lhs != nullptr);
37         lhs = lhs->next;
38         rhs = rhs->next;
39         size_--;
40     }
41     head_ = lhs;
42 }
43 
Equals(DependChains* that)44 bool DependChains::Equals(DependChains* that)
45 {
46     if (that == nullptr) {
47         return false;
48     }
49     if (size_ != that->size_) {
50         return false;
51     }
52     auto lhs = this->head_;
53     auto rhs = that->head_;
54     while (lhs != rhs) {
55         if (lhs->gate != rhs->gate) {
56             return false;
57         }
58         lhs = lhs->next;
59         rhs = rhs->next;
60     }
61     return true;
62 }
63 
UpdateNode(GateRef gate)64 DependChains* DependChains::UpdateNode(GateRef gate)
65 {
66     // assign node->next to head
67     Node* node = chunk_->New<Node>(gate, head_);
68     DependChains* that = new (chunk_) DependChains(chunk_);
69     // assign head to node
70     that->head_ = node;
71     that->size_ = size_ + 1;
72     return that;
73 }
74 } // namespace panda::ecmascript::kungfu