]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/support/Arrays.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / support / Arrays.h
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #pragma once
7
8 #include "antlr4-common.h"
9
10 namespace antlrcpp {
11
12   class ANTLR4CPP_PUBLIC Arrays {
13   public:
14
15     static std::string listToString(const std::vector<std::string> &list, const std::string &separator);
16
17     template <typename T>
18     static bool equals(const std::vector<T> &a, const std::vector<T> &b) {
19       if (a.size() != b.size())
20         return false;
21
22       for (size_t i = 0; i < a.size(); ++i)
23         if (!(a[i] == b[i]))
24           return false;
25
26       return true;
27     }
28
29     template <typename T>
30     static bool equals(const std::vector<T *> &a, const std::vector<T *> &b) {
31       if (a.size() != b.size())
32         return false;
33
34       for (size_t i = 0; i < a.size(); ++i) {
35         if (a[i] == b[i])
36           continue;
37         if (!(*a[i] == *b[i]))
38           return false;
39       }
40
41       return true;
42     }
43
44     template <typename T>
45     static bool equals(const std::vector<Ref<T>> &a, const std::vector<Ref<T>> &b) {
46       if (a.size() != b.size())
47         return false;
48
49       for (size_t i = 0; i < a.size(); ++i) {
50         if (!a[i] && !b[i])
51           continue;
52         if (!a[i] || !b[i])
53           return false;
54         if (a[i] == b[i])
55           continue;
56
57         if (!(*a[i] == *b[i]))
58           return false;
59       }
60
61       return true;
62     }
63
64     template <typename T>
65     static std::string toString(const std::vector<T> &source) {
66       std::string result = "[";
67       bool firstEntry = true;
68       for (auto &value : source) {
69         result += value.toString();
70         if (firstEntry) {
71           result += ", ";
72           firstEntry = false;
73         }
74       }
75       return result + "]";
76     }
77
78     template <typename T>
79     static std::string toString(const std::vector<Ref<T>> &source) {
80       std::string result = "[";
81       bool firstEntry = true;
82       for (auto &value : source) {
83         result += value->toString();
84         if (firstEntry) {
85           result += ", ";
86           firstEntry = false;
87         }
88       }
89       return result + "]";
90     }
91
92     template <typename T>
93     static std::string toString(const std::vector<T *> &source) {
94       std::string result = "[";
95       bool firstEntry = true;
96       for (auto value : source) {
97         result += value->toString();
98         if (firstEntry) {
99           result += ", ";
100           firstEntry = false;
101         }
102       }
103       return result + "]";
104     }
105
106   };
107
108   template <>
109   std::string Arrays::toString(const std::vector<antlr4::tree::ParseTree *> &source);
110 }