Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / cldnn_engine / simple_math.h
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6 #include <set>
7 #include <map>
8 #include <vector>
9 #include <functional>
10 #include <string>
11 #include <utility>
12
13 /*
14 *   Simple integer arithmetics to be used for the work sizes calculation
15 *   Supported ops: +,-,*,/,%,(,)
16 *   * no unary -,+
17 *   Variables defined as single chars and should not include one of the ops, whitespaces or 0-9
18 */
19
20
21 class SimpleMathExpression {
22 public:
23     SimpleMathExpression() :m_parsed(false) {}
24     void SetVariables(const std::map<char, int>& vars);
25     bool SetExpression(const std::string& expression);
26     bool IsParsed()const { return m_parsed; }
27     int Evaluate()const;  // undefined behavior if not parsed properly
28
29 private:
30     std::map<char, int> m_variables;
31     std::string m_expression;
32     bool m_parsed;
33     bool Parse();
34
35     struct Token {
36         enum TokenType {
37             Value,
38             Operator,
39         } type;
40         int value;
41         char op;
42         explicit Token(TokenType t = Value, int v = 0, char o = 0) :type(t), value(v), op(o) {}
43     };
44     std::vector<Token> m_parsedTokens;
45
46     static const std::set<char> whitespaces;
47     using Operator = std::pair<int, std::function<int(int, int)>>;  // priority, function
48     static const std::map<char, Operator> operators;
49 };