-
Notifications
You must be signed in to change notification settings - Fork 20
/
ap.cpp
10505 lines (9490 loc) · 296 KB
/
ap.cpp
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
/*************************************************************************
Copyright (c) Sergey Bochkanov (ALGLIB project).
>>> SOURCE LICENSE >>>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (www.fsf.org); either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available at
http://www.fsf.org/licensing/licenses
>>> END OF LICENSE >>>
*************************************************************************/
#include "stdafx.h"
#include "ap.h"
#include <limits>
#include <locale.h>
using namespace std;
#if defined(AE_CPU)
#if (AE_CPU==AE_INTEL)
#if AE_COMPILER==AE_MSVC
#include <intrin.h>
#endif
#endif
#endif
// disable some irrelevant warnings
#if (AE_COMPILER==AE_MSVC)
#pragma warning(disable:4100)
#pragma warning(disable:4127)
#pragma warning(disable:4702)
#pragma warning(disable:4996)
#endif
/////////////////////////////////////////////////////////////////////////
//
// THIS SECTION IMPLEMENTS BASIC FUNCTIONALITY LIKE
// MEMORY MANAGEMENT FOR VECTORS/MATRICES WHICH IS
// SHARED BETWEEN C++ AND PURE C LIBRARIES
//
/////////////////////////////////////////////////////////////////////////
namespace alglib_impl
{
/*
* local definitions
*/
#define x_nb 16
#define AE_DATA_ALIGN 16
#define AE_PTR_ALIGN sizeof(void*)
#define DYN_BOTTOM ((void*)1)
#define DYN_FRAME ((void*)2)
#define AE_LITTLE_ENDIAN 1
#define AE_BIG_ENDIAN 2
#define AE_MIXED_ENDIAN 3
#define AE_SER_ENTRY_LENGTH 11
#define AE_SER_ENTRIES_PER_ROW 5
#define AE_SM_DEFAULT 0
#define AE_SM_ALLOC 1
#define AE_SM_READY2S 2
#define AE_SM_TO_STRING 10
#define AE_SM_FROM_STRING 20
#define AE_SM_TO_CPPSTRING 11
#define AE_LOCK_CYCLES 512
#define AE_CRITICAL_ASSERT(x) if( !(x) ) abort()
/*
* alloc counter (if used)
*/
#ifdef AE_USE_ALLOC_COUNTER
ae_int64_t _alloc_counter = 0;
#endif
#ifdef AE_DEBUGRNG
static ae_int_t _debug_rng_s0 = 11;
static ae_int_t _debug_rng_s1 = 13;
#endif
/*
* These declarations are used to ensure that
* sizeof(ae_int32_t)==4, sizeof(ae_int64_t)==8, sizeof(ae_int_t)==sizeof(void*).
* they will lead to syntax error otherwise (array size will be negative).
*
* you can remove them, if you want - they are not used anywhere.
*
*/
static char _ae_int32_t_must_be_32_bits_wide[1-2*((int)(sizeof(ae_int32_t))-4)*((int)(sizeof(ae_int32_t))-4)];
static char _ae_int64_t_must_be_64_bits_wide[1-2*((int)(sizeof(ae_int64_t))-8)*((int)(sizeof(ae_int64_t))-8)];
static char _ae_int_t_must_be_pointer_sized [1-2*((int)(sizeof(ae_int_t))-(int)sizeof(void*))*((int)(sizeof(ae_int_t))-(int)(sizeof(void*)))];
ae_int_t ae_misalignment(const void *ptr, size_t alignment)
{
union _u
{
const void *ptr;
ae_int_t iptr;
} u;
u.ptr = ptr;
return (ae_int_t)(u.iptr%alignment);
}
void* ae_align(void *ptr, size_t alignment)
{
char *result = (char*)ptr;
if( (result-(char*)0)%alignment!=0 )
result += alignment - (result-(char*)0)%alignment;
return result;
}
void ae_break(ae_state *state, ae_error_type error_type, const char *msg)
{
#ifndef AE_USE_CPP_ERROR_HANDLING
if( state!=NULL )
{
if( state->thread_exception_handler!=NULL )
state->thread_exception_handler(state);
ae_state_clear(state);
state->last_error = error_type;
state->error_msg = msg;
if( state->break_jump!=NULL )
longjmp(*(state->break_jump), 1);
else
abort();
}
else
abort();
#else
if( state!=NULL )
{
if( state->thread_exception_handler!=NULL )
state->thread_exception_handler(state);
ae_state_clear(state);
state->last_error = error_type;
state->error_msg = msg;
}
throw error_type;
#endif
}
void* aligned_malloc(size_t size, size_t alignment)
{
if( size==0 )
return NULL;
if( alignment<=1 )
{
/* no alignment, just call malloc */
void *block;
void **p; ;
block = malloc(sizeof(void*)+size);
if( block==NULL )
return NULL;
p = (void**)block;
*p = block;
#ifdef AE_USE_ALLOC_COUNTER
_alloc_counter++;
#endif
return (void*)((char*)block+sizeof(void*));
}
else
{
/* align */
void *block;
char *result;
block = malloc(alignment-1+sizeof(void*)+size);
if( block==NULL )
return NULL;
result = (char*)block+sizeof(void*);
/*if( (result-(char*)0)%alignment!=0 )
result += alignment - (result-(char*)0)%alignment;*/
result = (char*)ae_align(result, alignment);
*((void**)(result-sizeof(void*))) = block;
#ifdef AE_USE_ALLOC_COUNTER
_alloc_counter++;
#endif
return result;
}
}
void aligned_free(void *block)
{
void *p;
if( block==NULL )
return;
p = *((void**)((char*)block-sizeof(void*)));
free(p);
#ifdef AE_USE_ALLOC_COUNTER
_alloc_counter--;
#endif
}
/************************************************************************
Malloc's memory with automatic alignment.
Returns NULL when zero size is specified.
Error handling:
* if state is NULL, returns NULL on allocation error
* if state is not NULL, calls ae_break() on allocation error
************************************************************************/
void* ae_malloc(size_t size, ae_state *state)
{
void *result;
if( size==0 )
return NULL;
result = aligned_malloc(size,AE_DATA_ALIGN);
if( result==NULL && state!=NULL)
{
char buf[256];
sprintf(buf, "ae_malloc(): out of memory (attempted to allocate %llu bytes)", (unsigned long long)size);
ae_break(state, ERR_OUT_OF_MEMORY, buf);
}
return result;
}
void ae_free(void *p)
{
if( p!=NULL )
aligned_free(p);
}
/************************************************************************
Sets pointers to the matrix rows.
* dst must be correctly initialized matrix
* dst->data.ptr points to the beginning of memory block allocated for
row pointers.
* dst->ptr - undefined (initialized during algorithm processing)
* storage parameter points to the beginning of actual storage
************************************************************************/
void ae_matrix_update_row_pointers(ae_matrix *dst, void *storage)
{
char *p_base;
void **pp_ptr;
ae_int_t i;
if( dst->rows>0 && dst->cols>0 )
{
p_base = (char*)storage;
pp_ptr = (void**)dst->data.ptr;
dst->ptr.pp_void = pp_ptr;
for(i=0; i<dst->rows; i++, p_base+=dst->stride*ae_sizeof(dst->datatype))
pp_ptr[i] = p_base;
}
else
dst->ptr.pp_void = NULL;
}
/************************************************************************
Returns size of datatype.
Zero for dynamic types like strings or multiple precision types.
************************************************************************/
ae_int_t ae_sizeof(ae_datatype datatype)
{
switch(datatype)
{
case DT_BOOL: return (ae_int_t)sizeof(ae_bool);
case DT_INT: return (ae_int_t)sizeof(ae_int_t);
case DT_REAL: return (ae_int_t)sizeof(double);
case DT_COMPLEX: return 2*(ae_int_t)sizeof(double);
default: return 0;
}
}
/************************************************************************
This dummy function is used to prevent compiler messages about unused
locals in automatically generated code.
It makes nothing - just accepts pointer, "touches" it - and that is all.
It performs several tricky operations without side effects which confuse
compiler so it does not compain about unused locals in THIS function.
************************************************************************/
void ae_touch_ptr(void *p)
{
void * volatile fake_variable0 = p;
void * volatile fake_variable1 = fake_variable0;
fake_variable0 = fake_variable1;
}
/************************************************************************
This function initializes ALGLIB environment state.
NOTES:
* stacks contain no frames, so ae_make_frame() must be called before
attaching dynamic blocks. Without it ae_leave_frame() will cycle
forever (which is intended behavior).
************************************************************************/
void ae_state_init(ae_state *state)
{
ae_int32_t *vp;
/*
* p_next points to itself because:
* * correct program should be able to detect end of the list
* by looking at the ptr field.
* * NULL p_next may be used to distinguish automatic blocks
* (in the list) from non-automatic (not in the list)
*/
state->last_block.p_next = &(state->last_block);
state->last_block.deallocator = NULL;
state->last_block.ptr = DYN_BOTTOM;
state->p_top_block = &(state->last_block);
#ifndef AE_USE_CPP_ERROR_HANDLING
state->break_jump = NULL;
#endif
state->error_msg = "";
/*
* determine endianness and initialize precomputed IEEE special quantities.
*/
state->endianness = ae_get_endianness();
if( state->endianness==AE_LITTLE_ENDIAN )
{
vp = (ae_int32_t*)(&state->v_nan);
vp[0] = 0;
vp[1] = (ae_int32_t)0x7FF80000;
vp = (ae_int32_t*)(&state->v_posinf);
vp[0] = 0;
vp[1] = (ae_int32_t)0x7FF00000;
vp = (ae_int32_t*)(&state->v_neginf);
vp[0] = 0;
vp[1] = (ae_int32_t)0xFFF00000;
}
else if( state->endianness==AE_BIG_ENDIAN )
{
vp = (ae_int32_t*)(&state->v_nan);
vp[1] = 0;
vp[0] = (ae_int32_t)0x7FF80000;
vp = (ae_int32_t*)(&state->v_posinf);
vp[1] = 0;
vp[0] = (ae_int32_t)0x7FF00000;
vp = (ae_int32_t*)(&state->v_neginf);
vp[1] = 0;
vp[0] = (ae_int32_t)0xFFF00000;
}
else
abort();
/*
* set threading information
*/
state->worker_thread = NULL;
state->parent_task = NULL;
state->thread_exception_handler = NULL;
}
/************************************************************************
This function clears ALGLIB environment state.
All dynamic data controlled by state are freed.
************************************************************************/
void ae_state_clear(ae_state *state)
{
while( state->p_top_block->ptr!=DYN_BOTTOM )
ae_frame_leave(state);
}
#ifndef AE_USE_CPP_ERROR_HANDLING
/************************************************************************
This function sets jump buffer for error handling.
buf may be NULL.
************************************************************************/
void ae_state_set_break_jump(ae_state *state, jmp_buf *buf)
{
state->break_jump = buf;
}
#endif
/************************************************************************
This function makes new stack frame.
This function takes two parameters: environment state and pointer to the
dynamic block which will be used as indicator of the frame beginning.
This dynamic block must be initialized by caller and mustn't be changed/
deallocated/reused till ae_leave_frame called. It may be global or local
variable (local is even better).
************************************************************************/
void ae_frame_make(ae_state *state, ae_frame *tmp)
{
tmp->db_marker.p_next = state->p_top_block;
tmp->db_marker.deallocator = NULL;
tmp->db_marker.ptr = DYN_FRAME;
state->p_top_block = &tmp->db_marker;
}
/************************************************************************
This function leaves current stack frame and deallocates all automatic
dynamic blocks which were attached to this frame.
************************************************************************/
void ae_frame_leave(ae_state *state)
{
while( state->p_top_block->ptr!=DYN_FRAME && state->p_top_block->ptr!=DYN_BOTTOM)
{
if( state->p_top_block->ptr!=NULL && state->p_top_block->deallocator!=NULL)
((ae_deallocator)(state->p_top_block->deallocator))(state->p_top_block->ptr);
state->p_top_block = state->p_top_block->p_next;
}
state->p_top_block = state->p_top_block->p_next;
}
/************************************************************************
This function attaches block to the dynamic block list
block block
state ALGLIB environment state
NOTES:
* never call it for special blocks which marks frame boundaries!
************************************************************************/
void ae_db_attach(ae_dyn_block *block, ae_state *state)
{
block->p_next = state->p_top_block;
state->p_top_block = block;
}
/************************************************************************
This function malloc's dynamic block:
block destination block, assumed to be uninitialized
size size (in bytes)
state ALGLIB environment state. May be NULL.
make_automatic if true, vector is added to the dynamic block list
block is assumed to be uninitialized, its fields are ignored.
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
NOTES:
* never call it for blocks which are already in the list
************************************************************************/
ae_bool ae_db_malloc(ae_dyn_block *block, ae_int_t size, ae_state *state, ae_bool make_automatic)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(size>=0, "ae_db_malloc(): negative size", state);
if( size<0 )
return ae_false;
/* alloc */
block->ptr = ae_malloc((size_t)size, state);
if( block->ptr==NULL && size!=0 )
return ae_false;
if( make_automatic && state!=NULL )
ae_db_attach(block, state);
else
block->p_next = NULL;
block->deallocator = ae_free;
return ae_true;
}
/************************************************************************
This function realloc's dynamic block:
block destination block (initialized)
size new size (in bytes)
state ALGLIB environment state
block is assumed to be initialized.
This function:
* deletes old contents
* preserves automatic state
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
NOTES:
* never call it for special blocks which mark frame boundaries!
************************************************************************/
ae_bool ae_db_realloc(ae_dyn_block *block, ae_int_t size, ae_state *state)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(size>=0, "ae_db_realloc(): negative size", state);
if( size<0 )
return ae_false;
/* realloc */
if( block->ptr!=NULL )
((ae_deallocator)block->deallocator)(block->ptr);
block->ptr = ae_malloc((size_t)size, state);
if( block->ptr==NULL && size!=0 )
return ae_false;
block->deallocator = ae_free;
return ae_true;
}
/************************************************************************
This function clears dynamic block (releases all dynamically allocated
memory). Dynamic block may be in automatic management list - in this case
it will NOT be removed from list.
block destination block (initialized)
NOTES:
* never call it for special blocks which marks frame boundaries!
************************************************************************/
void ae_db_free(ae_dyn_block *block)
{
if( block->ptr!=NULL )
((ae_deallocator)block->deallocator)(block->ptr);
block->ptr = NULL;
block->deallocator = ae_free;
}
/************************************************************************
This function swaps contents of two dynamic blocks (pointers and
deallocators) leaving other parameters (automatic management settings,
etc.) unchanged.
NOTES:
* never call it for special blocks which marks frame boundaries!
************************************************************************/
void ae_db_swap(ae_dyn_block *block1, ae_dyn_block *block2)
{
void (*deallocator)(void*) = NULL;
void * volatile ptr;
ptr = block1->ptr;
deallocator = block1->deallocator;
block1->ptr = block2->ptr;
block1->deallocator = block2->deallocator;
block2->ptr = ptr;
block2->deallocator = deallocator;
}
/************************************************************************
This function creates ae_vector.
Vector size may be zero. Vector contents is uninitialized.
dst destination vector
size vector size, may be zero
datatype guess what...
state ALGLIB environment state
make_automatic if true, vector is added to the dynamic block list
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
dst is assumed to be uninitialized, its fields are ignored.
************************************************************************/
ae_bool ae_vector_init(ae_vector *dst, ae_int_t size, ae_datatype datatype, ae_state *state, ae_bool make_automatic)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(size>=0, "ae_vector_init(): negative size", state);
if( size<0 )
return ae_false;
/* init */
dst->cnt = size;
dst->datatype = datatype;
if( !ae_db_malloc(&dst->data, size*ae_sizeof(datatype), state, make_automatic) )
return ae_false;
dst->ptr.p_ptr = dst->data.ptr;
return ae_true;
}
/************************************************************************
This function creates copy of ae_vector.
dst destination vector
src well, it is source
state ALGLIB environment state
make_automatic if true, vector is added to the dynamic block list
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
dst is assumed to be uninitialized, its fields are ignored.
************************************************************************/
ae_bool ae_vector_init_copy(ae_vector *dst, ae_vector *src, ae_state *state, ae_bool make_automatic)
{
if( !ae_vector_init(dst, src->cnt, src->datatype, state, make_automatic) )
return ae_false;
if( src->cnt!=0 )
memcpy(dst->ptr.p_ptr, src->ptr.p_ptr, (size_t)(src->cnt*ae_sizeof(src->datatype)));
return ae_true;
}
/************************************************************************
This function creates ae_vector from x_vector:
dst destination vector
src source, vector in x-format
state ALGLIB environment state
make_automatic if true, vector is added to the dynamic block list
dst is assumed to be uninitialized, its fields are ignored.
************************************************************************/
void ae_vector_init_from_x(ae_vector *dst, x_vector *src, ae_state *state, ae_bool make_automatic)
{
ae_vector_init(dst, (ae_int_t)src->cnt, (ae_datatype)src->datatype, state, make_automatic);
if( src->cnt>0 )
memcpy(dst->ptr.p_ptr, src->ptr, (size_t)(((ae_int_t)src->cnt)*ae_sizeof((ae_datatype)src->datatype)));
}
/************************************************************************
This function changes length of ae_vector.
dst destination vector
newsize vector size, may be zero
state ALGLIB environment state
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
NOTES:
* vector must be initialized
* all contents is destroyed during setlength() call
* new size may be zero.
************************************************************************/
ae_bool ae_vector_set_length(ae_vector *dst, ae_int_t newsize, ae_state *state)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(newsize>=0, "ae_vector_set_length(): negative size", state);
if( newsize<0 )
return ae_false;
/* set length */
if( dst->cnt==newsize )
return ae_true;
dst->cnt = newsize;
if( !ae_db_realloc(&dst->data, newsize*ae_sizeof(dst->datatype), state) )
return ae_false;
dst->ptr.p_ptr = dst->data.ptr;
return ae_true;
}
/************************************************************************
This function provides "CLEAR" functionality for vector (contents is
cleared, but structure still left in valid state).
The function clears vector contents (releases all dynamically allocated
memory). Vector may be in automatic management list - in this case it
will NOT be removed from list.
IMPORTANT: this function does NOT invalidates dst; it just releases all
dynamically allocated storage, but dst still may be used after call to
ae_vector_set_length().
dst destination vector
************************************************************************/
void ae_vector_clear(ae_vector *dst)
{
dst->cnt = 0;
ae_db_free(&dst->data);
dst->ptr.p_ptr = 0;
}
/************************************************************************
This function provides "DESTROY" functionality for vector (contents is
cleared, all internal structures are destroyed). For vectors it is same
as CLEAR.
dst destination vector
************************************************************************/
void ae_vector_destroy(ae_vector *dst)
{
ae_vector_clear(dst);
}
/************************************************************************
This function efficiently swaps contents of two vectors, leaving other
pararemeters (automatic management, etc.) unchanged.
************************************************************************/
void ae_swap_vectors(ae_vector *vec1, ae_vector *vec2)
{
ae_int_t cnt;
ae_datatype datatype;
void *p_ptr;
ae_db_swap(&vec1->data, &vec2->data);
cnt = vec1->cnt;
datatype = vec1->datatype;
p_ptr = vec1->ptr.p_ptr;
vec1->cnt = vec2->cnt;
vec1->datatype = vec2->datatype;
vec1->ptr.p_ptr = vec2->ptr.p_ptr;
vec2->cnt = cnt;
vec2->datatype = datatype;
vec2->ptr.p_ptr = p_ptr;
}
/************************************************************************
This function creates ae_matrix.
Matrix size may be zero, in such cases both rows and cols are zero.
Matrix contents is uninitialized.
dst destination natrix
rows rows count
cols cols count
datatype element type
state ALGLIB environment state
make_automatic if true, matrix is added to the dynamic block list
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
dst is assumed to be uninitialized, its fields are ignored.
************************************************************************/
ae_bool ae_matrix_init(ae_matrix *dst, ae_int_t rows, ae_int_t cols, ae_datatype datatype, ae_state *state, ae_bool make_automatic)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(rows>=0 && cols>=0, "ae_matrix_init(): negative length", state);
if( rows<0 || cols<0 )
return ae_false;
/* if one of rows/cols is zero, another MUST be too */
if( rows==0 || cols==0 )
{
rows = 0;
cols = 0;
}
/* init */
dst->rows = rows;
dst->cols = cols;
dst->stride = cols;
while( dst->stride*ae_sizeof(datatype)%AE_DATA_ALIGN!=0 )
dst->stride++;
dst->datatype = datatype;
if( !ae_db_malloc(&dst->data, dst->rows*((ae_int_t)sizeof(void*)+dst->stride*ae_sizeof(datatype))+AE_DATA_ALIGN-1, state, make_automatic) )
return ae_false;
ae_matrix_update_row_pointers(dst, ae_align((char*)dst->data.ptr+dst->rows*sizeof(void*),AE_DATA_ALIGN));
return ae_true;
}
/************************************************************************
This function creates copy of ae_matrix.
dst destination matrix
src well, it is source
state ALGLIB environment state
make_automatic if true, matrix is added to the dynamic block list
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
dst is assumed to be uninitialized, its fields are ignored.
************************************************************************/
ae_bool ae_matrix_init_copy(ae_matrix *dst, ae_matrix *src, ae_state *state, ae_bool make_automatic)
{
ae_int_t i;
if( !ae_matrix_init(dst, src->rows, src->cols, src->datatype, state, make_automatic) )
return ae_false;
if( src->rows!=0 && src->cols!=0 )
{
if( dst->stride==src->stride )
memcpy(dst->ptr.pp_void[0], src->ptr.pp_void[0], (size_t)(src->rows*src->stride*ae_sizeof(src->datatype)));
else
for(i=0; i<dst->rows; i++)
memcpy(dst->ptr.pp_void[i], src->ptr.pp_void[i], (size_t)(dst->cols*ae_sizeof(dst->datatype)));
}
return ae_true;
}
void ae_matrix_init_from_x(ae_matrix *dst, x_matrix *src, ae_state *state, ae_bool make_automatic)
{
char *p_src_row;
char *p_dst_row;
ae_int_t row_size;
ae_int_t i;
ae_matrix_init(dst, (ae_int_t)src->rows, (ae_int_t)src->cols, (ae_datatype)src->datatype, state, make_automatic);
if( src->rows!=0 && src->cols!=0 )
{
p_src_row = (char*)src->ptr;
p_dst_row = (char*)(dst->ptr.pp_void[0]);
row_size = ae_sizeof((ae_datatype)src->datatype)*(ae_int_t)src->cols;
for(i=0; i<src->rows; i++, p_src_row+=src->stride*ae_sizeof((ae_datatype)src->datatype), p_dst_row+=dst->stride*ae_sizeof((ae_datatype)src->datatype))
memcpy(p_dst_row, p_src_row, (size_t)(row_size));
}
}
/************************************************************************
This function changes length of ae_matrix.
dst destination matrix
rows size, may be zero
cols size, may be zero
state ALGLIB environment state
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
NOTES:
* matrix must be initialized
* all contents is destroyed during setlength() call
* new size may be zero.
************************************************************************/
ae_bool ae_matrix_set_length(ae_matrix *dst, ae_int_t rows, ae_int_t cols, ae_state *state)
{
/* ensure that size is >=0
two ways to exit: 1) through ae_assert, if we have non-NULL state, 2) by returning ae_false */
if( state!=NULL )
ae_assert(rows>=0 && cols>=0, "ae_matrix_set_length(): negative length", state);
if( rows<0 || cols<0 )
return ae_false;
if( dst->rows==rows && dst->cols==cols )
return ae_true;
dst->rows = rows;
dst->cols = cols;
dst->stride = cols;
while( dst->stride*ae_sizeof(dst->datatype)%AE_DATA_ALIGN!=0 )
dst->stride++;
if( !ae_db_realloc(&dst->data, dst->rows*((ae_int_t)sizeof(void*)+dst->stride*ae_sizeof(dst->datatype))+AE_DATA_ALIGN-1, state) )
return ae_false;
ae_matrix_update_row_pointers(dst, ae_align((char*)dst->data.ptr+dst->rows*sizeof(void*),AE_DATA_ALIGN));
return ae_true;
}
/************************************************************************
This function provides "CLEAR" functionality for vector (contents is
cleared, but structure still left in valid state).
The function clears matrix contents (releases all dynamically allocated
memory). Matrix may be in automatic management list - in this case it
will NOT be removed from list.
IMPORTANT: this function does NOT invalidates dst; it just releases all
dynamically allocated storage, but dst still may be used after call to
ae_matrix_set_length().
dst destination matrix
************************************************************************/
void ae_matrix_clear(ae_matrix *dst)
{
dst->rows = 0;
dst->cols = 0;
dst->stride = 0;
ae_db_free(&dst->data);
dst->ptr.p_ptr = 0;
}
/************************************************************************
This function provides "DESTROY" functionality for matrix (contents is
cleared, but structure still left in valid state).
For matrices it is same as CLEAR.
dst destination matrix
************************************************************************/
void ae_matrix_destroy(ae_matrix *dst)
{
ae_matrix_clear(dst);
}
/************************************************************************
This function efficiently swaps contents of two vectors, leaving other
pararemeters (automatic management, etc.) unchanged.
************************************************************************/
void ae_swap_matrices(ae_matrix *mat1, ae_matrix *mat2)
{
ae_int_t rows;
ae_int_t cols;
ae_int_t stride;
ae_datatype datatype;
void *p_ptr;
ae_db_swap(&mat1->data, &mat2->data);
rows = mat1->rows;
cols = mat1->cols;
stride = mat1->stride;
datatype = mat1->datatype;
p_ptr = mat1->ptr.p_ptr;
mat1->rows = mat2->rows;
mat1->cols = mat2->cols;
mat1->stride = mat2->stride;
mat1->datatype = mat2->datatype;
mat1->ptr.p_ptr = mat2->ptr.p_ptr;
mat2->rows = rows;
mat2->cols = cols;
mat2->stride = stride;
mat2->datatype = datatype;
mat2->ptr.p_ptr = p_ptr;
}
/************************************************************************
This function creates smart pointer structure.
dst destination smart pointer.
already allocated, but not initialized.
subscriber pointer to pointer which receives updates in the
internal object stored in ae_smart_ptr. Any update to
dst->ptr is translated to subscriber. Can be NULL.
state ALGLIB environment state
make_automatic if true, smart pointer is added to the dynamic block list
After initialization, smart pointer stores NULL pointer.
Error handling:
* if state is NULL, returns ae_false on allocation error
* if state is not NULL, calls ae_break() on allocation error
* returns ae_true on success
************************************************************************/
ae_bool ae_smart_ptr_init(ae_smart_ptr *dst, void **subscriber, ae_state *state, ae_bool make_automatic)
{
dst->subscriber = subscriber;
dst->ptr = NULL;
if( dst->subscriber!=NULL )
*(dst->subscriber) = dst->ptr;
dst->is_owner = ae_false;
dst->frame_entry.deallocator = ae_smart_ptr_destroy;
dst->frame_entry.ptr = dst;
if( make_automatic && state!=NULL )
ae_db_attach(&dst->frame_entry, state);
return ae_true;
}
/************************************************************************
This function clears smart pointer structure.
dst destination smart pointer.
After call to this function smart pointer contains NULL reference, which
is propagated to its subscriber (in cases non-NULL subscruber was
specified during pointer creation).
************************************************************************/
void ae_smart_ptr_clear(void *_dst)
{
ae_smart_ptr *dst = (ae_smart_ptr*)_dst;
if( dst->is_owner && dst->ptr!=NULL )
dst->destroy(dst->ptr);
dst->is_owner = ae_false;
dst->ptr = NULL;
dst->destroy = NULL;
if( dst->subscriber!=NULL )
*(dst->subscriber) = NULL;
}
/************************************************************************
This function dstroys smart pointer structure (same as clearing it).
dst destination smart pointer.
************************************************************************/
void ae_smart_ptr_destroy(void *_dst)
{
ae_smart_ptr_clear(_dst);
}