|
3 | 3 | // Copyright 2009-2011 Microsoft Corporation; Saarland University;
|
4 | 4 | // Jan Silovsky; Yanmin Qian;
|
5 | 5 | // Johns Hopkins University (Author: Daniel Povey)
|
| 6 | +// 2016 Xiaohui Zhang |
6 | 7 |
|
7 | 8 | // See ../../COPYING for clarification regarding multiple authors
|
8 | 9 | //
|
@@ -87,6 +88,112 @@ template<class T> inline void ReadBasicType(std::istream &is,
|
87 | 88 | }
|
88 | 89 | }
|
89 | 90 |
|
| 91 | +// Template that covers integers. |
| 92 | +template<class T> |
| 93 | +inline void WriteIntegerPairVector(std::ostream &os, bool binary, |
| 94 | + const std::vector<std::pair<T, T> > &v) { |
| 95 | + // Compile time assertion that this is not called with a wrong type. |
| 96 | + KALDI_ASSERT_IS_INTEGER_TYPE(T); |
| 97 | + if (binary) { |
| 98 | + char sz = sizeof(T); // this is currently just a check. |
| 99 | + os.write(&sz, 1); |
| 100 | + int32 vecsz = static_cast<int32>(v.size()); |
| 101 | + KALDI_ASSERT((size_t)vecsz == v.size()); |
| 102 | + os.write(reinterpret_cast<const char *>(&vecsz), sizeof(vecsz)); |
| 103 | + if (vecsz != 0) { |
| 104 | + os.write(reinterpret_cast<const char *>(&(v[0])), sizeof(T) * vecsz * 2); |
| 105 | + } |
| 106 | + } else { |
| 107 | + // focus here is on prettiness of text form rather than |
| 108 | + // efficiency of reading-in. |
| 109 | + // reading-in is dominated by low-level operations anyway: |
| 110 | + // for efficiency use binary. |
| 111 | + os << "[ "; |
| 112 | + typename std::vector<std::pair<T, T> >::const_iterator iter = v.begin(), |
| 113 | + end = v.end(); |
| 114 | + for (; iter != end; ++iter) { |
| 115 | + if (sizeof(T) == 1) |
| 116 | + os << static_cast<int16>(iter->first) << ',' |
| 117 | + << static_cast<int16>(iter->second) << ' '; |
| 118 | + else |
| 119 | + os << iter->first << ',' |
| 120 | + << iter->second << ' '; |
| 121 | + } |
| 122 | + os << "]\n"; |
| 123 | + } |
| 124 | + if (os.fail()) { |
| 125 | + throw std::runtime_error("Write failure in WriteIntegerPairVector."); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// Template that covers integers. |
| 130 | +template<class T> |
| 131 | +inline void ReadIntegerPairVector(std::istream &is, bool binary, |
| 132 | + std::vector<std::pair<T, T> > *v) { |
| 133 | + KALDI_ASSERT_IS_INTEGER_TYPE(T); |
| 134 | + KALDI_ASSERT(v != NULL); |
| 135 | + if (binary) { |
| 136 | + int sz = is.peek(); |
| 137 | + if (sz == sizeof(T)) { |
| 138 | + is.get(); |
| 139 | + } else { // this is currently just a check. |
| 140 | + KALDI_ERR << "ReadIntegerPairVector: expected to see type of size " |
| 141 | + << sizeof(T) << ", saw instead " << sz << ", at file position " |
| 142 | + << is.tellg(); |
| 143 | + } |
| 144 | + int32 vecsz; |
| 145 | + is.read(reinterpret_cast<char *>(&vecsz), sizeof(vecsz)); |
| 146 | + if (is.fail() || vecsz < 0) goto bad; |
| 147 | + v->resize(vecsz); |
| 148 | + if (vecsz > 0) { |
| 149 | + is.read(reinterpret_cast<char *>(&((*v)[0])), sizeof(T)*vecsz*2); |
| 150 | + } |
| 151 | + } else { |
| 152 | + std::vector<std::pair<T, T> > tmp_v; // use temporary so v doesn't use extra memory |
| 153 | + // due to resizing. |
| 154 | + is >> std::ws; |
| 155 | + if (is.peek() != static_cast<int>('[')) { |
| 156 | + KALDI_ERR << "ReadIntegerPairVector: expected to see [, saw " |
| 157 | + << is.peek() << ", at file position " << is.tellg(); |
| 158 | + } |
| 159 | + is.get(); // consume the '['. |
| 160 | + is >> std::ws; // consume whitespace. |
| 161 | + while (is.peek() != static_cast<int>(']')) { |
| 162 | + if (sizeof(T) == 1) { // read/write chars as numbers. |
| 163 | + int16 next_t1, next_t2; |
| 164 | + is >> next_t1; |
| 165 | + if (is.fail()) goto bad; |
| 166 | + if (is.peek() != static_cast<int>(',')) |
| 167 | + KALDI_ERR << "ReadIntegerPairVector: expected to see ',', saw " |
| 168 | + << is.peek() << ", at file position " << is.tellg(); |
| 169 | + is.get(); // consume the ','. |
| 170 | + is >> next_t2 >> std::ws; |
| 171 | + if (is.fail()) goto bad; |
| 172 | + else |
| 173 | + tmp_v.push_back(std::make_pair<T, T>((T)next_t1, (T)next_t2)); |
| 174 | + } else { |
| 175 | + T next_t1, next_t2; |
| 176 | + is >> next_t1; |
| 177 | + if (is.fail()) goto bad; |
| 178 | + if (is.peek() != static_cast<int>(',')) |
| 179 | + KALDI_ERR << "ReadIntegerPairVector: expected to see ',', saw " |
| 180 | + << is.peek() << ", at file position " << is.tellg(); |
| 181 | + is.get(); // consume the ','. |
| 182 | + is >> next_t2 >> std::ws; |
| 183 | + if (is.fail()) goto bad; |
| 184 | + else |
| 185 | + tmp_v.push_back(std::make_pair<T, T>((T)next_t1, (T)next_t2)); |
| 186 | + } |
| 187 | + } |
| 188 | + is.get(); // get the final ']'. |
| 189 | + *v = tmp_v; // could use std::swap to use less temporary memory, but this |
| 190 | + // uses less permanent memory. |
| 191 | + } |
| 192 | + if (!is.fail()) return; |
| 193 | + bad: |
| 194 | + KALDI_ERR << "ReadIntegerPairVector: read failure at file position " |
| 195 | + << is.tellg(); |
| 196 | +} |
90 | 197 |
|
91 | 198 | template<class T> inline void WriteIntegerVector(std::ostream &os, bool binary,
|
92 | 199 | const std::vector<T> &v) {
|
|
0 commit comments