-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C++] Avoid copying statically generated serialized ATNs #3613
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. | ||
* Use of this file is governed by the BSD 3-clause license that | ||
* can be found in the LICENSE.txt file in the project root. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <cstring> | ||
#include <iterator> | ||
#include <vector> | ||
|
||
#include "antlr4-common.h" | ||
#include "misc/MurmurHash.h" | ||
|
||
namespace antlr4 { | ||
namespace atn { | ||
|
||
class ANTLR4CPP_PUBLIC SerializedATNView final { | ||
public: | ||
using value_type = int32_t; | ||
using size_type = size_t; | ||
using difference_type = ptrdiff_t; | ||
using reference = int32_t&; | ||
using const_reference = const int32_t&; | ||
using pointer = int32_t*; | ||
using const_pointer = const int32_t*; | ||
using iterator = const_pointer; | ||
using const_iterator = const_pointer; | ||
using reverse_iterator = std::reverse_iterator<iterator>; | ||
using const_reverse_iterator = std::reverse_iterator<const_iterator>; | ||
|
||
SerializedATNView() = default; | ||
|
||
SerializedATNView(const_pointer data, size_type size) : _data(data), _size(size) {} | ||
|
||
SerializedATNView(const std::vector<int32_t> &serializedATN) : _data(serializedATN.data()), _size(serializedATN.size()) {} | ||
|
||
SerializedATNView(const SerializedATNView&) = default; | ||
|
||
SerializedATNView& operator=(const SerializedATNView&) = default; | ||
|
||
const_iterator begin() const { return data(); } | ||
|
||
const_iterator cbegin() const { return data(); } | ||
|
||
const_iterator end() const { return data() + size(); } | ||
|
||
const_iterator cend() const { return data() + size(); } | ||
|
||
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } | ||
|
||
const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); } | ||
|
||
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } | ||
|
||
const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); } | ||
|
||
bool empty() const { return size() == 0; } | ||
|
||
const_pointer data() const { return _data; } | ||
|
||
size_type size() const { return _size; } | ||
|
||
size_type size_bytes() const { return size() * sizeof(value_type); } | ||
|
||
const_reference operator[](size_type index) const { return _data[index]; } | ||
|
||
private: | ||
const_pointer _data = nullptr; | ||
size_type _size = 0; | ||
}; | ||
|
||
inline bool operator==(const SerializedATNView &lhs, const SerializedATNView &rhs) { | ||
return (lhs.data() == rhs.data() && lhs.size() == rhs.size()) || | ||
(lhs.size() == rhs.size() && std::memcmp(lhs.data(), rhs.data(), lhs.size_bytes()) == 0); | ||
} | ||
|
||
inline bool operator!=(const SerializedATNView &lhs, const SerializedATNView &rhs) { | ||
return !operator==(lhs, rhs); | ||
} | ||
|
||
inline bool operator<(const SerializedATNView &lhs, const SerializedATNView &rhs) { | ||
int diff = std::memcmp(lhs.data(), rhs.data(), std::min(lhs.size_bytes(), rhs.size_bytes())); | ||
return diff < 0 || (diff == 0 && lhs.size() < rhs.size()); | ||
} | ||
|
||
} // namespace atn | ||
} // namespace antlr4 | ||
|
||
namespace std { | ||
|
||
template <> | ||
struct hash<::antlr4::atn::SerializedATNView> { | ||
size_t operator()(const ::antlr4::atn::SerializedATNView &serializedATNView) const { | ||
return ::antlr4::misc::MurmurHash::hashCode(serializedATNView.data(), serializedATNView.size()); | ||
} | ||
}; | ||
|
||
} // namespace std |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could even be
static constexpr
. Probably sizeof() below will be smart enough to already know this at compile-time even withconst
, but withconstexpr
it doesn't have an excuse not to know.