1 /**
2  * @file   result_coords.h
3  *
4  * @section LICENSE
5  *
6  * The MIT License
7  *
8  * @copyright Copyright (c) 2017-2021 TileDB, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  * @section DESCRIPTION
29  *
30  * This file defines struct ResultCoords.
31  */
32 
33 #ifndef TILEDB_RESULT_COORDS_H
34 #define TILEDB_RESULT_COORDS_H
35 
36 #include <iostream>
37 #include <vector>
38 
39 #include "tiledb/sm/query/result_tile.h"
40 
41 using namespace tiledb::common;
42 
43 namespace tiledb {
44 namespace sm {
45 
46 /**
47  * Stores information about cell coordinates of a sparse fragment
48  * that are in the result of a subarray query.
49  */
50 struct ResultCoords {
51   /**
52    * The result tile the coords belong to.
53    *
54    * Note that the tile this points to is allocated and freed in
55    * sparse_read/dense_read, so the lifetime of this struct must not exceed
56    * the scope of those functions.
57    */
58   ResultTile* tile_;
59   /** The position of the coordinates in the tile. */
60   uint64_t pos_;
61   /** Whether this instance is "valid". */
62   bool valid_;
63 
64   /** Constructor. */
ResultCoordsResultCoords65   ResultCoords()
66       : tile_(nullptr)
67       , pos_(0)
68       , valid_(false) {
69   }
70 
71   /** Constructor. */
ResultCoordsResultCoords72   ResultCoords(ResultTile* tile, uint64_t pos)
73       : tile_(tile)
74       , pos_(pos)
75       , valid_(true) {
76   }
77 
78   /** Moves to the next cell */
nextResultCoords79   bool next() {
80     if (pos_ == tile_->cell_num() - 1)
81       return false;
82 
83     pos_++;
84     return true;
85   }
86 
87   /** Invalidate this instance. */
invalidateResultCoords88   void invalidate() {
89     valid_ = false;
90   }
91 
92   /** Return true if this instance is valid. */
validResultCoords93   bool valid() const {
94     return valid_;
95   }
96 
97   /**
98    * Returns a string coordinate. Applicable only to string
99    * dimensions.
100    */
coord_stringResultCoords101   std::string coord_string(unsigned dim_idx) const {
102     return tile_->coord_string(pos_, dim_idx);
103   }
104 
105   /**
106    * Returns the coordinate at the object's position `pos_` from the object's
107    * tile `tile_` on the given dimension.
108    *
109    * @param dim_idx The index of the dimension to retrieve the coordinate for.
110    * @return A constant pointer to the requested coordinate.
111    */
coordResultCoords112   const void* coord(unsigned dim_idx) const {
113     return tile_->coord(pos_, dim_idx);
114   }
115 
116   /**
117    * Returns true if the coordinates (at the current position) of the
118    * calling ResultCoords object and the input are the same across all
119    * dimensions.
120    */
same_coordsResultCoords121   bool same_coords(const ResultCoords& rc) const {
122     return tile_->same_coords(*(rc.tile_), pos_, rc.pos_);
123   }
124 };
125 
126 }  // namespace sm
127 }  // namespace tiledb
128 
129 #endif  // TILEDB_RESULT_COORDS_H
130