Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / Project / exc / Box.cpp
blob: 85f65ffcbbcb193cd42339ee317e835407fc8826 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Box.h"

Box::Box(const std::string& name) : name(name), items(), subboxes()
{}
Box::Box(const std::string& name, const std::deque<std::string>& items, const std::deque<Box*> subboxes) : name(name), items(items), subboxes(subboxes)
{}

std::string& Box::get_name() {
	return name;
}

void Box::pushItem(std::string item) {
	items.push_back(item);
}

void Box::pushSubbox(Box* subbox) {
	subboxes.push_back(subbox);
}

void Box::removeUselessSubBoxes() {
	for (auto i = subboxes.begin(), end = subboxes.end(); i != end; ++i) {
		(*i)->removeUselessSubBoxes();
		if ((*i)->items.empty() && (*i)->subboxes.size() < 2) {
			if ((*i)->subboxes.empty()) {
				subboxes.erase(i); // затова държа самия итератор
				continue;
			}
			*i = (*i)->subboxes[0];
		}
	}
}

std::ostream& Box::printWithInd(std::ostream& ostr, std::string indent) {
	ostr << indent << name << std::endl;
	for (std::string& i : items)
		ostr << indent << " + " << i << std::endl;

	indent += "    ";
	for (Box* sb : subboxes)
		sb->printWithInd(ostr, indent);
	return ostr;
}

std::ostream& Box::print(std::ostream& ostr) {
	return printWithInd(ostr, "");
}