GAMS Data eXchange (GDX) API
Low-level "eXPert level" C++ API for reading and writing GAMS Data eXchange files
Loading...
Searching...
No Matches
gxfile.h
1/*
2 * GAMS - General Algebraic Modeling System GDX API
3 *
4 * Copyright (c) 2017-2024 GAMS Software GmbH <support@gams.com>
5 * Copyright (c) 2017-2024 GAMS Development Corp. <support@gams.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#pragma once
27
28// Description:
29// This unit defines the GDX Object as a C++ object.
30
31#include "gdlib/datastorage.h" // for TLinkedData
32#include "gclgms.h" // for GLOBAL_MAX_INDEX_DIM, GMS_MAX_INDEX_DIM
33#include "gdlib/gmsdata.h" // for TTblGamsData
34#include "gdlib/gmsobj.h" // for TBooleanBitArray, TXList, TXStrings
35#include "gdlib/strhash.h" // for TXCSStrHashList, TXStrHashList
36#include "gdlib/utils.h"
37#include <array> // for array
38#include <cstdint> // for int64_t, uint8_t
39#include <cstring> // for size_t
40#include <initializer_list>// for initializer_list
41#include <limits> // for numeric_limits
42#include <memory> // for unique_ptr, allocator
43#include <optional> // for optional
44#include <string> // for string
45
46namespace gdlib::gmsstrm
47{
48class TXStream;
49}// namespace gdx::gmsstrm
50
51//======================================================================================================================
52
53namespace gdx
54{
55
56using TgdxUELIndex = std::array<int, GMS_MAX_INDEX_DIM>;
57using TgdxValues = std::array<double, GMS_VAL_SCALE + 1>;
58
59using TDomainIndexProc_t = void ( * )( int RawIndex, int MappedIndex, void *Uptr );
60using TDataStoreProc_t = void ( * )( const int *Indx, const double *Vals );
61using TDataStoreFiltProc_t = int ( * )( const int *Indx, const double *Vals, void *Uptr );
62using TDataStoreExProc_t = int ( * )( const int *Indx, const double *Vals, const int afdim, void *Uptr );
63
64using TDataStoreExProc_F = int ( * )( const int *Indx, const double *Vals, const int afdim, int64_t Uptr );
65using TDataStoreFiltProc_F = int ( * )( const int *Indx, const double *Vals, int64_t Uptr );
66using TDomainIndexProc_F = void ( * )( int RawIndex, int MappedIndex, int64_t Uptr );
67
68const std::array<int, GMS_DT_ALIAS + 1> DataTypSize { 1, 1, 5, 5, 0 };
69
70constexpr int DOMC_UNMAPPED = -2,// indicator for unmapped index pos
71 DOMC_EXPAND = -1, // indicator growing index pos
72 DOMC_STRICT = 0; // indicator mapped index pos
73
74const std::string BADUEL_PREFIX = "?L__",
75 BADStr_PREFIX = "?Str__",
76 strGDXCOMPRESS = "GDXCOMPRESS",
77 strGDXCONVERT = "GDXCONVERT";
78
79struct TDFilter {
80 int FiltNumber {}, FiltMaxUel {};
81 gdlib::gmsobj::TBooleanBitArray FiltMap {};
82 bool FiltSorted {};
83
84 TDFilter( int Nr, int UserHigh ) : FiltNumber { Nr },
85 FiltMaxUel { UserHigh }
86 {
87 }
88
89 ~TDFilter() = default;
90
91 [[nodiscard]] int MemoryUsed() const
92 {
93 return FiltMap.MemoryUsed();
94 }
95
96 [[nodiscard]] bool InFilter( int V ) const
97 {
98 return V >= 0 && V <= FiltMaxUel && FiltMap.GetBit( V );
99 }
100
101 void SetFilter( int ix, bool v )
102 {
103 FiltMap.SetBit( ix, v );
104 }
105};
106
107using TSetBitMap = gdlib::gmsobj::TBooleanBitArray;
108
109enum class TgdxDAction : uint8_t
110{
111 dm_unmapped,
112 dm_strict,
113 dm_filter,
114 dm_expand
115};
116
117struct TDomain {
118 TDFilter *DFilter;
119 TgdxDAction DAction;
120};
121
122using TDomainList = std::array<TDomain, GLOBAL_MAX_INDEX_DIM>;
123
124using TCommentsList = gdlib::gmsobj::TXStrings;
125
127 int SSyNr;
128 int64_t SPosition;
129 int SDim, SDataCount, SErrors;
130 gdxSyType SDataType;
131 int SUserInfo;
132 bool SSetText;
133 std::array<char, GMS_SSSIZE> SExplTxt;
134 bool SIsCompressed;
135 bool SScalarFrst; // not stored
136 std::unique_ptr<int[]> SDomSymbols,// real domain info
137 SDomStrings;// relaxed domain info
138 std::optional<TCommentsList> SCommentsList;
139 std::unique_ptr<TSetBitMap> SSetBitMap;// for 1-dim sets only
140};
142
143enum TgdxIntlValTyp : uint8_t
144{// values stored internally via the indicator byte
145 vm_valund,
146 vm_valna,
147 vm_valpin,
148 vm_valmin,
149 vm_valeps,
150 vm_zero,
151 vm_one,
152 vm_mone,
153 vm_half,
154 vm_two,
155 vm_normal,
156 vm_count
157};
158
159enum TgxFileMode : uint8_t
160{
161 f_not_open,
162 fr_init,
163 fw_init,
164 fw_dom_raw,
165 fw_dom_map,
166 fw_dom_str,
167 fw_raw_data,
168 fw_map_data,
169 fw_str_data,
170 f_raw_elem,
171 f_map_elem,
172 f_str_elem,
173 fr_raw_data,
174 fr_map_data,
175 fr_mapr_data,
176 fr_str_data,
177 fr_filter,
178 fr_slice,
179 tgxfilemode_count
180};
181
182class TgxModeSet : public utils::IContainsPredicate<TgxFileMode>
183{
184 std::array<bool, tgxfilemode_count> modeActive {};
185 uint8_t count {};
186
187public:
188 TgxModeSet( const std::initializer_list<TgxFileMode> &modes );
189 ~TgxModeSet() override = default;
190 [[nodiscard]] bool contains( const TgxFileMode &mode ) const override;
191 [[nodiscard]] bool empty() const;
192};
193
194const TgxModeSet AnyWriteMode { fw_init, fw_dom_raw, fw_dom_map, fw_dom_str, fw_raw_data, fw_map_data, fw_str_data },
195 AnyReadMode { fr_init, fr_raw_data, fr_map_data, fr_mapr_data, fr_str_data };
196
197enum class TgdxElemSize : uint8_t
198{
199 sz_byte,
200 sz_word,
201 sz_integer
202};
203
204// N.B.: we store integers in [0..high(integer)] in TIntegerMapping, so
205// FMAXCAPACITY = high(integer) + 1 is all we will ever need, and we will
206// never get a request to grow any larger. The checks and code
207// in growMapping reflect this
209{
210 int64_t FCapacity {}, FMapBytes {};
211 int64_t FMAXCAPACITY { std::numeric_limits<int>::max() + static_cast<int64_t>( 1 ) };
212 int FHighestIndex {};
213 int *PMap {};
214
215 void growMapping( int F );
216
217public:
218 TIntegerMapping() = default;
220 [[nodiscard]] int MemoryUsed() const;
221 [[nodiscard]] int GetHighestIndex() const;
222 [[nodiscard]] int GetMapping( int F ) const;
223 void SetMapping( int F, int T );
224 [[nodiscard]] int size() const;
225 [[nodiscard]] bool empty() const;
226 void reset();
227};
228
229enum class TUELUserMapStatus : uint8_t
230{
231 map_unknown,
232 map_unsorted,
233 map_sorted,
234 map_sortgrow,
235 map_sortfull
236};
237
238template<typename T>
239using TXStrHashListImpl = gdlib::strhash::TXStrHashList<T>;
240
241template<typename T>
242using TXCSStrHashListImpl = gdlib::strhash::TXCSStrHashList<T>;
243
244class TUELTable final : public TXStrHashListImpl<int>
245{
246 TUELUserMapStatus FMapToUserStatus { TUELUserMapStatus::map_unknown };
247
248public:
249 std::unique_ptr<TIntegerMapping> UsrUel2Ent {};// from user uelnr to table entry
250 TUELTable();
251 ~TUELTable() override = default;
252 [[nodiscard]] int size() const;
253 [[nodiscard]] bool empty() const;
254 [[nodiscard]] int GetUserMap( int i ) const;
255 void SetUserMap( int EN, int N );
256 int NewUsrUel( int EN );
257 int AddUsrNew( const char *s, size_t slen );
258 int AddUsrIndxNew( const char *s, size_t slen, int UelNr );
259 [[nodiscard]] int GetMaxUELLength() const;
260 int IndexOf( const char *s );
261 int AddObject( const char *id, size_t idlen, int mapping );
262 int StoreObject( const char *id, size_t idlen, int mapping );
263 const char *operator[]( int index ) const;
264 void RenameEntry( int N, const char *s );
265 [[nodiscard]] int MemoryUsed() const;
266 void SaveToStream( gdlib::gmsstrm::TXStream &S );
267 void LoadFromStream( gdlib::gmsstrm::TXStream &S );
268 TUELUserMapStatus GetMapToUserStatus();
269 void ResetMapToUserStatus();
270};
271
272int MakeGoodExplText( char *s );
273
274struct TAcronym {
275 std::string AcrName, AcrText;
276 int AcrMap {}, AcrReadMap { -1 };
277 bool AcrAutoGen {};
278
279 TAcronym( const char *Name, const char *Text, int Map );
280 explicit TAcronym( gdlib::gmsstrm::TXStream &S );
281 TAcronym() = default;
282 virtual ~TAcronym() = default;
283 [[nodiscard]] int MemoryUsed() const;
284 void SaveToStream( gdlib::gmsstrm::TXStream &S ) const;
285 void SetNameAndText( const char *Name, const char *Text );
286};
287
289{
290 gdlib::gmsobj::TXList<TAcronym> FList;
291
292public:
293 TAcronymList() = default;
295 int FindEntry( int Map );
296 int FindName( const char *Name );
297 int AddEntry( const char *Name, const char *Text, int Map );
298 void CheckEntry( int Map );
299 void SaveToStream( gdlib::gmsstrm::TXStream &S );
300 void LoadFromStream( gdlib::gmsstrm::TXStream &S );
301 int MemoryUsed();
302 [[nodiscard]] int size() const;
303 TAcronym &operator[]( int Index );
304};
305
307{
308 gdlib::gmsobj::TXList<TDFilter> FList;
309
310public:
311 TFilterList() = default;
312 ~TFilterList();
313 void AddFilter( TDFilter *F );
314 void DeleteFilter( int ix );
315 TDFilter *FindFilter( int Nr );
316 [[nodiscard]] size_t MemoryUsed() const;
317};
318
319using TIntlValueMapDbl = std::array<double, vm_count>;
320using TIntlValueMapI64 = std::array<int64_t, vm_count>;
321
322using LinkedDataType = gdlib::datastorage::TLinkedData<int, double>;
323using LinkedDataIteratorType = gdlib::datastorage::TLinkedDataRec<int, double> *;
324
325using TSetTextList = TXCSStrHashListImpl<int>;
326
327using TNameList = TXStrHashListImpl<PgdxSymbRecord>;
328
329template<typename T>
330using TTblGamsDataImpl = gdlib::gmsdata::TTblGamsData<T>;
331
332using TDomainStrList = TXStrHashListImpl<uint8_t>;
333
334enum tvarvaltype : uint8_t
335{
336 vallevel, // 1
337 valmarginal,// 2
338 vallower, // 3
339 valupper, // 4
340 valscale // 5
341};
342
343extern std::string DLLLoadPath;// can be set by loader, so the "dll" knows where it is loaded from
344
346{
347 int64_t i;
348 void *p;
349};
350
351bool IsGoodIdent( const char *S );
352bool CanBeQuoted( const char *s, size_t slen );
353bool GoodUELString( const char *s, size_t slen );
354
355int ConvertGDXFile( const std::string &fn, const std::string &MyComp );
356
357}// namespace gdx
Definition: gxfile.h:289
Definition: gxfile.h:307
Definition: gxfile.h:209
Definition: gxfile.h:245
Definition: gxfile.h:183
Definition: gxfile.h:274
Definition: gxfile.h:79
Definition: gxfile.h:117
Definition: gxfile.h:126
Definition: gxfile.h:346