blob: e6ab66426fb12a2c89a94095487aecebea046312 [file] [log] [blame]
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +00001//===-- SequenceToOffsetTable.h - Compress similar sequences ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// SequenceToOffsetTable can be used to emit a number of null-terminated
11// sequences as one big array. Use the same memory when a sequence is a suffix
12// of another.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef TBLGEN_SEQUENCE_TO_OFFSET_TABLE_H
17#define TBLGEN_SEQUENCE_TO_OFFSET_TABLE_H
18
19#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000020#include <algorithm>
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000021#include <cassert>
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +000022#include <cctype>
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000023#include <functional>
Craig Topper862b5042013-08-28 07:03:02 +000024#include <map>
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000025#include <vector>
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000026
27namespace llvm {
28
29/// SequenceToOffsetTable - Collect a number of terminated sequences of T.
30/// Compute the layout of a table that contains all the sequences, possibly by
31/// reusing entries.
32///
Dmitri Gribenkoa00b80b2012-08-23 16:54:08 +000033/// @tparam SeqT The sequence container. (vector or string).
34/// @tparam Less A stable comparator for SeqT elements.
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000035template<typename SeqT, typename Less = std::less<typename SeqT::value_type> >
36class SequenceToOffsetTable {
37 typedef typename SeqT::value_type ElemT;
38
39 // Define a comparator for SeqT that sorts a suffix immediately before a
40 // sequence with that suffix.
41 struct SeqLess : public std::binary_function<SeqT, SeqT, bool> {
42 Less L;
43 bool operator()(const SeqT &A, const SeqT &B) const {
44 return std::lexicographical_compare(A.rbegin(), A.rend(),
45 B.rbegin(), B.rend(), L);
46 }
47 };
48
49 // Keep sequences ordered according to SeqLess so suffixes are easy to find.
50 // Map each sequence to its offset in the table.
51 typedef std::map<SeqT, unsigned, SeqLess> SeqMap;
52
53 // Sequences added so far, with suffixes removed.
54 SeqMap Seqs;
55
56 // Entries in the final table, or 0 before layout was called.
57 unsigned Entries;
58
59 // isSuffix - Returns true if A is a suffix of B.
60 static bool isSuffix(const SeqT &A, const SeqT &B) {
61 return A.size() <= B.size() && std::equal(A.rbegin(), A.rend(), B.rbegin());
62 }
63
64public:
65 SequenceToOffsetTable() : Entries(0) {}
66
67 /// add - Add a sequence to the table.
68 /// This must be called before layout().
69 void add(const SeqT &Seq) {
70 assert(Entries == 0 && "Cannot call add() after layout()");
71 typename SeqMap::iterator I = Seqs.lower_bound(Seq);
72
73 // If SeqMap contains a sequence that has Seq as a suffix, I will be
74 // pointing to it.
75 if (I != Seqs.end() && isSuffix(Seq, I->first))
76 return;
77
78 I = Seqs.insert(I, std::make_pair(Seq, 0u));
79
80 // The entry before I may be a suffix of Seq that can now be erased.
81 if (I != Seqs.begin() && isSuffix((--I)->first, Seq))
82 Seqs.erase(I);
83 }
84
Chris Lattner387c9dc2012-05-17 15:55:41 +000085 bool empty() const { return Seqs.empty(); }
Craig Topper4e5babe2012-09-14 06:37:49 +000086
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000087 /// layout - Computes the final table layout.
Craig Topper5974c312012-09-15 01:22:42 +000088 void layout() {
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +000089 assert(Entries == 0 && "Can only call layout() once");
90 // Lay out the table in Seqs iteration order.
91 for (typename SeqMap::iterator I = Seqs.begin(), E = Seqs.end(); I != E;
92 ++I) {
93 I->second = Entries;
94 // Include space for a terminator.
95 Entries += I->first.size() + 1;
96 }
97 }
98
99 /// get - Returns the offset of Seq in the final table.
100 unsigned get(const SeqT &Seq) const {
101 assert(Entries && "Call layout() before get()");
102 typename SeqMap::const_iterator I = Seqs.lower_bound(Seq);
103 assert(I != Seqs.end() && isSuffix(Seq, I->first) &&
104 "get() called with sequence that wasn't added first");
105 return I->second + (I->first.size() - Seq.size());
106 }
107
108 /// emit - Print out the table as the body of an array initializer.
109 /// Use the Print function to print elements.
Jakob Stoklund Olesen0d4e2ea2012-03-30 20:24:14 +0000110 void emit(raw_ostream &OS,
111 void (*Print)(raw_ostream&, ElemT),
112 const char *Term = "0") const {
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +0000113 assert(Entries && "Call layout() before emit()");
114 for (typename SeqMap::const_iterator I = Seqs.begin(), E = Seqs.end();
115 I != E; ++I) {
116 OS << " /* " << I->second << " */ ";
117 for (typename SeqT::const_iterator SI = I->first.begin(),
118 SE = I->first.end(); SI != SE; ++SI) {
119 Print(OS, *SI);
120 OS << ", ";
121 }
Jakob Stoklund Olesen0d4e2ea2012-03-30 20:24:14 +0000122 OS << Term << ",\n";
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +0000123 }
124 }
125};
126
Jakob Stoklund Olesenc19f72b2012-03-30 21:12:52 +0000127// Helper function for SequenceToOffsetTable<string>.
128static inline void printChar(raw_ostream &OS, char C) {
129 unsigned char UC(C);
130 if (isalnum(UC) || ispunct(UC)) {
131 OS << '\'';
132 if (C == '\\' || C == '\'')
133 OS << '\\';
134 OS << C << '\'';
135 } else {
136 OS << unsigned(UC);
137 }
138}
139
Jakob Stoklund Olesen184440e2012-03-30 17:25:40 +0000140} // end namespace llvm
141
142#endif