-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfbtBlend.h
5864 lines (4723 loc) · 401 KB
/
fbtBlend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
-------------------------------------------------------------------------------
This file is part of FBT (File Binary Tables).
http://gamekit.googlecode.com/
Copyright (c) 2010 Charlie C & Erwin Coumans.
-------------------------------------------------------------------------------
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
-------------------------------------------------------------------------------
*/
/*
Slightly modified by Flix01 (same license) in the following way:
-> Now fbtBlend.h is header-only (needs FBTBLEND_IMPLEMENTATION before including it in a single .cpp file).
-> So now only fbtBlend.h and Blender.h can be used to parse a .blend file.
-> Updated Blender Version to 279 (64bit, little endian).
-> Removed all the compilation warning gcc could find.
-> FBT_HAVE_CONFIG now has its meaning reversed: if you want to include "fbtCnfig.h" you must define it.
-> Added int parse(const char* path);
-> Remember to #define FBT_USE_GZ_FILE 1, if you want to support PM_COMPRESSED .blend files (you need to link to zlib: -lz).
-> Added workaround for people just defining FBT_USE_GZ_FILE, without setting it to 1.
-> In fbtFile, the method(s) to parse from file are:
-> Before:
int parse(const char* path, int mode = PM_UNCOMPRESSED);
-> Now:
int parse(const char* path,const char* uncompressedFileDetectorPrefix="BLENDER");
int parse(const char* path, int mode);
// This allows auto-detection of the .blend file compression, by just using: parse(const char* path);
-> Added a public static helper method fbtFile::UTF8_fopen(...) (used internally to to allow UTF8 char paths on Windows).
-> Minor changes made to class fbtArray<...> and its iterator (but code is still backward-compatible).
-> Added class fbtString (not used here), as a replacement for std::string.
-> Added support for zstd decompression through #define FBT_USE_ZSTD (it needs linking to -lzstd).
-> Updated the "ID enums" (please search: [DNA_ID_enums 1/2] and [DNA_ID_enums 2/2]).
*/
#ifndef _fbtBlend_h_
#define _fbtBlend_h_
//#include "fbtFile.h"
#ifndef _fbtFile_h_
#define _fbtFile_h_
// #include "fbtTypes.h"
#ifndef _fbtTypes_h_
#define _fbtTypes_h_
#ifndef FBT_HAVE_CONFIG
// global config settings
//#define FBT_USE_GZ_FILE 1 // Adds support to PM_COMPRESSED .blend < 3.00 files (needs linking to zlib: -lz)
//#define FBT_USE_ZSTD_FILE 1 // Adds support to PM_COMPRESSED .blend 3.00 files (needs linking to zlib: -lzstd)
#define fbtDEBUG 1 // Traceback detail
//#define FBT_TYPE_LEN_VALIDATE 1 // Write a validation file (use MakeFBT.cmake->ADD_FBT_VALIDATOR to add a self validating build)
// global config settings end
#else
#include "fbtConfig.h"
#endif
// Workaround for people just defining FBT_USE_GZ_FILE, without setting it to 1
#ifdef FBT_USE_GZ_FILE
# undef FBT_USE_GZ_FILE
# define FBT_USE_GZ_FILE 1
#endif
// Workaround for people just defining FBT_USE_ZSTD_FILE, without setting it to 1
#ifdef FBT_USE_ZSTD_FILE
# undef FBT_USE_ZSTD_FILE
# define FBT_USE_ZSTD_FILE 1
#endif
#include <string.h> //memcmp
#include "Blender.h" // THIS FILE DEPENDS ON THE BLENDER VERSION (TOGETHER WITH bfBlender.cpp THAT'S AT THE BOTTOM OF THIS FILE)
// global config settings
#ifndef fbtMaxTable
# if BLENDER_VERSION<306
# define fbtMaxTable 5000 // Maximum number of elements in a table
# else
# define fbtMaxTable 6000 // Maximum number of elements in a table
# endif
#endif
#ifndef fbtMaxID
# define fbtMaxID 64 // Maximum character array length
#endif
#ifndef fbtMaxMember
# define fbtMaxMember 256 // Maximum number of members in a struct or class.
#endif
#ifndef fbtDefaultAlloc
# define fbtDefaultAlloc 2048 // Table default allocation size
#endif
#ifndef FBT_ARRAY_SLOTS
# if BLENDER_VERSION>279
# define FBT_ARRAY_SLOTS 3 // Maximum dimensional array, eg: (int m_member[..][..] -> [FBT_ARRAY_SLOTS])
# else
# define FBT_ARRAY_SLOTS 2 // Maximum dimensional array, eg: (int m_member[..][..] -> [FBT_ARRAY_SLOTS])
# endif
#endif
// global config settings end
/** \addtogroup FBT
* @{
*/
#if (defined(DEBUG) || defined(_DEBUG)) && FBT_USE_DEBUG_ASSERTS == 1
# include <assert.h> // Keep this the only std include in headers
# define FBT_DEBUG 1
# define FBT_ASSERT(x) assert(x)
#else
# define FBT_ASSERT(x) ((void)0)
#endif
#ifdef FBT_USE_COMPILER_CHECKS
#define FBT_ASSERTCOMP(x, n) typedef bool x[(n) ? 1 : -1];
#else
#define FBT_ASSERTCOMP(x, n)
#endif
#define FBT_PLATFORM_WIN32 0
#define FBT_PLATFORM_LINUX 2
#define FBT_PLATFORM_APPLE 3
#if defined (_WIN32)
# define FBT_PLATFORM FBT_PLATFORM_WIN32
#elif defined(__APPLE__)
# define FBT_PLATFORM FBT_PLATFORM_APPLE
#else
# define FBT_PLATFORM FBT_PLATFORM_LINUX
#endif
#define FBT_COMPILER_MSVC 0
#define FBT_COMPILER_GNU 1
#if defined(_MSC_VER)
# define FBT_COMPILER FBT_COMPILER_MSVC
#elif defined(__GNUC__)
# define FBT_COMPILER FBT_COMPILER_GNU
#else
# error unknown compiler
#endif
#define FBT_ENDIAN_LITTLE 0
#define FBT_ENDIAN_BIG 1
#if defined(__sgi) || defined (__sparc) || \
defined (__sparc__) || defined (__PPC__) || \
defined (__ppc__) || defined (__BIG_ENDIAN__)
#define FBT_ENDIAN FBT_ENDIAN_BIG
#else
#define FBT_ENDIAN FBT_ENDIAN_LITTLE
#endif
#if FBT_ENDIAN == FBT_ENDIAN_BIG
# define FBT_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
# define FBT_ID2(c, d) ( (c)<<8 | (d) )
#else
# define FBT_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
# define FBT_ID2(c, d) ( (d)<<8 | (c) )
#endif
#define FBT_ARCH_32 0
#define FBT_ARCH_64 1
#if defined(__x86_64__) || defined(_M_X64) || \
defined(__powerpc64__) || defined(__alpha__) || \
defined(__ia64__) || defined(__s390__) || \
defined(__s390x__) || defined(__aarch64__)
#define FBT_ARCH FBT_ARCH_64
#else
#define FBT_ARCH FBT_ARCH_32
#endif
#if FBT_PLATFORM == FBT_PLATFORM_WIN32
# if defined(__MINGW32__) || \
defined(__CYGWIN__) || \
(defined (_MSC_VER) && _MSC_VER < 1300)
# define FBT_INLINE inline
# else
# define FBT_INLINE __forceinline
# endif
#else
# define FBT_INLINE inline
#endif
// Integer types
typedef long FBTlong;
typedef unsigned long FBTulong;
typedef int FBTint32;
typedef unsigned int FBTuint32;
typedef short FBTint16;
typedef unsigned short FBTuint16;
typedef signed char FBTint8;
typedef unsigned char FBTuint8;
typedef unsigned char FBTubyte;
typedef signed char FBTbyte;
typedef bool FBTint1;
#if FBT_COMPILER == FBT_COMPILER_GNU
typedef long long FBTint64;
typedef unsigned long long FBTuint64;
#else
typedef __int64 FBTint64;
typedef unsigned __int64 FBTuint64;
#endif
// Arch dependent types
#if FBT_ARCH == FBT_ARCH_64
typedef FBTuint64 FBTuintPtr;
typedef FBTint64 FBTintPtr;
#else
typedef FBTuint32 FBTuintPtr;
typedef FBTint32 FBTintPtr;
#endif
typedef FBTuintPtr FBTsize;
// Type for arrays & tables (Always unsigned & 32bit)
typedef FBTuint32 FBTsizeType;
const FBTuint32 FBT_NPOS = (FBTuint32) - 1;
typedef FBTuint32 FBThash;
typedef FBTuint16 FBTtype;
FBT_ASSERTCOMP(FBTsizeType_NPOS_VALIDATE, FBT_NPOS == ((FBTuint32) - 1));
#define _FBT_CACHE_LIMIT 999
template <typename T> FBT_INLINE void fbtSwap(T& a, T& b) { T t(a); a = b; b = t; }
template <typename T> FBT_INLINE T fbtMax(const T& a, const T& b) { return a < b ? b : a; }
template <typename T> FBT_INLINE T fbtMin(const T& a, const T& b) { return a < b ? a : b; }
template <typename T> FBT_INLINE T fbtClamp(const T& v, const T& a, const T& b) { return v < a ? a : v > b ? b : v; }
#define fbtMalloc(size) ::malloc(size)
#define fbtCalloc(size, len) ::calloc(size, len)
#define fbtFree(ptr) ::free(ptr)
#define fbtMemset ::memset
#define fbtMemcpy ::memcpy
#define fbtMemmove ::memmove
#define fbtMemcmp ::memcmp
class fbtDebugger
{
public:
typedef void (*ReportHook) (FBTuintPtr client, const char* buffer);
struct Reporter
{
FBTuintPtr m_client;
ReportHook m_hook;
};
static bool isDebugger(void);
static void reportIDE(const char* src, long line, const char* msg, ...);
static void errorIDE(const char* src, long line, const char* msg, ...);
static void report(const char* msg, ...);
static void breakProcess(void);
static void setReportHook(Reporter& hook);
private:
static Reporter m_report;
};
#define fbtPrintf fbtDebugger::report
#define fbtTRACE fbtDebugger::reportIDE
#define fbtERROR fbtDebugger::errorIDE
typedef enum fbtEndian
{
FBT_ENDIAN_IS_BIG = 0,
FBT_ENDIAN_IS_LITTLE = 1,
FBT_ENDIAN_NATIVE,
} fbtEndian;
typedef union fbtEndianTest
{
FBTbyte bo[4];
FBTint32 test;
} fbtEndianTest;
FBT_INLINE fbtEndian fbtGetEndian(void)
{
fbtEndianTest e;
e.test = FBT_ENDIAN_IS_LITTLE;
return static_cast<fbtEndian>(e.bo[0]);
}
FBT_INLINE bool fbtIsEndian(const fbtEndian& endian)
{
fbtEndianTest e;
e.test = endian;
return static_cast<fbtEndian>(e.bo[0]) == endian;
}
FBT_INLINE FBTuint16 fbtSwap16(FBTuint16 in)
{
return static_cast<FBTuint16>(((in & 0xFF00) >> 8) | ((in & 0x00FF) << 8));
}
FBT_INLINE FBTuint32 fbtSwap32(const FBTuint32& in)
{
return (((in & 0xFF000000) >> 24) | ((in & 0x00FF0000) >> 8) | ((in & 0x0000FF00) << 8) | ((in & 0x000000FF) << 24));
}
FBT_INLINE FBTint16 fbtSwap16(FBTint16 in)
{
return fbtSwap16(static_cast<FBTuint16>(in));
}
FBT_INLINE FBTint32 fbtSwap32(const FBTint32& in)
{
return fbtSwap32(static_cast<FBTuint32>(in));
}
FBT_INLINE FBTuint64 fbtSwap64(const FBTuint64& in)
{
FBTuint64 r = 0;
const FBTubyte* src = reinterpret_cast<const FBTubyte*>(&in);
FBTubyte* dst = reinterpret_cast<FBTubyte*>(&r);
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
return r;
}
FBT_INLINE void fbtSwap16(FBTuint16* sp, FBTsize len)
{
FBTsizeType i;
for (i = 0; i < len; ++i)
{
*sp = fbtSwap16(*sp);
++sp;
}
}
FBT_INLINE void fbtSwap32(FBTuint32* ip, FBTsize len)
{
FBTsizeType i;
for (i = 0; i < len; ++i)
{
*ip = fbtSwap32(*ip);
++ip;
}
}
FBT_INLINE void fbtSwap64(FBTuint64* dp, FBTsize len)
{
FBTsizeType i;
for (i = 0; i < len; ++i)
{
*dp = fbtSwap64(*dp);
++dp;
}
}
FBT_INLINE FBTint64 fbtSwap64(const FBTint64& in)
{
return fbtSwap64(static_cast<FBTuint64>(in));
}
enum fbtPointerLen
{
FBT_VOID = sizeof(void*),
FBT_VOID4 = FBT_VOID == 4,
FBT_VOID8 = FBT_VOID == 8,
};
#if FBT_ARCH == FBT_ARCH_64
FBT_ASSERTCOMP(FBT_VOID8_ASSERT, FBT_VOID8);
#else
FBT_ASSERTCOMP(FBT_VOID4_ASSERT, FBT_VOID4);
#endif
class fbtList
{
public:
struct Link
{
Link* next;
Link* prev;
};
Link* first;
Link* last;
public:
fbtList() : first(0), last(0) {}
~fbtList() { clear(); }
void clear(void) { first = last = 0; }
void push_back(void* v)
{
Link* link = ((Link*)v);
if (!link)
return;
link->prev = last;
if (last)
last->next = link;
if (!first)
first = link;
last = link;
}
};
template <typename T>
class fbtArrayIterator
{
public:
typedef typename T::Pointer Iterator;
typedef typename T::ReferenceType ValueType;
typedef typename T::ConstReferenceType ConstValueType;
protected:
mutable Iterator m_iterator;
mutable FBTsizeType m_cur;
mutable FBTsizeType m_capacity;
public:
fbtArrayIterator() : m_iterator(0), m_cur(0), m_capacity(0) {}
fbtArrayIterator(Iterator begin, FBTsizeType size) : m_iterator(begin), m_cur(0), m_capacity(size) {}
fbtArrayIterator(T& v) : m_iterator(v.ptr()), m_cur(0), m_capacity(v.size()) { }
fbtArrayIterator(T& v,FBTsizeType cur) : m_iterator(v.ptr()), m_cur(cur), m_capacity(v.size()) { }
~fbtArrayIterator() {}
FBT_INLINE bool hasMoreElements(void) const { return m_iterator && m_cur < m_capacity; }
FBT_INLINE ValueType getNext(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur++]; }
FBT_INLINE ConstValueType getNext(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur++]; }
FBT_INLINE void next(void) const { FBT_ASSERT(hasMoreElements()); ++m_cur; }
FBT_INLINE ValueType peekNext(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur]; }
FBT_INLINE ConstValueType peekNext(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur]; }
FBT_INLINE bool operator==(const fbtArrayIterator& rhs) const {return (m_iterator==rhs.m_iterator && m_cur==rhs.m_cur && m_capacity==rhs.m_capacity);} // default should work
FBT_INLINE static fbtArrayIterator Find(const fbtArrayIterator& start,const fbtArrayIterator& finish,ConstValueType v) {
FBT_ASSERT(start.m_iterator==finish.m_iterator);
FBT_ASSERT(start.m_capacity==finish.m_capacity);
FBT_ASSERT(start.m_cur<finish.m_cur);
for (FBTsizeType i = start.m_cur; i < finish.m_cur; i++) {
if (start.m_iterator[i] == v) return fbtArrayIterator(start,i);
}
return fbtArrayIterator(start,finish.m_cur);
}
protected:
fbtArrayIterator(const fbtArrayIterator& rhs,FBTsizeType cur) : m_iterator(rhs.m_iterator), m_cur(cur), m_capacity(rhs.m_capacity) { }
};
template <typename T>
class fbtArray
{
public:
typedef T* Pointer;
typedef const T* ConstPointer;
typedef T ValueType;
typedef const T ConstValueType;
typedef T& ReferenceType;
typedef const T& ConstReferenceType;
typedef fbtArrayIterator<fbtArray<T> > Iterator;
typedef const fbtArrayIterator<fbtArray<T> > ConstIterator;
//typedef Iterator iterator;
//typedef ConstIterator const_Iterator;
public:
fbtArray() : m_size(0), m_capacity(0), m_data(0), m_cache(0) {}
fbtArray(FBTsizeType size) : m_size(0), m_capacity(0), m_data(0), m_cache(0) {resize(size);}
fbtArray(FBTsizeType size,const T& fill) : m_size(0), m_capacity(0), m_data(0), m_cache(0) {resize(size,fill);}
fbtArray(const fbtArray<T>& o)
: m_size(o.size()), m_capacity(0), m_data(0), m_cache(0)
{
reserve(m_size);
copy(m_data, o.m_data, m_size);
}
~fbtArray() { clear(); }
void clear(bool useCache = false)
{
if (!useCache)
{
if (m_data)
delete []m_data;
m_data = 0;
m_capacity = 0;
m_size = 0;
m_cache = 0;
}
else
{
++m_cache;
if (m_cache > _FBT_CACHE_LIMIT )
clear(false);
else m_size = 0;
}
}
FBTsizeType find(const T& v)
{
for (FBTsizeType i = 0; i < m_size; i++)
{
if (m_data[i] == v)
return i;
}
return FBT_NPOS;
}
FBT_INLINE static Iterator Find(const Iterator& start,const Iterator& finish,const ConstValueType& v) {return Iterator::Find(start,finish,v);}
FBT_INLINE void push_back(const T& v)
{
if (m_size == m_capacity) {
const T v_val = v; // Hehe, v can be a reference to old m_data here!
reserve(m_size == 0 ? 8 : (m_size * 2));
m_data[m_size] = v_val;
}
else m_data[m_size] = v;
m_size++;
}
FBT_INLINE void pop_back(void)
{
m_size--;
m_data[m_size].~T();
}
void erase(const T& v)
{
erase(find(v));
}
void erase(FBTsizeType pos)
{
if (m_size > 0)
{
if (pos != FBT_NPOS)
{
swap(pos, m_size - 1);
m_size--;
m_data[m_size].~T();
}
}
}
void resize(FBTsizeType nr)
{
if (nr < m_size)
{
for (FBTsizeType i = m_size; i < nr; i++)
m_data[i].~T();
}
else
{
if (nr > m_size)
reserve(nr);
}
m_size = nr;
}
void resize(FBTsizeType nr, const T& fill)
{
if (nr < m_size)
{
for (FBTsizeType i = m_size; i < nr; i++)
m_data[i].~T();
}
else
{
if (nr > m_size)
reserve(nr);
for (FBTsizeType i = m_size; i < nr; i++)
m_data[i] = fill;
}
m_size = nr;
}
void reserve(FBTsizeType nr)
{
if (m_capacity < nr)
{
T* p = new T[nr];
if (m_data != 0)
{
copy(p, m_data, m_size);
delete []m_data;
}
m_data = p;
m_capacity = nr;
}
}
void sort(bool (*cmp)(const T& a, const T& b))
{
if (m_size > 1 && cmp)
_sort(cmp, 0, m_size - 1);
}
FBT_INLINE T& operator[](FBTsizeType idx) { FBT_ASSERT(idx >= 0 && idx < m_capacity); return m_data[idx]; }
FBT_INLINE const T& operator[](FBTsizeType idx) const { FBT_ASSERT(idx >= 0 && idx < m_capacity); return m_data[idx]; }
FBT_INLINE T& at(FBTsizeType idx) { FBT_ASSERT(idx >= 0 && idx < m_capacity); return m_data[idx]; }
FBT_INLINE const T& at(FBTsizeType idx) const { FBT_ASSERT(idx >= 0 && idx < m_capacity); return m_data[idx]; }
FBT_INLINE T& front(void) { FBT_ASSERT(m_size > 0); return m_data[0]; }
FBT_INLINE T& back(void) { FBT_ASSERT(m_size > 0); return m_data[m_size-1]; }
FBT_INLINE ConstPointer ptr(void) const { return m_data; }
FBT_INLINE Pointer ptr(void) { return m_data; }
FBT_INLINE bool valid(void) const { return m_data != 0;}
FBT_INLINE FBTsizeType capacity(void) const { return m_capacity; }
FBT_INLINE FBTsizeType size(void) const { return m_size; }
FBT_INLINE bool empty(void) const { return m_size == 0;}
FBT_INLINE Iterator iterator(void) { return m_data && m_size > 0 ? Iterator(m_data, m_size) : Iterator(); }
FBT_INLINE ConstIterator iterator(void) const { return m_data && m_size > 0 ? ConstIterator(m_data, m_size) : ConstIterator(); }
FBT_INLINE Iterator begin(void) { return m_data && m_size > 0 ? Iterator(m_data, m_size) : Iterator(); }
FBT_INLINE ConstIterator begin(void) const { return m_data && m_size > 0 ? ConstIterator(m_data, m_size) : ConstIterator(); }
FBT_INLINE Iterator end(void) { return m_data && m_size > 0 ? Iterator(*this, m_size) : Iterator(); }
FBT_INLINE ConstIterator end(void) const { return m_data && m_size > 0 ? ConstIterator(*this, m_size) : ConstIterator(); }
fbtArray<T> &operator= (const fbtArray<T> &rhs)
{
if (this != &rhs)
{
clear();
FBTsizeType os = rhs.size();
if (os > 0)
{
resize(os);
copy(m_data, rhs.m_data, os);
}
}
return *this;
}
FBT_INLINE void copy(Pointer dst, ConstPointer src, FBTsizeType size)
{
FBT_ASSERT(size <= m_size);
for (FBTsizeType i = 0; i < size; i++) dst[i] = src[i];
}
FBT_INLINE bool equal(const fbtArray<T> &rhs)
{
if (rhs.size() != size()) return false;
if (empty()) return true;
return fbtMemcmp(m_data, rhs.m_data, sizeof(T)*m_size) == 0;
}
// Not too sure about this
FBT_INLINE void swap(fbtArray<T> &rhs) {
FBTsizeType rhs_size = rhs.m_size; rhs.m_size = m_size; m_size = rhs_size;
FBTsizeType rhs_cap = rhs.m_capacity; rhs.m_capacity = m_capacity; m_capacity = rhs_cap;
int rhs_cache = rhs.m_cache; rhs.m_cache = m_cache; m_cache = rhs_cache; // Waht's m_cache ?
Pointer rhs_data = rhs.m_data; rhs.m_data = m_data; m_data = rhs_data;
}
protected:
void _sort(bool (*cmp)(const T& a, const T& b), int lo, int hi)
{
// btAlignedObjectArray.h
int i = lo, j = hi;
T x = m_data[(lo+hi)/2];
// partition
do
{
while (cmp(m_data[i], x))
i++;
while (cmp(x, m_data[j]))
j--;
if (i <= j)
{
swap(i, j);
i++; j--;
}
}
while (i <= j);
// recursion
if (lo < j)
_sort( cmp, lo, j);
if (i < hi)
_sort( cmp, i, hi);
}
void swap(FBTsizeType a, FBTsizeType b)
{
ValueType t = m_data[a];
m_data[a] = m_data[b];
m_data[b] = t;
}
FBTsizeType m_size;
FBTsizeType m_capacity;
Pointer m_data;
int m_cache;
};
template <typename T>
class fbtHashTableIterator
{
public:
typedef typename T::Pointer Iterator;
typedef typename T::Entry& Pair;
typedef typename T::ConstEntry& ConstPair;
typedef typename T::ReferenceKeyType KeyType;
typedef typename T::ReferenceValueType ValueType;
typedef typename T::ConstReferenceKeyType ConstKeyType;
typedef typename T::ConstReferenceValueType ConstValueType;
protected:
mutable Iterator m_iterator;
mutable FBTsizeType m_cur;
const FBTsizeType m_capacity;
public:
fbtHashTableIterator() : m_iterator(0), m_cur(0), m_capacity(0) {}
fbtHashTableIterator(Iterator begin, FBTsizeType size) : m_iterator(begin), m_cur(0), m_capacity(size) { }
fbtHashTableIterator(T& v) : m_iterator(v.ptr()), m_cur(0), m_capacity(v.size()) {}
~fbtHashTableIterator() {}
FBT_INLINE bool hasMoreElements(void) const { return (m_iterator && m_cur < m_capacity); }
FBT_INLINE Pair getNext(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur++];}
FBT_INLINE ConstPair getNext(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur++];}
FBT_INLINE void next(void) const { FBT_ASSERT(hasMoreElements()); ++m_cur; }
FBT_INLINE Pair peekNext(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur]; }
FBT_INLINE KeyType peekNextKey(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur].first;}
FBT_INLINE ValueType peekNextValue(void) { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur].second; }
FBT_INLINE ConstPair peekNext(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur]; }
FBT_INLINE ConstKeyType peekNextKey(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur].first;}
FBT_INLINE ConstValueType peekNextValue(void) const { FBT_ASSERT(hasMoreElements()); return m_iterator[m_cur].second; }
};
// magic numbers from http://www.isthe.com/chongo/tech/comp/fnv/
#define _FBT_INITIAL_FNV 0x9E3779B1
#define _FBT_INITIAL_FNV2 0x9E3779B9
#define _FBT_MULTIPLE_FNV 0x1000193
#define _FBT_TWHASH(key) \
key += ~(key << 15); \
key ^= (key >> 10); \
key += (key << 3); \
key ^= (key >> 6); \
key += ~(key << 11); \
key ^= (key >> 16);
class fbtCharHashKey
{
protected:
char* m_key;
mutable FBThash m_hash;
public:
fbtCharHashKey() : m_key(0), m_hash(FBT_NPOS) {}
fbtCharHashKey(char* k) : m_key(k), m_hash(FBT_NPOS) {hash();}
fbtCharHashKey(const char* k) : m_key(const_cast<char*>(k)), m_hash(FBT_NPOS) {}
fbtCharHashKey(const fbtCharHashKey& k) : m_key(k.m_key), m_hash(k.m_hash) { if (m_hash == FBT_NPOS) hash(); }
FBThash hash(void) const
{
if (!m_key) return FBT_NPOS;
if (m_hash != FBT_NPOS) return m_hash;
// Fowler / Noll / Vo (FNV) Hash
m_hash = (FBThash)_FBT_INITIAL_FNV;
for (int i = 0; m_key[i]; i++)
{
m_hash = m_hash ^ (m_key[i]); // xor the low 8 bits
m_hash = m_hash * _FBT_MULTIPLE_FNV; // multiply by the magic number
}
return m_hash;
}
FBT_INLINE bool operator== (const fbtCharHashKey& v) const {return hash() == v.hash();}
FBT_INLINE bool operator!= (const fbtCharHashKey& v) const {return hash() != v.hash();}
FBT_INLINE bool operator== (const FBThash& v) const {return hash() == v;}
FBT_INLINE bool operator!= (const FBThash& v) const {return hash() != v;}
};
class fbtIntHashKey
{
protected:
FBTint32 m_key;
public:
fbtIntHashKey() : m_key(0) {}
fbtIntHashKey(FBTint32 k) : m_key(k) {}
fbtIntHashKey(const fbtIntHashKey& k) : m_key(k.m_key) { }
FBT_INLINE FBThash hash(void) const { return static_cast<FBThash>(m_key) * _FBT_INITIAL_FNV; }
FBT_INLINE bool operator== (const fbtIntHashKey& v) const {return hash() == v.hash();}
FBT_INLINE bool operator!= (const fbtIntHashKey& v) const {return hash() != v.hash();}
FBT_INLINE bool operator== (const FBThash& v) const {return hash() == v;}
FBT_INLINE bool operator!= (const FBThash& v) const {return hash() != v;}
};
//For handing invalid pointers
class fbtSizeHashKey
{
protected:
FBTsize m_key;
mutable FBThash m_hash;
public:
fbtSizeHashKey()
: m_key(0), m_hash(FBT_NPOS)
{
}
fbtSizeHashKey(const FBTsize& k)
: m_key(k), m_hash(FBT_NPOS)
{
hash();
}
fbtSizeHashKey(const fbtSizeHashKey& k)
: m_key(k.m_key), m_hash(FBT_NPOS)
{
hash();
}
FBT_INLINE FBThash hash(void) const
{
if (m_hash != FBT_NPOS)
return m_hash;
m_hash = (FBThash)m_key;
_FBT_TWHASH(m_hash);
return m_hash;
}
FBT_INLINE bool operator== (const fbtSizeHashKey& v) const { return hash() == v.hash();}
FBT_INLINE bool operator!= (const fbtSizeHashKey& v) const { return hash() != v.hash();}
FBT_INLINE bool operator== (const FBThash& v) const { return hash() == v;}
FBT_INLINE bool operator!= (const FBThash& v) const { return hash() != v;}
};
template<typename T>
class fbtTHashKey
{
protected:
T* m_key;
mutable FBThash m_hash;
public:
fbtTHashKey() : m_key(0), m_hash(FBT_NPOS) { hash(); }
fbtTHashKey(T* k) : m_key(k), m_hash(FBT_NPOS) { hash(); }
fbtTHashKey(const fbtTHashKey& k) : m_key(k.m_key), m_hash(k.m_hash) { if (m_hash == FBT_NPOS) hash(); }
FBT_INLINE T* key(void) {return m_key;}
FBT_INLINE const T* key(void) const {return m_key;}
FBT_INLINE FBThash hash(void) const
{
if (m_hash != FBT_NPOS)
return m_hash;
// Yields the least collisions.
m_hash = static_cast<FBThash>(reinterpret_cast<FBTuintPtr>(m_key));
_FBT_TWHASH(m_hash);
return m_hash;
}
FBT_INLINE bool operator== (const fbtTHashKey& v) const { return hash() == v.hash();}
FBT_INLINE bool operator!= (const fbtTHashKey& v) const { return hash() != v.hash();}
FBT_INLINE bool operator== (const FBThash& v) const { return hash() == v;}
FBT_INLINE bool operator!= (const FBThash& v) const { return hash() != v;}
};
typedef fbtTHashKey<void> fbtPointerHashKey;
template<typename Key, typename Value>
struct fbtHashEntry
{
Key first;
Value second;
fbtHashEntry() {}
fbtHashEntry(const Key& k, const Value& v) : first(k), second(v) {}
FBT_INLINE bool operator==(const fbtHashEntry& rhs) const
{
return first == rhs.first && second == rhs.second;
}
};
#define _FBT_UTHASHTABLE_HASH(key) ((key.hash() & (m_capacity - 1)))
#define _FBT_UTHASHTABLE_HKHASH(key) ((hk & (m_capacity - 1)))
#define _FBT_UTHASHTABLE_FORCE_POW2 1
#define _FBT_UTHASHTABLE_INIT 32
#define _FBT_UTHASHTABLE_EXPANSE (m_size * 2)
#define _FBT_UTHASHTABLE_STAT FBT_HASHTABLE_STAT
#define _FBT_UTHASHTABLE_STAT_ALLOC 0
#if _FBT_UTHASHTABLE_FORCE_POW2 == 1
#define _FBT_UTHASHTABLE_POW2(x) \
--x; x |= x >> 16; x |= x >> 8; x |= x >> 4; \
x |= x >> 2; x |= x >> 1; ++x;
#define _FBT_UTHASHTABLE_IS_POW2(x) (x && !((x-1) & x))
#endif