2 * NOTICE and LICENSE for Tecplot Input/Output Library (TecIO) - OpenFOAM
4 * Copyright (C) 1988-2009 Tecplot, Inc. All rights reserved worldwide.
6 * Tecplot hereby grants OpenCFD limited authority to distribute without
7 * alteration the source code to the Tecplot Input/Output library, known
8 * as TecIO, as part of its distribution of OpenFOAM and the
9 * OpenFOAM_to_Tecplot converter. Users of this converter are also hereby
10 * granted access to the TecIO source code, and may redistribute it for the
11 * purpose of maintaining the converter. However, no authority is granted
12 * to alter the TecIO source code in any form or manner.
14 * This limited grant of distribution does not supersede Tecplot, Inc.'s
15 * copyright in TecIO. Contact Tecplot, Inc. for further information.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
26 #define TECPLOTENGINEMODULE
30 *****************************************************************
31 *****************************************************************
33 ****** Copyright (C) 1988-2008 Tecplot, Inc. *******
35 *****************************************************************
36 *****************************************************************
44 #include "Q_UNICODE.h"
49 /* * SET FUNCTIONS * */
51 #if defined TECPLOTKERNEL
52 /* CORE SOURCE CODE REMOVED */
53 #if InitNumZones > InitNumVars
56 #if ZoneExpansionFactor > VarExpansionFactor
60 #define SetInitSize (PadOut(1,SetBitSize))
61 #define SetExpansionFactor 2
64 using namespace tecplot::strutil
;
68 Set_pa
AllocSet(Boolean_t show_error_msg
)
70 Set_pa Set
= ALLOC_ITEM(struct _Set_a
, "Set header");
73 Set
->size
= SetInitSize
;
74 Set
->data
= ALLOC_ARRAY(SetInitSize
/ SetBitSize
, SetData_t
, "Set data");
75 if (Set
->data
== NULL
)
80 if ((Set
== NULL
) && show_error_msg
)
82 # if defined TECPLOTKERNEL
83 /* CORE SOURCE CODE REMOVED */
85 fprintf(stderr
, "Out of memory for sets");
94 void DeallocSet(Set_pa
*Set
)
99 FREE_ARRAY((*Set
)->data
, "Set data");
100 FREE_ITEM(*Set
, "Set header");
107 * This function adapts the DeallocSet function to work with the
108 * ArrayList's deallocation callback.
110 Boolean_t
SetItemDestructor(void *ItemRef
,
111 ArbParam_t ClientData
)
113 Set_pa
*SetRef
= (Set_pa
*)ItemRef
;
115 REQUIRE(VALID_REF(SetRef
));
116 REQUIRE(VALID_REF(*SetRef
) || *SetRef
== NULL
);
121 ENSURE(*SetRef
== NULL
);
128 Boolean_t
ExpandSet(Set_pa Set
,
130 Boolean_t show_error_msg
)
135 REQUIRE(max_val
>= 0);
141 # if defined TECPLOTKERNEL
142 /* CORE SOURCE CODE REMOVED */
144 fprintf(stderr
, "Null Set expand");
150 if (max_val
<= Set
->size
)
153 new_size
= Set
->size
;
154 while (new_size
< max_val
)
155 new_size
*= SetExpansionFactor
;
157 new_size
= PadOut(new_size
, SetBitSize
);
159 data
= ALLOC_ARRAY(new_size
/ SetBitSize
, SetData_t
, "new Set data");
165 # if defined TECPLOTKERNEL
166 /* CORE SOURCE CODE REMOVED */
168 fprintf(stderr
, "Out of memory for sets");
173 size_t old_set_size_in_bytes
= sizeof(data
[0]) * (Set
->size
/ SetBitSize
);
174 memcpy(data
, Set
->data
, old_set_size_in_bytes
);
176 size_t new_set_size_in_bytes
= sizeof(data
[0]) * (new_size
/ SetBitSize
);
177 size_t numBytesToReset
= new_set_size_in_bytes
- old_set_size_in_bytes
;
178 memset(((char*)data
) + old_set_size_in_bytes
, 0, numBytesToReset
);
180 FREE_ARRAY(Set
->data
, "old Set data");
182 Set
->size
= new_size
;
189 Boolean_t
CopySet(Set_pa dst
,
191 Boolean_t show_error_msg
)
193 if (dst
&& dst
->data
&&
195 ExpandSet(dst
, src
->size
, show_error_msg
))
197 SetIndex_t src_size_in_words
= src
->size
/ SetBitSize
;
198 size_t numBytesToCopy
= sizeof(dst
->data
[0]) * src_size_in_words
;
199 memcpy(dst
->data
, src
->data
, numBytesToCopy
);
201 SetIndex_t dst_size_in_words
= dst
->size
/ SetBitSize
;
202 CHECK(dst_size_in_words
>=src_size_in_words
); // ...guaranteed by above ExpandSet() call
203 size_t numBytesToReset
= sizeof(dst
->data
[0]) * (dst_size_in_words
- src_size_in_words
);
204 memset((char*)(dst
->data
+ src_size_in_words
), 0, numBytesToReset
);
215 Boolean_t
AppendSet(Set_pa dst
,
217 Boolean_t show_error_msg
)
219 if (dst
&& dst
->data
&&
223 ForAllMembersInSet(member
, src
)
225 if (!AddToSet(dst
, member
, TRUE
))
237 void ClearSet(Set_pa Set
)
239 if (Set
&& Set
->data
)
240 memset(Set
->data
, 0, Set
->size
/ SetBitSize
* sizeof(Set
->data
[0]));
244 #if defined USE_FUNCTIONS_FOR_SETS
247 Boolean_t
AddToSet(Set_pa Set
,
249 Boolean_t show_error_msg
)
251 REQUIRE(member
>= 0);
254 ((member
+ 1 <= Set
->size
) || ExpandSet(Set
, member
+ 1, show_error_msg
)))
256 SetIndex_t word
= member
/ SetBitSize
;
257 SetData_t bit
= (SetData_t
)1 << (member
% SetBitSize
);
258 Set
->data
[word
] |= bit
;
269 void RemoveFromSet(Set_pa Set
,
272 REQUIRE(member
>= 0);
273 if (Set
&& (member
< Set
->size
) && Set
->data
)
275 SetIndex_t word
= member
/ SetBitSize
;
276 SetData_t bit
= (SetData_t
)1 << (member
% SetBitSize
);
277 Set
->data
[word
] &= (((SetData_t
) - 1) ^ bit
);
279 } /* RemoveFromSet() */
283 * Similar to RemoveFromSet except it shifts the Set.
285 void DeleteSetMember(Set_pa Set
,
288 SetIndex_t LastMember
;
290 REQUIRE(VALID_REF(Set
));
291 REQUIRE(Member
>= 0);
293 LastMember
= GetPrevMember(Set
, BAD_SET_VALUE
);
294 if (Member
<= LastMember
)
296 ShiftSet(Set
, Member
+ 1, LastMember
, -1);
297 RemoveFromSet(Set
, LastMember
);
303 * Similar to AddToSet except that if the new member is within the currently
304 * defined set the members are shifted accordingly.
306 Boolean_t
InsertSetMember(Set_pa Set
,
308 Boolean_t ShowErrMsg
)
310 Boolean_t IsOk
= TRUE
;
311 SetIndex_t OrigLastMember
;
313 REQUIRE(VALID_REF(Set
));
315 /* first, determine if we need to shift the set */
316 OrigLastMember
= GetPrevMember(Set
, BAD_SET_VALUE
);
317 if (Member
<= OrigLastMember
)
319 IsOk
= ExpandSet(Set
, (OrigLastMember
+ 1) + 1, ShowErrMsg
);
320 ShiftSet(Set
, Member
, OrigLastMember
, 1);
324 IsOk
= AddToSet(Set
, Member
, ShowErrMsg
);
326 ENSURE(VALID_BOOLEAN(IsOk
));
330 #if defined USE_FUNCTIONS_FOR_SETS
333 Boolean_t
InSet(Set_pa Set
,
337 * Sometimes InSet is called with negative numbers. This is not correct, but
338 * its what we have to work with. Maybe some day, we can make this assertion.
341 if (Set
&& (0 <= member
&& member
< Set
->size
))
343 SetIndex_t word
= member
/ SetBitSize
;
344 SetData_t bit
= (SetData_t
)1 << (member
% SetBitSize
);
345 return (Set
->data
[word
]&bit
) != 0;
355 Boolean_t
IsEmpty(Set_pa Set
)
357 if (Set
&& Set
->data
)
359 SetIndex_t set_size_in_words
= Set
->size
/ SetBitSize
;
361 for (word
= 0; word
< set_size_in_words
; word
++)
362 if (Set
->data
[word
] != 0)
371 Boolean_t
HasVoids(Set_pa Set
)
373 Boolean_t Result
= FALSE
;
374 SetIndex_t ContiguousMember
= 0;
375 SetIndex_t Member
= 0;
377 REQUIRE(VALID_REF(Set
));
379 /* look for voids in the set */
380 ForAllMembersInSet(Member
, Set
)
382 if (Member
== ContiguousMember
)
393 ENSURE(VALID_BOOLEAN(Result
));
400 SetIndex_t
MemberCount(Set_pa Set
)
402 SetIndex_t count
= 0;
403 if (Set
&& Set
->data
)
405 SetIndex_t set_size_in_words
= Set
->size
/ SetBitSize
;
407 for (word
= 0; word
< set_size_in_words
; word
++)
409 SetData_t word_val
= Set
->data
[word
];
414 word_val
= word_val
>> 1;
419 } /* MemberCount() */
424 SetIndex_t
GetNextMember(Set_pa Set
,
427 SetIndex_t next_member
= BAD_SET_VALUE
;
428 if (Set
&& Set
->data
)
430 SetIndex_t set_size_in_words
= Set
->size
/ SetBitSize
;
432 SetData_t word_val
= 0;
434 if (start_at
== BAD_SET_VALUE
)
438 if (word
< set_size_in_words
)
439 word_val
= Set
->data
[0];
441 else if (start_at
+ 1 < Set
->size
)
443 word
= (start_at
+ 1) / SetBitSize
;
444 bit
= (start_at
+ 1) % SetBitSize
;
445 if (word
< set_size_in_words
)
446 word_val
= Set
->data
[word
] >> bit
;
450 return BAD_SET_VALUE
;
452 while ((word
< set_size_in_words
) && (word_val
== 0))
456 if (word
< set_size_in_words
)
457 word_val
= Set
->data
[word
];
459 if (word
< set_size_in_words
)
461 while (!(word_val
&1))
466 next_member
= word
* SetBitSize
+ bit
;
470 } /* GetNextMember() */
475 SetIndex_t
GetPrevMember(Set_pa Set
,
478 SetIndex_t next_member
= BAD_SET_VALUE
;
479 if (Set
&& Set
->data
)
481 SetIndex_t set_size_in_words
= Set
->size
/ SetBitSize
;
483 SetData_t word_val
= 0;
485 if (start_at
== BAD_SET_VALUE
)
487 word
= set_size_in_words
- 1;
488 bit
= SetBitSize
- 1;
490 word_val
= Set
->data
[word
];
492 else if (start_at
> 0)
494 word
= (start_at
- 1) / SetBitSize
;
495 bit
= (start_at
- 1) % SetBitSize
;
497 word_val
= Set
->data
[word
] << (SetBitSize
- bit
- 1);
501 return BAD_SET_VALUE
;
503 while ((word
>= 0) && (word_val
== 0))
506 bit
= SetBitSize
- 1;
508 word_val
= Set
->data
[word
] << (SetBitSize
- bit
- 1);
512 while (!(word_val
&SetLastBit
))
517 next_member
= word
* SetBitSize
+ bit
;
521 } /* GetPrevMember() */
526 Boolean_t
EqualSets(Set_pa set1
,
529 SetIndex_t set1_size_in_words
,
531 min_set_size_in_words
,
536 set1_size_in_words
= set1
->size
/ SetBitSize
;
537 set2_size_in_words
= set2
->size
/ SetBitSize
;
538 min_set_size_in_words
= MIN(set1_size_in_words
, set2_size_in_words
);
540 for (ii
= 0; ii
< min_set_size_in_words
; ii
++)
541 if (set1
->data
[ii
] != set2
->data
[ii
])
543 for (ii
= min_set_size_in_words
; ii
< set1_size_in_words
; ii
++)
544 if (set1
->data
[ii
] != 0)
546 for (ii
= min_set_size_in_words
; ii
< set2_size_in_words
; ii
++)
547 if (set2
->data
[ii
] != 0)
555 Boolean_t
IsSubSet(Set_pa childset
,
560 ForAllMembersInSet(s
, childset
)
562 if (!InSet(parentset
, s
))
576 * functions added 11/7 by byron. These are roughed in for now and could
577 * stand to be optimized later.....
581 * Return the number of members in a set that preceed a given member.
584 SetIndex_t
MemberOffset(Set_pa Set
,
588 SetIndex_t Offset
= -1;
589 if (InSet(Set
, Member
))
591 for (I
= 0; I
<= Member
; I
++)
601 * Return the position in the set of the nth member of a set.
603 SetIndex_t
OffsetMember(Set_pa Set
,
607 SetIndex_t Member
= BAD_SET_VALUE
;
608 for (I
= 0; I
<= Offset
; I
++)
610 Member
= GetNextMember(Set
, Member
);
611 if (Member
== BAD_SET_VALUE
)
617 Boolean_t
CopySetMember(Set_pa DstSet
,
618 SetIndex_t DstOffset
,
620 SetIndex_t SrcOffset
)
622 if (InSet(SrcSet
, SrcOffset
))
623 return (AddToSet(DstSet
, DstOffset
, TRUE
));
625 RemoveFromSet(DstSet
, DstOffset
);
633 * v---ShiftPos1 v--ShiftPos2
634 * +-------------------------------------+
635 * | | | | | | | |x| | | | | | | |x| | | |
636 * +-------------------------------------+
639 * v---ShiftPos1 v--ShiftPos2
640 * +-------------------------------------+
641 * | | | | | | | | | |x| | | | | | | |x| |
642 * +-------------------------------------+
645 * Shift all bits between ShiftPos1 and ShiftPos2
646 * by ShiftAmount. The bits that the shift
647 * replaces fill in the hole left by the shift
651 void ShiftSet(Set_pa Set
,
652 SetIndex_t ShiftPos1
,
653 SetIndex_t ShiftPos2
,
654 SetIndex_t ShiftAmount
)
660 if ((Set
== NULL
) || (IsEmpty(Set
)))
663 NewSet
= AllocSet(TRUE
);
668 if (!CopySet(NewSet
, Set
, TRUE
))
674 SPos
= ShiftPos1
- 1;
675 while (DPos
> ShiftPos2
+ ShiftAmount
)
676 CopySetMember(NewSet
, DPos
--, Set
, SPos
--);
678 while (SPos
>= ShiftPos1
)
679 CopySetMember(NewSet
, DPos
--, Set
, SPos
--);
681 else if (ShiftAmount
> 0)
684 SPos
= ShiftPos2
+ 1;
685 while (DPos
< ShiftPos1
+ ShiftAmount
)
686 CopySetMember(NewSet
, DPos
++, Set
, SPos
++);
688 while (SPos
<= ShiftPos2
)
689 CopySetMember(NewSet
, DPos
++, Set
, SPos
++);
691 CopySet(Set
, NewSet
, TRUE
);