Essentia  2.1-beta6-dev
asciidag.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_UTILS_ASCIIDAG_H
21 #define ESSENTIA_UTILS_ASCIIDAG_H
22 
23 #include <vector>
24 #include <string>
25 #include "types.h"
26 
27 namespace essentia {
28 
32 class Position {
33  public:
34  int x, y;
35 
36  Position(int x_, int y_) : x(x_), y(y_) {}
37 
38  Position operator+(const Position& other) const {
39  return Position(x+other.x, y+other.y);
40  }
41 
42  bool operator==(const Position& other) const {
43  return x == other.x && y == other.y;
44  }
45 };
46 
47 inline std::ostream& operator<<(std::ostream& out, const Position& pos) {
48  return out << '<' << pos.x << ',' << pos.y << '>';
49 }
50 
53 
54 
55 
61 std::vector<std::string> makeRectangle(const std::string& network);
62 
68 std::vector<std::string> makeRectangle(const char* const* network, int size);
69 
70 
76 class AsciiCanvas : public std::vector<std::string> {
77  public:
78  // NB: template is only used so that ARRAY_SIZE can work, we only want const char*[] here
79  template <typename NetworkType>
80  explicit AsciiCanvas(const NetworkType& network) {
81  (*this) = makeRectangle(network, ARRAY_SIZE(network));
82  }
83 
84  AsciiCanvas& operator=(const std::vector<std::string>& other);
85 
86  int height() const { return (int)size(); }
87  int width() const { return (height() == 0) ? 0 : at(0).size(); }
88 
93  void addBorder();
94 
98  void fill(char c);
99 
100  const std::string& at(int i) const { return std::vector<std::string>::at(i); }
101  std::string& at(int i) { return std::vector<std::string>::at(i); }
102 
103  char at(const Position& pos) const { return (*this)[pos.y][pos.x]; }
104  char& at(const Position& pos) { return (*this)[pos.y][pos.x]; }
105 };
106 
107 inline std::ostream& operator<<(std::ostream& out, const AsciiCanvas& canvas) {
108  out << '\n';
109  for (int i=0; i<canvas.height(); i++) out << canvas.at(i) << '\n';
110  return out;
111 }
112 
113 
121 class AsciiBox {
122  public:
123  int posX, posY; // position of the top-left corner
124  int width, height; // size of the box
125  std::string title; // title of the box (main name contained in it)
126 
131  AsciiBox(const std::vector<std::string>& network, int x, int y);
132 
136  bool borderContains(int x, int y) const;
137 
141  static bool isBox(const std::vector<std::string>& network, int x, int y);
142 
148  static std::vector<AsciiBox> findBoxes(const std::vector<std::string>& network);
149 
150 };
151 
152 
153 } // namespace essentia
154 
155 #endif // ESSENTIA_UTILS_ASCIIDAG_H
Definition: asciidag.h:121
int width
Definition: asciidag.h:124
static bool isBox(const std::vector< std::string > &network, int x, int y)
int posY
Definition: asciidag.h:123
bool borderContains(int x, int y) const
int posX
Definition: asciidag.h:123
std::string title
Definition: asciidag.h:125
int height
Definition: asciidag.h:124
static std::vector< AsciiBox > findBoxes(const std::vector< std::string > &network)
AsciiBox(const std::vector< std::string > &network, int x, int y)
Definition: asciidag.h:76
std::string & at(int i)
Definition: asciidag.h:101
AsciiCanvas & operator=(const std::vector< std::string > &other)
AsciiCanvas(const NetworkType &network)
Definition: asciidag.h:80
char & at(const Position &pos)
Definition: asciidag.h:104
int height() const
Definition: asciidag.h:86
int width() const
Definition: asciidag.h:87
const std::string & at(int i) const
Definition: asciidag.h:100
char at(const Position &pos) const
Definition: asciidag.h:103
Definition: asciidag.h:32
int y
Definition: asciidag.h:34
Position(int x_, int y_)
Definition: asciidag.h:36
int x
Definition: asciidag.h:34
bool operator==(const Position &other) const
Definition: asciidag.h:42
Position operator+(const Position &other) const
Definition: asciidag.h:38
#define ARRAY_SIZE(arr)
Definition: essentiautil.h:36
Definition: algorithm.h:28
std::vector< std::string > makeRectangle(const std::string &network)
std::ostream & operator<<(std::ostream &out, const Parameter &p)
Position Direction
Definition: asciidag.h:52