Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Conversation

ghost
Copy link

@ghost ghost commented May 5, 2020

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.

@googlebot googlebot added the cla: yes All contributors in pull request have signed the CLA with Google. label May 5, 2020
@ghost ghost requested a review from fangism May 5, 2020 12:39
class AutoPopStack {
public:
typedef T value_type;
typedef typename std::remove_pointer<value_type>::type basic_value_type;
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done & done

stack_ = stack;
stack->Push(std::move(value));
}
AutoPop(this_type* stack, basic_value_type& value) {
Copy link
Collaborator

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).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 64 to 68
// 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;
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 81 to 95
return const_cast<value_type&>(
static_cast<const this_type&>(*this).top());
}
Copy link
Collaborator

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.

Copy link
Author

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.

}

// Push a value_type onto the stack (takes reference)
void Push(const basic_value_type& value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const value_type&

Copy link
Author

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 Show resolved Hide resolved
@ghost ghost force-pushed the autopop branch from 723534d to b2fb535 Compare May 5, 2020 20:00
Copy link
Collaborator

@fangism fangism left a 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.

Comment on lines 114 to 121

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_;
Copy link
Collaborator

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.

Copy link
Author

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"
Copy link
Collaborator

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


namespace verible {

// AutoPop stack class. Templated version of SyntaxTreeContext::AutoPop
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

typedef AutoPopStack<value_type> this_type;

typedef value_type& reference_type;
typedef const value_type& const_reference_type;
Copy link
Collaborator

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.

Copy link
Author

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>
Copy link
Collaborator

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

Copy link
Author

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.

Copy link
Collaborator

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*> {
Copy link
Collaborator

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.)

Copy link
Author

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());
Copy link
Collaborator

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;

Copy link
Author

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.

Lukasz Dalek added 2 commits May 6, 2020 13:50
@ghost ghost force-pushed the autopop branch from b2fb535 to 4aa1261 Compare May 6, 2020 11:55
#ifndef VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_
#define VERIBLE_COMMON_UTIL_AUTO_POP_STACK_H_

#include <iterator>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.

@hzeller hzeller closed this in b55e90a May 6, 2020
hzeller pushed a commit that referenced this pull request May 6, 2020
issues #289

PiperOrigin-RevId: 310247369
nikhiljha pushed a commit to nikhiljha/verible that referenced this pull request Sep 27, 2022
nikhiljha pushed a commit to nikhiljha/verible that referenced this pull request Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes All contributors in pull request have signed the CLA with Google.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants