SequenceToOffsetTable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===-- 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. #ifndef LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
  16. #define LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <algorithm>
  19. #include <cassert>
  20. #include <cctype>
  21. #include <functional>
  22. #include <map>
  23. #include <vector>
  24. namespace llvm {
  25. /// SequenceToOffsetTable - Collect a number of terminated sequences of T.
  26. /// Compute the layout of a table that contains all the sequences, possibly by
  27. /// reusing entries.
  28. ///
  29. /// @tparam SeqT The sequence container. (vector or string).
  30. /// @tparam Less A stable comparator for SeqT elements.
  31. template<typename SeqT, typename Less = std::less<typename SeqT::value_type> >
  32. class SequenceToOffsetTable {
  33. typedef typename SeqT::value_type ElemT;
  34. // Define a comparator for SeqT that sorts a suffix immediately before a
  35. // sequence with that suffix.
  36. struct SeqLess : public std::binary_function<SeqT, SeqT, bool> {
  37. Less L;
  38. bool operator()(const SeqT &A, const SeqT &B) const {
  39. return std::lexicographical_compare(A.rbegin(), A.rend(),
  40. B.rbegin(), B.rend(), L);
  41. }
  42. };
  43. // Keep sequences ordered according to SeqLess so suffixes are easy to find.
  44. // Map each sequence to its offset in the table.
  45. typedef std::map<SeqT, unsigned, SeqLess> SeqMap;
  46. // Sequences added so far, with suffixes removed.
  47. SeqMap Seqs;
  48. // Entries in the final table, or 0 before layout was called.
  49. unsigned Entries;
  50. // isSuffix - Returns true if A is a suffix of B.
  51. static bool isSuffix(const SeqT &A, const SeqT &B) {
  52. return A.size() <= B.size() && std::equal(A.rbegin(), A.rend(), B.rbegin());
  53. }
  54. public:
  55. SequenceToOffsetTable() : Entries(0) {}
  56. /// add - Add a sequence to the table.
  57. /// This must be called before layout().
  58. void add(const SeqT &Seq) {
  59. assert(Entries == 0 && "Cannot call add() after layout()");
  60. typename SeqMap::iterator I = Seqs.lower_bound(Seq);
  61. // If SeqMap contains a sequence that has Seq as a suffix, I will be
  62. // pointing to it.
  63. if (I != Seqs.end() && isSuffix(Seq, I->first))
  64. return;
  65. I = Seqs.insert(I, std::make_pair(Seq, 0u));
  66. // The entry before I may be a suffix of Seq that can now be erased.
  67. if (I != Seqs.begin() && isSuffix((--I)->first, Seq))
  68. Seqs.erase(I);
  69. }
  70. bool empty() const { return Seqs.empty(); }
  71. unsigned size() const {
  72. assert(Entries && "Call layout() before size()");
  73. return Entries;
  74. }
  75. /// layout - Computes the final table layout.
  76. void layout() {
  77. assert(Entries == 0 && "Can only call layout() once");
  78. // Lay out the table in Seqs iteration order.
  79. for (typename SeqMap::iterator I = Seqs.begin(), E = Seqs.end(); I != E;
  80. ++I) {
  81. I->second = Entries;
  82. // Include space for a terminator.
  83. Entries += I->first.size() + 1;
  84. }
  85. }
  86. /// get - Returns the offset of Seq in the final table.
  87. unsigned get(const SeqT &Seq) const {
  88. assert(Entries && "Call layout() before get()");
  89. typename SeqMap::const_iterator I = Seqs.lower_bound(Seq);
  90. assert(I != Seqs.end() && isSuffix(Seq, I->first) &&
  91. "get() called with sequence that wasn't added first");
  92. return I->second + (I->first.size() - Seq.size());
  93. }
  94. /// emit - Print out the table as the body of an array initializer.
  95. /// Use the Print function to print elements.
  96. void emit(raw_ostream &OS,
  97. void (*Print)(raw_ostream&, ElemT),
  98. const char *Term = "0") const {
  99. assert(Entries && "Call layout() before emit()");
  100. for (typename SeqMap::const_iterator I = Seqs.begin(), E = Seqs.end();
  101. I != E; ++I) {
  102. OS << " /* " << I->second << " */ ";
  103. for (typename SeqT::const_iterator SI = I->first.begin(),
  104. SE = I->first.end(); SI != SE; ++SI) {
  105. Print(OS, *SI);
  106. OS << ", ";
  107. }
  108. OS << Term << ",\n";
  109. }
  110. }
  111. };
  112. // Helper function for SequenceToOffsetTable<string>.
  113. static inline void printChar(raw_ostream &OS, char C) {
  114. unsigned char UC(C);
  115. if (isalnum(UC) || ispunct(UC)) {
  116. OS << '\'';
  117. if (C == '\\' || C == '\'')
  118. OS << '\\';
  119. OS << C << '\'';
  120. } else {
  121. OS << unsigned(UC);
  122. }
  123. }
  124. } // end namespace llvm
  125. #endif