-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.hpp
More file actions
44 lines (32 loc) · 1.09 KB
/
size.hpp
File metadata and controls
44 lines (32 loc) · 1.09 KB
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
#pragma once
#include "nodes.hpp"
namespace lambda {
inline std::size_t getSize(std::unique_ptr<LambdaBase>& node) noexcept;
namespace _size {
constexpr std::size_t getSizeVariable(
std::unique_ptr<LambdaBase>& node) noexcept {
return sizeof(*(std::unique_ptr<Variable>&)node);
}
inline std::size_t getSizeFunction(std::unique_ptr<LambdaBase>& node) noexcept {
return getSize(((std::unique_ptr<Function>&)node)->body) +
sizeof(*(std::unique_ptr<Function>&)node);
}
inline std::size_t getSizeApplication(
std::unique_ptr<LambdaBase>& node) noexcept {
return getSize(((std::unique_ptr<Application>&)node)->left) +
getSize(((std::unique_ptr<Application>&)node)->right);
}
} // namespace _size
inline std::size_t getSize(std::unique_ptr<LambdaBase>& node) noexcept {
if (!node) return 0;
switch (node->getType()) {
case LambdaType::VARIABLE:
return _size::getSizeVariable(node);
case LambdaType::FUNCTION:
return _size::getSizeFunction(node);
case LambdaType::APPLICATION:
return _size::getSizeApplication(node);
}
return 0;
}
} // namespace lambda