foam to Tecplot360 converter
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / dataConversion / foamToTecplot360 / tecio / tecsrc / set.cpp
blob2f6252a4c9893a028d15cc535c20572e53ee46ea
1 /*
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.
17 * Tecplot, Inc.
18 * 3535 Factoria Blvd, Ste. 550
19 * Bellevue, WA 98006, USA
20 * Phone: +1 425 653 1200
21 * http://www.tecplot.com/
24 #include "stdafx.h"
25 #include "MASTER.h"
26 #define TECPLOTENGINEMODULE
30 *****************************************************************
31 *****************************************************************
32 ******* ********
33 ****** Copyright (C) 1988-2008 Tecplot, Inc. *******
34 ******* ********
35 *****************************************************************
36 *****************************************************************
41 #define SETMODULE
42 #include "GLOBAL.h"
43 #include "TASSERT.h"
44 #include "Q_UNICODE.h"
45 #include "ALLOC.h"
46 #include "SET.h"
49 /* * SET FUNCTIONS * */
51 #if defined TECPLOTKERNEL
52 /* CORE SOURCE CODE REMOVED */
53 #if InitNumZones > InitNumVars
54 #else
55 #endif
56 #if ZoneExpansionFactor > VarExpansionFactor
57 #else
58 #endif
59 #else
60 #define SetInitSize (PadOut(1,SetBitSize))
61 #define SetExpansionFactor 2
62 #endif
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");
71 if (Set)
73 Set->size = SetInitSize;
74 Set->data = ALLOC_ARRAY(SetInitSize / SetBitSize, SetData_t, "Set data");
75 if (Set->data == NULL)
76 DeallocSet(&Set);
77 else
78 ClearSet(Set);
80 if ((Set == NULL) && show_error_msg)
82 # if defined TECPLOTKERNEL
83 /* CORE SOURCE CODE REMOVED */
84 # else
85 fprintf(stderr, "Out of memory for sets");
86 # endif
88 return Set;
89 } /* AllocSet() */
94 void DeallocSet(Set_pa *Set)
96 if (Set && *Set)
98 if ((*Set)->data)
99 FREE_ARRAY((*Set)->data, "Set data");
100 FREE_ITEM(*Set, "Set header");
101 *Set = NULL;
103 } /* DeallocSet() */
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);
118 if (*SetRef != NULL)
119 DeallocSet(SetRef);
121 ENSURE(*SetRef == NULL);
122 return TRUE;
128 Boolean_t ExpandSet(Set_pa Set,
129 SetIndex_t max_val,
130 Boolean_t show_error_msg)
132 SetData_t *data;
133 long new_size;
135 REQUIRE(max_val >= 0);
137 if (!Set)
139 if (show_error_msg)
141 # if defined TECPLOTKERNEL
142 /* CORE SOURCE CODE REMOVED */
143 # else
144 fprintf(stderr, "Null Set expand");
145 # endif
147 return FALSE;
150 if (max_val <= Set->size)
151 return TRUE;
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");
161 if (!data)
163 if (show_error_msg)
165 # if defined TECPLOTKERNEL
166 /* CORE SOURCE CODE REMOVED */
167 # else
168 fprintf(stderr, "Out of memory for sets");
169 # endif
171 return FALSE;
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");
181 Set->data = data;
182 Set->size = new_size;
183 return TRUE;
184 } /* ExpandSet() */
189 Boolean_t CopySet(Set_pa dst,
190 Set_pa src,
191 Boolean_t show_error_msg)
193 if (dst && dst->data &&
194 src && src->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);
206 return TRUE;
208 else
209 return FALSE;
210 } /* CopySet() */
215 Boolean_t AppendSet(Set_pa dst,
216 Set_pa src,
217 Boolean_t show_error_msg)
219 if (dst && dst->data &&
220 src && src->data)
222 SetIndex_t member;
223 ForAllMembersInSet(member, src)
225 if (!AddToSet(dst, member, TRUE))
226 return FALSE;
228 return TRUE;
230 else
231 return FALSE;
232 } /* AppendSet() */
237 void ClearSet(Set_pa Set)
239 if (Set && Set->data)
240 memset(Set->data, 0, Set->size / SetBitSize * sizeof(Set->data[0]));
241 } /* ClearSet() */
244 #if defined USE_FUNCTIONS_FOR_SETS
247 Boolean_t AddToSet(Set_pa Set,
248 SetIndex_t member,
249 Boolean_t show_error_msg)
251 REQUIRE(member >= 0);
252 if (Set &&
253 Set->data &&
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;
259 return TRUE;
261 else
262 return FALSE;
263 } /* AddToSet() */
264 #endif
269 void RemoveFromSet(Set_pa Set,
270 SetIndex_t member)
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,
286 SetIndex_t Member)
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,
307 SetIndex_t Member,
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);
323 if (IsOk)
324 IsOk = AddToSet(Set, Member, ShowErrMsg);
326 ENSURE(VALID_BOOLEAN(IsOk));
327 return IsOk;
330 #if defined USE_FUNCTIONS_FOR_SETS
333 Boolean_t InSet(Set_pa Set,
334 SetIndex_t member)
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.
339 REQUIRE(member>=0);
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;
347 else
348 return FALSE;
349 } /* InSet() */
350 #endif
355 Boolean_t IsEmpty(Set_pa Set)
357 if (Set && Set->data)
359 SetIndex_t set_size_in_words = Set->size / SetBitSize;
360 SetIndex_t word;
361 for (word = 0; word < set_size_in_words; word++)
362 if (Set->data[word] != 0)
363 return FALSE;
365 return TRUE;
366 } /* IsEmpty() */
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)
384 ContiguousMember++;
386 else
388 Result = TRUE;
389 break;
393 ENSURE(VALID_BOOLEAN(Result));
394 return 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;
406 SetIndex_t word;
407 for (word = 0; word < set_size_in_words; word++)
409 SetData_t word_val = Set->data[word];
410 while (word_val)
412 if (word_val&1)
413 count++;
414 word_val = word_val >> 1;
418 return count;
419 } /* MemberCount() */
424 SetIndex_t GetNextMember(Set_pa Set,
425 SetIndex_t start_at)
427 SetIndex_t next_member = BAD_SET_VALUE;
428 if (Set && Set->data)
430 SetIndex_t set_size_in_words = Set->size / SetBitSize;
431 SetIndex_t word;
432 SetData_t word_val = 0;
433 int bit;
434 if (start_at == BAD_SET_VALUE)
436 word = 0;
437 bit = 0;
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;
448 else
450 return BAD_SET_VALUE;
452 while ((word < set_size_in_words) && (word_val == 0))
454 word++;
455 bit = 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))
463 word_val >>= 1;
464 bit++;
466 next_member = word * SetBitSize + bit;
469 return next_member;
470 } /* GetNextMember() */
475 SetIndex_t GetPrevMember(Set_pa Set,
476 SetIndex_t start_at)
478 SetIndex_t next_member = BAD_SET_VALUE;
479 if (Set && Set->data)
481 SetIndex_t set_size_in_words = Set->size / SetBitSize;
482 SetIndex_t word;
483 SetData_t word_val = 0;
484 int bit;
485 if (start_at == BAD_SET_VALUE)
487 word = set_size_in_words - 1;
488 bit = SetBitSize - 1;
489 if (word >= 0)
490 word_val = Set->data[word];
492 else if (start_at > 0)
494 word = (start_at - 1) / SetBitSize;
495 bit = (start_at - 1) % SetBitSize;
496 if (word >= 0)
497 word_val = Set->data[word] << (SetBitSize - bit - 1);
499 else
501 return BAD_SET_VALUE;
503 while ((word >= 0) && (word_val == 0))
505 word--;
506 bit = SetBitSize - 1;
507 if (word >= 0)
508 word_val = Set->data[word] << (SetBitSize - bit - 1);
510 if (word >= 0)
512 while (!(word_val&SetLastBit))
514 word_val <<= 1;
515 bit--;
517 next_member = word * SetBitSize + bit;
520 return next_member;
521 } /* GetPrevMember() */
526 Boolean_t EqualSets(Set_pa set1,
527 Set_pa set2)
529 SetIndex_t set1_size_in_words,
530 set2_size_in_words,
531 min_set_size_in_words,
533 if (!set1 || !set2)
534 return FALSE;
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])
542 return FALSE;
543 for (ii = min_set_size_in_words; ii < set1_size_in_words; ii++)
544 if (set1->data[ii] != 0)
545 return FALSE;
546 for (ii = min_set_size_in_words; ii < set2_size_in_words; ii++)
547 if (set2->data[ii] != 0)
548 return FALSE;
550 return TRUE;
552 } /* EqualSets() */
555 Boolean_t IsSubSet(Set_pa childset,
556 Set_pa parentset)
558 SetIndex_t s;
560 ForAllMembersInSet(s, childset)
562 if (!InSet(parentset, s))
563 return (FALSE);
566 return (TRUE);
568 } /* IsSubSet() */
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,
585 SetIndex_t Member)
587 SetIndex_t I;
588 SetIndex_t Offset = -1;
589 if (InSet(Set, Member))
591 for (I = 0; I <= Member; I++)
593 if (InSet(Set, I))
594 Offset++;
597 return (Offset);
601 * Return the position in the set of the nth member of a set.
603 SetIndex_t OffsetMember(Set_pa Set,
604 SetIndex_t Offset)
606 SetIndex_t I;
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)
612 break;
614 return (Member);
617 Boolean_t CopySetMember(Set_pa DstSet,
618 SetIndex_t DstOffset,
619 Set_pa SrcSet,
620 SetIndex_t SrcOffset)
622 if (InSet(SrcSet, SrcOffset))
623 return (AddToSet(DstSet, DstOffset, TRUE));
624 else
625 RemoveFromSet(DstSet, DstOffset);
626 return (TRUE);
632 * Initial:
633 * v---ShiftPos1 v--ShiftPos2
634 * +-------------------------------------+
635 * | | | | | | | |x| | | | | | | |x| | | |
636 * +-------------------------------------+
638 * Shift +2
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)
656 Set_pa NewSet;
657 SetIndex_t DPos;
658 SetIndex_t SPos;
660 if ((Set == NULL) || (IsEmpty(Set)))
661 return;
663 NewSet = AllocSet(TRUE);
665 if (NewSet == NULL)
666 return;
668 if (!CopySet(NewSet, Set, TRUE))
669 return;
671 if (ShiftAmount < 0)
673 DPos = ShiftPos2;
674 SPos = ShiftPos1 - 1;
675 while (DPos > ShiftPos2 + ShiftAmount)
676 CopySetMember(NewSet, DPos--, Set, SPos--);
677 SPos = ShiftPos2;
678 while (SPos >= ShiftPos1)
679 CopySetMember(NewSet, DPos--, Set, SPos--);
681 else if (ShiftAmount > 0)
683 DPos = ShiftPos1;
684 SPos = ShiftPos2 + 1;
685 while (DPos < ShiftPos1 + ShiftAmount)
686 CopySetMember(NewSet, DPos++, Set, SPos++);
687 SPos = ShiftPos1;
688 while (SPos <= ShiftPos2)
689 CopySetMember(NewSet, DPos++, Set, SPos++);
691 CopySet(Set, NewSet, TRUE);
692 DeallocSet(&NewSet);