|
| 1 | +//==-- SimpleTable.h -- tabular data simple transforms and I/O -------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// Defines a simple model for a tabular data container with simple operations |
| 9 | +// over rows and columns. Columns are named and referenced by name. |
| 10 | +// Major use case is to model dynamically-sized "2D" sets of output files by |
| 11 | +// tools like post-link and being able to manipulate columns - for example |
| 12 | +// replace a column listing files with bitcode with a column of .spv files. |
| 13 | +// |
| 14 | +// TODO May make sense to make the interface SQL-like in future if evolves. |
| 15 | +// TODO Use YAML as serialization format. |
| 16 | +// TODO Today cells are strings, but can be extended to other commonly used |
| 17 | +// types such as integers. |
| 18 | +// |
| 19 | +// Example of a table: |
| 20 | +// [Code|Symbols|Properties] |
| 21 | +// a_0.bc|a_0.sym|a_0.props |
| 22 | +// a_1.bc|a_1.sym|a_1.props |
| 23 | +//===----------------------------------------------------------------------===// |
| 24 | + |
| 25 | +#ifndef LLVM_SUPPORT_SIMPLETABLE_H |
| 26 | +#define LLVM_SUPPORT_SIMPLETABLE_H |
| 27 | + |
| 28 | +#include "llvm/ADT/ArrayRef.h" |
| 29 | +#include "llvm/ADT/StringRef.h" |
| 30 | +#include "llvm/Support/Error.h" |
| 31 | +#include "llvm/Support/MemoryBuffer.h" |
| 32 | + |
| 33 | +#include <list> |
| 34 | +#include <map> |
| 35 | +#include <memory> |
| 36 | +#include <string> |
| 37 | + |
| 38 | +namespace llvm { |
| 39 | +namespace util { |
| 40 | + |
| 41 | +// The tabular data abstraction. |
| 42 | +// TODO Supports only cells of string type only for now. |
| 43 | +class SimpleTable { |
| 44 | +public: |
| 45 | + using UPtrTy = std::unique_ptr<SimpleTable>; |
| 46 | + |
| 47 | + // A single row in the table. Basically a vector of string data cells. |
| 48 | + class Row { |
| 49 | + public: |
| 50 | + Row() = default; |
| 51 | + |
| 52 | + Row(SimpleTable *Parent, int NCols) : Parent(Parent) { |
| 53 | + Cells.resize(NCols); |
| 54 | + } |
| 55 | + StringRef getCell(StringRef ColName) const; |
| 56 | + StringRef getCell(StringRef ColName, StringRef DefaultVal) const; |
| 57 | + |
| 58 | + void setParent(SimpleTable *P) { |
| 59 | + assert(Parent == nullptr && "parent already set"); |
| 60 | + Parent = P; |
| 61 | + } |
| 62 | + |
| 63 | + private: |
| 64 | + friend class SimpleTable; |
| 65 | + |
| 66 | + Row(SimpleTable *Parent) : Parent(Parent) {} |
| 67 | + |
| 68 | + Row(SimpleTable *Parent, ArrayRef<StringRef> R) : Row(Parent) { |
| 69 | + for (auto Cell : R) |
| 70 | + Cells.emplace_back(Cell.str()); |
| 71 | + } |
| 72 | + |
| 73 | + std::string &operator[](int I) { return Cells[I]; } |
| 74 | + |
| 75 | + const std::string &operator[](int I) const { return Cells[I]; } |
| 76 | + |
| 77 | + private: |
| 78 | + std::vector<std::string> Cells; |
| 79 | + SimpleTable *Parent; |
| 80 | + }; |
| 81 | + |
| 82 | +public: |
| 83 | + SimpleTable() = default; |
| 84 | + static Expected<UPtrTy> create(ArrayRef<StringRef> ColNames); |
| 85 | + static Expected<UPtrTy> create(int NColumns); |
| 86 | + int getNumColumns() const { return static_cast<int>(ColumnNames.size()); } |
| 87 | + int getNumRows() const { return static_cast<int>(rows().size()); } |
| 88 | + |
| 89 | + // Add a column with given title and assign cells to given values. The table |
| 90 | + // must be empty or the number of the input cells must match column size. |
| 91 | + Error addColumn(const Twine &Title, ArrayRef<StringRef> Cells); |
| 92 | + Error addColumn(const Twine &Title, ArrayRef<std::string> Cells); |
| 93 | + |
| 94 | + // Replaces a column in this table with another column of the same size from |
| 95 | + // another table. Columns are identified by their names. If source column name |
| 96 | + // is empty, it is assumed to match the source's name. |
| 97 | + Error replaceColumn(StringRef Name, const SimpleTable &Src, |
| 98 | + StringRef SrcName = ""); |
| 99 | + |
| 100 | + // Renames a column. |
| 101 | + Error renameColumn(StringRef OldName, StringRef NewName); |
| 102 | + |
| 103 | + // Removes all columns except those with given names. |
| 104 | + Error peelColumns(ArrayRef<StringRef> ColNames); |
| 105 | + |
| 106 | + // Iterates all cells top-down lef-right and adds their values to given |
| 107 | + // container. |
| 108 | + void linearize(std::vector<std::string> &Res) const; |
| 109 | + |
| 110 | + // Serialized the table to a stream. |
| 111 | + void write(raw_ostream &Out, bool WriteTitles = true, |
| 112 | + char ColSep = '|') const; |
| 113 | + |
| 114 | + // De-serializes a table from a stream. |
| 115 | + static Expected<UPtrTy> read(MemoryBuffer *Buf, char ColSep = '|'); |
| 116 | + |
| 117 | + // De-serializes a table from a file. |
| 118 | + static Expected<UPtrTy> read(const Twine &FileName, char ColSep = '|'); |
| 119 | + |
| 120 | + const SmallVectorImpl<Row> &rows() const { return Rows; } |
| 121 | + |
| 122 | + void addRow(ArrayRef<StringRef> R) { |
| 123 | + assert((R.size() == ColumnNames.size()) && "column number mismatch"); |
| 124 | + Rows.emplace_back(Row(this, R)); |
| 125 | + } |
| 126 | + |
| 127 | + int getColumnId(StringRef ColName) const; |
| 128 | + |
| 129 | + Row &operator[](int I) { return Rows[I]; } |
| 130 | + const Row &operator[](int I) const { return Rows[I]; } |
| 131 | + |
| 132 | +private: |
| 133 | + Error addColumnName(StringRef ColName); |
| 134 | + void rebuildName2NumMapping(); |
| 135 | + |
| 136 | + std::map<StringRef, int> ColumnName2Num; |
| 137 | + // Use list as the holder of string objects as modification never invalidate |
| 138 | + // element addresses and iterators, unlike vector. |
| 139 | + std::list<std::string> ColumnNames; |
| 140 | + SmallVector<std::list<std::string>::iterator, 4> ColumnNum2Name; |
| 141 | + SmallVector<Row, 4> Rows; |
| 142 | +}; |
| 143 | + |
| 144 | +} // namespace util |
| 145 | +} // namespace llvm |
| 146 | + |
| 147 | +#endif // LLVM_SUPPORT_SIMPLETABLE_H |
0 commit comments