-
Notifications
You must be signed in to change notification settings - Fork 215
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
Add generic AutoPopStack class #289
Conversation
common/util/auto_pop_stack.h
Outdated
class AutoPopStack { | ||
public: | ||
typedef T value_type; | ||
typedef typename std::remove_pointer<value_type>::type basic_value_type; |
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.
Let's keep the interface so that this class can work with pointers and non-pointers alike. In other words, let function parameters and parameters use value_type
and T&
for reference_type
, const T&
for const_reference_type
(the same way most STL containers are designed).
So no auto-deferencing/address-taking at the interface.
This means that the user of this class will need to pass pointers instead of references.
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.
Done & done
common/util/auto_pop_stack.h
Outdated
stack_ = stack; | ||
stack->Push(std::move(value)); | ||
} | ||
AutoPop(this_type* stack, basic_value_type& value) { |
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.
Related to above comment, this should support const value_type&
(copy).
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.
Done
common/util/auto_pop_stack.h
Outdated
typedef typename std::remove_pointer<value_type>::type basic_value_type; | ||
typedef AutoPopStack<value_type> this_type; | ||
|
||
// member class to handle push and pop of stack safely |
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.
Also emphasize that this is the only public interface that modifies the number of elements in the stack.
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.
Done
common/util/auto_pop_stack.h
Outdated
// All elements of stack are non-null because they refer to traversed nodes. | ||
typedef std::vector<value_type> stack_type; | ||
typedef typename stack_type::iterator iterator; | ||
typedef typename stack_type::const_iterator const_iterator; | ||
typedef typename stack_type::const_reverse_iterator const_reverse_iterator; |
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.
Move these typedefs near top of class.
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.
Done
common/util/auto_pop_stack.h
Outdated
return const_cast<value_type&>( | ||
static_cast<const this_type&>(*this).top()); | ||
} |
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.
No need for casting, just return stack_.back();
. This should internally look identical to the const
method counterpart.
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.
Just wanted to reuse const
top()
method. Fixed.
common/util/auto_pop_stack.h
Outdated
} | ||
|
||
// Push a value_type onto the stack (takes reference) | ||
void Push(const basic_value_type& value) { |
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.
const value_type&
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.
Done
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.
Only minor things remain. Looks good.
common/text/syntax_tree_context.h
Outdated
|
||
protected: | ||
// Pop the top SyntaxTreeNode off of the stack | ||
void Pop(); | ||
|
||
// Push a SyntaxTreeNode onto the stack (takes address) | ||
void Push(const SyntaxTreeNode& node); | ||
|
||
// Stack of ancestors of the current node that is updated as the tree | ||
// is traversed. Top of the stack is closest ancestor. | ||
// A vector is chosen to allow random access and searches from either end of | ||
// the stack. | ||
stack_type stack_; | ||
// private: | ||
// base_type::stack_type base_type::stack_; |
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.
Can delete this section now.
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.
Done
#include <iterator> | ||
#include <type_traits> | ||
#include <vector> | ||
#include "common/util/iterator_adaptors.h" |
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.
minor nit: blank line above here to separate groups of headers
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.
Done
common/util/auto_pop_stack.h
Outdated
|
||
namespace verible { | ||
|
||
// AutoPop stack class. Templated version of SyntaxTreeContext::AutoPop |
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.
Update comment to not mention SyntaxTreeContext::AutoPop (reference is cyclic).
Suggest describing use cases of this class, and a motivating example.
Also worth mentioning that this provides public read-only random access/iteration into the stack elements.
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.
Done
common/util/auto_pop_stack.h
Outdated
typedef AutoPopStack<value_type> this_type; | ||
|
||
typedef value_type& reference_type; | ||
typedef const value_type& const_reference_type; |
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.
Let's follow STL convention and call this const_reference
.
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.
Also fixed reference
#ifndef VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_ | ||
#define VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_ | ||
|
||
#include <iterator> |
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.
add (for size_t
):
#include
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.
Added cstddef
. Hope it's that what you meant.
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.
yes.
@@ -31,38 +33,16 @@ namespace verible { | |||
// Note: Public methods are named to follow STL convention for std::stack. | |||
// TODO(fangism): implement a general ForwardMatcher and ReverseMatcher | |||
// interface that can express AND/OR/NOT. | |||
class SyntaxTreeContext { | |||
class SyntaxTreeContext : public AutoPopStack<const SyntaxTreeNode*> { |
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.
Document that one of the invariants is that elements of the (internal) stack are restricted to non-nullptrs. (This was the case before refactoring.)
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.
Done
const_reverse_iterator rbegin() const { return stack_.rbegin(); } | ||
const_reverse_iterator rend() const { return stack_.rend(); } | ||
const SyntaxTreeNode& top() const { | ||
return *ABSL_DIE_IF_NULL(base_type::top()); |
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.
You can hide the base class's interface with:
protected:
using base_type::top;
This makes it clear that you are further restricting direct uses of the base class methods intentionally, and here is where you can alter the interface to return references if you wish.
Optionally, you can also be explicit about what base class interfaces you wish to expose:
public:
using base_type::begin;
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.
Just restricted access to base_type::top
. Also removed base_type::
from methods like rbegin
, rend
and so on.
Signed-off-by: Lukasz Dalek <[email protected]>
Signed-off-by: Lukasz Dalek <[email protected]>
#ifndef VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_ | ||
#define VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_ | ||
|
||
#include <iterator> |
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.
yes.
issues #289 PiperOrigin-RevId: 310247369
Generic autopop stack class is intended to be used in code base which uses stack-based algorithms like symbol or node tracking, e.g.
SyntaxTreeContext
.