1 /* Copyright (C) 2020 IBM Corp. 2 * This program is Licensed under the Apache License, Version 2.0 3 * (the "License"); you may not use this file except in compliance 4 * with the License. You may obtain a copy of the License at 5 * http://www.apache.org/licenses/LICENSE-2.0 6 * Unless required by applicable law or agreed to in writing, software 7 * distributed under the License is distributed on an "AS IS" BASIS, 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9 * See the License for the specific language governing permissions and 10 * limitations under the License. See accompanying LICENSE file. 11 */ 12 13 #ifndef TOC_H 14 #define TOC_H 15 16 #include <iostream> 17 #include <fstream> 18 19 // Table of Contents or Index or Catalog 20 class TOC 21 { 22 23 private: 24 uint64_t rows; 25 uint64_t cols; 26 std::unique_ptr<uint64_t[]> idx; 27 28 public: 29 TOC() = default; 30 TOC(uint64_t r,uint64_t c)31 TOC(uint64_t r, uint64_t c) : 32 rows(r), cols(c), idx(std::make_unique<uint64_t[]>(r * c)) 33 {} 34 35 void print(std::ostream& out = std::cout) const 36 { 37 out << "Rows, Cols: " << rows << ", " << cols << std::endl; 38 for (uint64_t i = 0; i < rows; ++i) { 39 for (uint64_t j = 0; j < cols; ++j) { 40 out << getIdx(i, j) << " "; 41 } 42 out << std::endl; 43 } 44 } 45 getRows()46 uint64_t getRows() const { return this->rows; } 47 getCols()48 uint64_t getCols() const { return this->cols; } 49 getIdx(int i,int j)50 uint64_t getIdx(int i, int j) const { return this->idx[i * cols + j]; } 51 setIdx(int i,int j,uint64_t value)52 void setIdx(int i, int j, uint64_t value) { this->idx[i * cols + j] = value; } 53 write(std::ostream & s)54 void write(std::ostream& s) 55 { 56 s.write(reinterpret_cast<char*>(&rows), sizeof(uint64_t)); 57 s.write(reinterpret_cast<char*>(&cols), sizeof(uint64_t)); 58 s.write(reinterpret_cast<char*>(idx.get()), sizeof(uint64_t) * rows * cols); 59 } 60 read(std::istream & s)61 void read(std::istream& s) 62 { 63 s.read(reinterpret_cast<char*>(&rows), sizeof(uint64_t)); 64 s.read(reinterpret_cast<char*>(&cols), sizeof(uint64_t)); 65 // Deallocate existing, allocate new. 66 idx.reset(new uint64_t[rows * cols]); 67 s.read(reinterpret_cast<char*>(idx.get()), sizeof(uint64_t) * rows * cols); 68 } 69 memorySize()70 long memorySize() { return sizeof(uint64_t) * (2 + rows * cols); } 71 }; 72 73 #endif // TOC_H 74