Skip to content

Commit

Permalink
Merge pull request #28 from aesophor/divide-by-zero-fix
Browse files Browse the repository at this point in the history
Fix issue #24: fix divide by zero in Workspace::DfsTileHelper()
  • Loading branch information
aesophor authored Nov 28, 2019
2 parents ce2d518 + 0895c1b commit 150cf6f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
53 changes: 34 additions & 19 deletions src/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,7 @@ Tree::Node* Tree::GetTreeNode(Client* client) const {
}

vector<Tree::Node*> Tree::GetLeaves() const {
vector<Tree::Node*> leaves;
stack<Tree::Node*> st;
st.push(root_node_.get());

while (!st.empty()) {
Tree::Node* node = st.top();
st.pop();

// If this node is a leaf, add it to the leaf vector.
if (node->children().empty()) {
leaves.push_back(node);
}

// Push all children onto the stack in reverse order (if any).
for (int i = node->children().size() - 1; i >= 0; i--) {
st.push(node->children().at(i));
}
}
return leaves;
return root_node_->GetLeaves();
}

Tree::Node* Tree::root_node() const {
Expand Down Expand Up @@ -243,6 +225,39 @@ Tree::Node* Tree::Node::GetRightSibling() const {
}
}

vector<Tree::Node*> Tree::Node::GetLeaves() {
vector<Tree::Node*> leaves;
stack<Tree::Node*> st;
st.push(this);

while (!st.empty()) {
Tree::Node* node = st.top();
st.pop();

// If this node is a leaf, add it to the leaf vector.
if (node->children().empty()) {
leaves.push_back(node);
}

// Push all children onto the stack in reverse order (if any).
for (int i = node->children().size() - 1; i >= 0; i--) {
st.push(node->children().at(i));
}
}
return leaves;
}

bool Tree::Node::HasTilingClientsInSubtree() {
// Get all leaves from this node's subtree,
// and check if it has any tiling client.
for (auto leaf : this->GetLeaves()) {
if (!leaf->client()->is_floating()) {
return true;
}
}
return false;
}

vector<Tree::Node*> Tree::Node::children() const {
vector<Tree::Node*> children(children_.size());
for (size_t i = 0; i < children.size(); i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Tree {
Tree::Node* GetLeftSibling() const;
Tree::Node* GetRightSibling() const;

std::vector<Tree::Node*> GetLeaves();
bool HasTilingClientsInSubtree();

std::vector<Tree::Node*> children() const;
Tree::Node* parent() const;
Client* client() const;
Expand Down
23 changes: 18 additions & 5 deletions src/workspace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ void Workspace::DfsTileHelper(Tree::Node* node, int x, int y, int w, int h, int
int gap_width) const {
vector<Tree::Node*> children = node->children();

// We don't care about floating windows. Remove them.
children.erase(
std::remove_if(children.begin(), children.end(),
[](Tree::Node* n) { return n->client() && n->client()->is_floating(); }),
children.end());
// We don't care about two kinds of `Tree::Node`s
// 1. an internal node with no tiling clients in its subtree
// 2. a leaf but it has a floating client
// Tree::Node::HasTilingClientsInSubtree() can check 1 and 2 at the same time.
children.erase(std::remove_if(children.begin(), children.end(),
[](Tree::Node* n) { return !n->HasTilingClientsInSubtree(); }),
children.end());

if (children.empty()) {
return;
}

// Calculate each child's x, y, width and height based on node's tiling
// direction.
Expand Down Expand Up @@ -177,6 +183,13 @@ void Workspace::SetTilingDirection(TilingDirection tiling_direction) {

Tree::Node* current_node = client_tree_.current_node();

// If current node has no siblings, we can simply set the new
// tiling direction on its parent and return.
if (current_node->parent()->children().size() == 1) {
current_node->parent()->set_tiling_direction(tiling_direction);
return;
}

unique_ptr<Client> client(current_node->release_client());
unique_ptr<Tree::Node> new_node = std::make_unique<Tree::Node>(std::move(client));

Expand Down

0 comments on commit 150cf6f

Please sign in to comment.