00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #include <assert.h>
00082 #include <gsl/gsl_errno.h>
00083 #include "gnn_pbundle.h"
00084 #include "chunkallocator.h"
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 static chunkallocator *_ca = NULL;
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 gnn_pbundle *
00123 gnn_pbundle_new ()
00124 {
00125 gnn_pbundle *pb;
00126
00127
00128 if (_ca == NULL)
00129 _ca = chunkallocator_new (sizeof (gnn_pbundle));
00130
00131
00132 pb = (gnn_pbundle *) chunkallocator_alloc (_ca);
00133 if (pb == NULL)
00134 GSL_ERROR_VAL ("could not allocate memory for pbundle",
00135 GSL_ENOMEM, NULL);
00136
00137
00138 pb->c = 0;
00139 pb->ph = NULL;
00140 pb->next = NULL;
00141
00142 return pb;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 void
00155 gnn_pbundle_destroy (gnn_pbundle *pb)
00156 {
00157 gnn_pbundle *ptr;
00158 gnn_pbundle *nextptr;
00159
00160 assert (pb != NULL);
00161
00162 ptr = pb;
00163 while (ptr != NULL)
00164 {
00165 gnn_pbundle *nextptr;
00166
00167 nextptr = ptr->next;
00168
00169 if (ptr->ph != NULL)
00170 gnn_phandle_unref (ptr->ph);
00171
00172 chunkallocator_free (_ca, ptr);
00173
00174 ptr = nextptr;
00175 }
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 int
00193 gnn_pbundle_insert (gnn_pbundle *pb, gnn_phandle *ph, int n)
00194 {
00195 gnn_pbundle *ptr;
00196
00197 assert (pb != NULL);
00198 assert (ph != NULL);
00199
00200
00201 ptr = pb;
00202 while ((ptr->next != NULL) && (ptr->next->ph != ph))
00203 ptr = ptr->next;
00204
00205
00206 if (ptr->next == NULL)
00207 {
00208
00209 ptr->next = (gnn_pbundle *) chunkallocator_alloc (_ca);
00210 if (ptr->next == NULL)
00211 GSL_ERROR ("could not alloc memory for pbundle", GSL_ENOMEM);
00212
00213 ptr = ptr->next;
00214
00215
00216 ptr->c = n;
00217 ptr->ph = ph;
00218 ptr->next = NULL;
00219
00220
00221 gnn_phandle_ref (ptr->ph);
00222 }
00223 else
00224 {
00225 ptr = ptr->next;
00226
00227
00228 ptr->c += n;
00229 }
00230
00231 return 0;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 int
00246 gnn_pbundle_remove (gnn_pbundle *pb, const gnn_phandle *ph, int n)
00247 {
00248 gnn_pbundle *ptr;
00249
00250 assert (pb != NULL);
00251 assert (ph != NULL);
00252
00253
00254 ptr = pb;
00255 while (pb->next != NULL && pb->next->ph != ph)
00256 ptr = ptr->next;
00257
00258
00259 if (pb->next->ph == ph)
00260 {
00261 gnn_pbundle *found;
00262
00263 found = ptr->next;
00264
00265
00266 found->c -= n;
00267
00268
00269 if (found->c <= 0)
00270 {
00271 ptr->next = ptr->next->next;
00272 gnn_phandle_unref (found->ph);
00273 chunkallocator_free (found);
00274 }
00275 }
00276 else
00277 GSL_ERROR ("parameter handle could not be found in bundle", GSL_EINVAL);
00278
00279 return 0;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 int
00294 gnn_pbundle_join (gnn_pbundle *pb, const gnn_pbundle *pbop)
00295 {
00296 gnn_pbundle *ptr;
00297
00298 assert (pb != NULL);
00299 assert (pbop != NULL);
00300
00301
00302 ptr = pbop->next;
00303 while (ptr != NULL)
00304 {
00305 gnn_pbundle_insert (pb, ptr->ph, ptr->c);
00306 ptr = ptr->next;
00307 }
00308
00309 return 0;
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 int
00324 gnn_pbundle_detach (gnn_pbundle *pb, const gnn_pbundle *pbop)
00325 {
00326 gnn_pbundle *ptr;
00327
00328 assert (pb != NULL);
00329 assert (pbop != NULL);
00330
00331
00332 ptr = pbop->next;
00333 while (ptr != NULL)
00334 {
00335 gnn_pbundle_remove (pb, ptr->ph, ptr->c);
00336 ptr = ptr->next;
00337 }
00338
00339 return 0;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 int
00352 gnn_pbundle_get_n_free (gnn_pbundle *pb)
00353 {
00354 int c;
00355 gnn_pbundle *ptr;
00356
00357 assert (pb != NULL);
00358
00359
00360 c = 0;
00361 ptr = pb->next;
00362 while (ptr != NULL)
00363 {
00364 c += gnn_phandle_get_free (ptr->ph);
00365 ptr = ptr->next;
00366 }
00367
00368 return c;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 int
00381 gnn_pbundle_get_size (gnn_pbundle *pb)
00382 {
00383 int c;
00384 gnn_pbundle *ptr;
00385
00386 assert (pb != NULL);
00387
00388
00389 c = 0;
00390 ptr = pb->next;
00391 while (ptr != NULL)
00392 {
00393 c += gnn_phandle_get_size (ptr->ph);
00394 ptr = ptr->next;
00395 }
00396
00397 return c;
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 int
00414 gnn_pbundle_get_w (gnn_pbundle *pb, gsl_vector *w)
00415 {
00416 int poffset;
00417 gnn_pbundle *ptr;
00418
00419 assert (pb != NULL);
00420
00421
00422 if (pb->next == NULL)
00423 {
00424 if (w != NULL)
00425 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00426 return 0;
00427 }
00428
00429
00430 poffset = 0;
00431 ptr = pb->next;
00432 while (ptr != NULL)
00433 {
00434 size_t size;
00435 gsl_vector_view w_view;
00436
00437 size = gnn_phandle_get_size (ptr->ph);
00438 if (size > 0)
00439 {
00440 w_view = gsl_vector_subvector (w, poffset, size);
00441 gsl_vector_memcpy (&(w_view.vector), gnn_phandle_get_w (ptr->ph));
00442 }
00443
00444 poffset += size;
00445 ptr = ptr->next;
00446 }
00447
00448 return 0;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 int
00463 gnn_pbundle_set_w (gnn_pbundle *pb, const gsl_vector *w)
00464 {
00465 int poffset;
00466 gnn_pbundle *ptr;
00467
00468 assert (pb != NULL);
00469
00470
00471 if (pb->next == NULL)
00472 {
00473 if (w != NULL)
00474 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00475 return 0;
00476 }
00477
00478
00479 poffset = 0;
00480 ptr = pb->next;
00481 while (ptr != NULL)
00482 {
00483 size_t size;
00484 gsl_vector_view w_view;
00485
00486 size = gnn_phandle_get_size (ptr->ph);
00487 if (size > 0)
00488 {
00489 w_view = gsl_vector_subvector ((gsl_vector *) w, poffset, size);
00490 gsl_vector_memcpy (gnn_phandle_get_w (ptr->ph), &(w_view.vector));
00491 }
00492
00493 poffset += size;
00494 ptr = ptr->next;
00495 }
00496
00497 return 0;
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 int
00510 gnn_pbundle_set_w_at (gnn_pbundle *pb, size_t i, double wi)
00511 {
00512 int poffset;
00513 gnn_pbundle *ptr;
00514
00515 assert (pb != NULL);
00516 assert (i >= 0);
00517
00518
00519 poffset = 0;
00520 ptr = pb->next;
00521 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
00522 {
00523 i -= gnn_phandle_get_size (ptr->ph);
00524 ptr = ptr->next;
00525 }
00526
00527
00528 if (ptr == NULL)
00529 GSL_ERROR ("index out of bounds", GSL_EINVAL);
00530 gsl_vector_set (gnn_phandle_get_w (ptr->ph), i, wi);
00531
00532 return 0;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 int
00544 gnn_pbundle_set_w_all (gnn_pbundle *pb, double wi)
00545 {
00546 gnn_pbundle *ptr;
00547
00548 assert (pb != NULL);
00549
00550
00551 ptr = pb->next;
00552 while (ptr != NULL)
00553 {
00554 if (gnn_phandle_get_size (ptr->ph) > 0)
00555 gsl_vector_set_all (gnn_phandle_get_w (ptr->ph), wi);
00556 ptr = ptr->next;
00557 }
00558
00559 return 0;
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 double
00571 gnn_pbundle_get_w_at (gnn_pbundle *pb, size_t i)
00572 {
00573 int poffset;
00574 gnn_pbundle *ptr;
00575
00576 assert (pb != NULL);
00577 assert (i >= 0);
00578
00579
00580 poffset = 0;
00581 ptr = pb->next;
00582 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
00583 {
00584 i -= gnn_phandle_get_size (ptr->ph);
00585 ptr = ptr->next;
00586 }
00587
00588
00589 if (ptr == NULL)
00590 GSL_ERROR ("index out of bounds", GSL_EINVAL);
00591
00592 return gsl_vector_get (gnn_phandle_get_w (ptr->ph), i);
00593 }
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 int
00610 gnn_pbundle_get_dw (gnn_pbundle *pb, gsl_vector *dw)
00611 {
00612 int poffset;
00613 gnn_pbundle *ptr;
00614
00615 assert (pb != NULL);
00616
00617
00618 if (pb->next == NULL)
00619 {
00620 if (dw != NULL)
00621 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00622 return 0;
00623 }
00624
00625
00626 poffset = 0;
00627 ptr = pb->next;
00628 while (ptr != NULL)
00629 {
00630 size_t size;
00631 gsl_vector_view dw_view;
00632
00633 size = gnn_phandle_get_size (ptr->ph);
00634 if (size > 0)
00635 {
00636
00637 dw_view = gsl_vector_subvector (dw, poffset, size);
00638
00639
00640 gnn_phandle_update (ptr->ph);
00641 gsl_vector_memcpy (&(dw_view.vector), gnn_phandle_get_dw (ptr->ph));
00642 }
00643
00644 poffset += size;
00645 ptr = ptr->next;
00646 }
00647
00648 return 0;
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 int
00664 gnn_pbundle_set_dw (gnn_pbundle *pb, const gsl_vector *dw)
00665 {
00666 int poffset;
00667 gnn_pbundle *ptr;
00668
00669 assert (pb != NULL);
00670
00671
00672 if (pb->next == NULL)
00673 {
00674 if (dw != NULL)
00675 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00676 return 0;
00677 }
00678
00679
00680 poffset = 0;
00681 ptr = pb->next;
00682 while (ptr != NULL)
00683 {
00684 size_t size;
00685 gsl_vector_view dw_view;
00686
00687 size = gnn_phandle_get_size (ptr->ph);
00688 if (size > 0)
00689 {
00690
00691 dw_view = gsl_vector_subvector ((gsl_vector *) dw, poffset, size);
00692
00693
00694 gsl_vector_memcpy (gnn_phandle_get_dw (ptr->ph), &(dw_view.vector));
00695 gnn_phandle_update (ptr->ph);
00696 }
00697
00698 poffset += size;
00699 ptr = ptr->next;
00700 }
00701
00702 return 0;
00703 }
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717 int
00718 gnn_pbundle_set_dw_at (gnn_pbundle *pb, size_t i, double dwi)
00719 {
00720 int poffset;
00721 gnn_pbundle *ptr;
00722
00723 assert (pb != NULL);
00724 assert (i >= 0);
00725
00726
00727 poffset = 0;
00728 ptr = pb->next;
00729 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
00730 {
00731 i -= gnn_phandle_get_size (ptr->ph);
00732 ptr = ptr->next;
00733 }
00734
00735
00736 if (ptr == NULL)
00737 GSL_ERROR ("index out of bounds", GSL_EINVAL);
00738 if (gsl_vector_int_get (gnn_phandle_get_f (ptr->ph), i) != 0)
00739 gsl_vector_set (gnn_phandle_get_dw (ptr->ph), i, dwi);
00740
00741 return 0;
00742 }
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755 int
00756 gnn_pbundle_set_dw_all (gnn_pbundle *pb, double dwi)
00757 {
00758 gnn_pbundle *ptr;
00759
00760 assert (pb != NULL);
00761
00762
00763 ptr = pb->next;
00764 while (ptr != NULL)
00765 {
00766 if (gnn_phandle_get_size (ptr->ph) > 0)
00767 {
00768 gsl_vector_set_all (gnn_phandle_get_dw (ptr->ph), dwi);
00769 gnn_phandle_update (ptr->ph);
00770 }
00771
00772 ptr = ptr->next;
00773 }
00774
00775 return 0;
00776 }
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 double
00790 gnn_pbundle_get_dw_at (gnn_pbundle *pb, size_t i)
00791 {
00792 int poffset;
00793 gnn_pbundle *ptr;
00794
00795 assert (pb != NULL);
00796 assert (i >= 0);
00797
00798
00799 poffset = 0;
00800 ptr = pb->next;
00801 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
00802 {
00803 i -= gnn_phandle_get_size (ptr->ph);
00804 ptr = ptr->next;
00805 }
00806
00807
00808 if (ptr == NULL)
00809 GSL_ERROR ("index out of bounds", GSL_EINVAL);
00810
00811 if (gsl_vector_int_get (gnn_phandle_get_f (ptr->ph), i) != 0)
00812 return gsl_vector_get (gnn_phandle_get_dw (ptr->ph), i);
00813 else
00814 return 0.0;
00815 }
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828 int
00829 gnn_pbundle_get_f (gnn_pbundle *pb, gsl_vector_int *f)
00830 {
00831 int poffset;
00832 gnn_pbundle *ptr;
00833
00834 assert (pb != NULL);
00835
00836
00837 if (pb->next == NULL)
00838 {
00839 if (f != NULL)
00840 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00841 return 0;
00842 }
00843
00844
00845 poffset = 0;
00846 ptr = pb->next;
00847 while (ptr != NULL)
00848 {
00849 size_t size;
00850 gsl_vector_int_view f_view;
00851
00852 size = gnn_phandle_get_size (ptr->ph);
00853 if (size > 0)
00854 {
00855 f_view = gsl_vector_int_subvector ((gsl_vector_int *) f,
00856 poffset, size);
00857 gsl_vector_int_memcpy (&(f_view.vector),
00858 gnn_phandle_get_f (ptr->ph));
00859 }
00860
00861 poffset += gnn_phandle_get_size (ptr->ph);
00862 ptr = ptr->next;
00863 }
00864
00865 return 0;
00866 }
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879 int
00880 gnn_pbundle_set_f (gnn_pbundle *pb, const gsl_vector_int *f)
00881 {
00882 int poffset;
00883 gnn_pbundle *ptr;
00884
00885 assert (pb != NULL);
00886
00887
00888 if (pb->next == NULL)
00889 {
00890 if (f != NULL)
00891 GSL_ERROR ("bundle and vector aren't of the same size", GSL_EINVAL);
00892 return 0;
00893 }
00894
00895
00896 poffset = 0;
00897 ptr = pb->next;
00898 while (ptr != NULL)
00899 {
00900 size_t size;
00901 gsl_vector_int_view f_view;
00902
00903 size = gnn_phandle_get_size (ptr->ph);
00904 if (size > 0)
00905 {
00906
00907 f_view = gsl_vector_int_subvector ((gsl_vector_int *) f,
00908 poffset, size);
00909 gsl_vector_int_memcpy (gnn_phandle_get_f (ptr->ph),
00910 &(f_view.vector));
00911
00912
00913 gnn_phandle_update (ptr->ph);
00914 }
00915
00916 poffset += size;
00917 ptr = ptr->next;
00918 }
00919
00920 return 0;
00921 }
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932 int
00933 gnn_pbundle_set_f_at (gnn_pbundle *pb, size_t i, int fi)
00934 {
00935 int poffset;
00936 gnn_pbundle *ptr;
00937
00938 assert (pb != NULL);
00939 assert (i >= 0);
00940 assert (fi == 0 || fi == 1);
00941
00942
00943 poffset = 0;
00944 ptr = pb->next;
00945 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
00946 {
00947 i -= gnn_phandle_get_size (ptr->ph);
00948 ptr = ptr->next;
00949 }
00950
00951
00952 if (ptr == NULL)
00953 GSL_ERROR ("index out of bounds", GSL_EINVAL);
00954 gsl_vector_int_set (gnn_phandle_get_f (ptr->ph), i, fi);
00955
00956
00957 gnn_phandle_update (ptr->ph);
00958
00959 return 0;
00960 }
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 int
00974 gnn_pbundle_set_f_all (gnn_pbundle *pb, int fi)
00975 {
00976 gnn_pbundle *ptr;
00977
00978 assert (pb != NULL);
00979 assert (fi == 0 || fi == 1);
00980
00981
00982 ptr = pb->next;
00983 while (ptr != NULL)
00984 {
00985 if (gnn_phandle_get_size (ptr->ph) > 0)
00986 {
00987 gsl_vector_int_set_all (gnn_phandle_get_f (ptr->ph), fi);
00988 gnn_phandle_update (ptr->ph);
00989 }
00990
00991 ptr = ptr->next;
00992 }
00993
00994 return 0;
00995 }
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 int
01006 gnn_pbundle_get_f_at (gnn_pbundle *pb, size_t i)
01007 {
01008 int poffset;
01009 gnn_pbundle *ptr;
01010
01011 assert (pb != NULL);
01012 assert (i >= 0);
01013
01014
01015 poffset = 0;
01016 ptr = pb->next;
01017 while (ptr != NULL && gnn_phandle_get_size (ptr->ph) <= i)
01018 {
01019 i -= gnn_phandle_get_size (ptr->ph);
01020 ptr = ptr->next;
01021 }
01022
01023
01024 if (ptr == NULL)
01025 GSL_ERROR ("index out of bounds", GSL_EINVAL);
01026
01027 return gsl_vector_int_get (gnn_phandle_get_f (ptr->ph), i);
01028 }
01029
01030
01031
01032