1. Harald Klimach
  2. TreElM

Source

TreElM / source / arrayMacros.inc

  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
! This file contains the source code for growing and dynamic arrays.
! This is used for arrays of primitives (int, long_int, real, ...) as well as
! for arrays of derived datatypes (tem_variable_type,...).
!
! To use these macros include the following to your source file.
!
! Smart growing array (GA) for ?tstring?
! Growing Arrays:
!
! declaration
!
?? text :: GA_decltxt(tname, tstring)
  !> Growing array type for ?tstring?
  type grw_?tname?Array_type
    integer :: nVals = 0
    integer :: ContainerSize = 0
    ?tstring?, allocatable :: Val(:)
  end type

  !> Initialize the dynamic array
  interface init
    module procedure init_GA_?tname?
  end interface

  !> Truncate the array, meaning
  !! cut off the trailing empty entries
  interface truncate
    module procedure truncate_GA_?tname?
  end interface

  !> Empty the entries  without changing arrays
  interface empty
    module procedure empty_GA_?tname?
  end interface

  !> Destroy the dynamic array
  interface destroy
    module procedure destroy_GA_?tname?
  end interface

  !> Append a value to the dynamic array
  !! and return its position.
  interface append
    module procedure append_GA_?tname?
  end interface

  !> Increase the size of the container
  !! for the array.
  interface expand
    module procedure expand_GA_?tname?
  end interface
?? end text GA_decltxt
!
! implementation
!
?? text :: GA_impltxt(tname, tstring)
  subroutine init_GA_?tname?(me, length)
    type(grw_?tname?Array_type), intent(out) :: me !< Dynamic Array to init
    integer, intent(in), optional :: length !< Initial length of the container

    if (present(length)) me%ContainerSize = length

    allocate(me%Val(me%ContainerSize))

  end subroutine init_GA_?tname?

  subroutine destroy_GA_?tname?(me)
    type(grw_?tname?Array_type), intent(inout) :: me !< Dynamic Array to init

    me%ContainerSize = 0 
    me%nVals = 0 
    if( allocated( me%Val ))     &
      deallocate(me%Val)

  end subroutine destroy_GA_?tname?


  subroutine truncate_GA_?tname?(me)
    !------------------------------------------------------------------------
    type(grw_?tname?Array_type) :: me !< Array to sortTruncate
    !------------------------------------------------------------------------
    ?tstring?, allocatable :: tArray(:)
    !------------------------------------------------------------------------

    ! Nothing to do if container size is not larger than the number of values
    ! in the array.
    if (me%containerSize > me%nVals) then
      allocate(tArray(me%nVals))
      tArray = me%val(:me%nVals)

      deallocate(me%val)
      me%containerSize = me%nVals
      allocate(me%val(me%containerSize))

      me%val = tArray
      deallocate(tArray)
    end if

  end subroutine truncate_GA_?tname?

  subroutine empty_GA_?tname?(me)
    !------------------------------------------------------------------------
    type(grw_?tname?Array_type) :: me !< Array to sortTruncate
    !------------------------------------------------------------------------
    ?tstring?, allocatable :: tArray(:)
    !------------------------------------------------------------------------

    me%nVals = 0

  end subroutine empty_GA_?tname?


  subroutine append_GA_?tname?(me, val, pos)
    type(grw_?tname?Array_type) :: me !< Array to append the value to
    ?tstring?, intent(in) :: val !< Value to append
    integer, intent(in), optional :: pos

    integer :: FoundPos

    if( present( pos ) ) then
      ! Do it smart array style, just use FoundPos
      FoundPos = pos
    else
      FoundPos = me%nVals + 1
    endif

    ! Value to append is larger than all existing ones,
    ! just put it to the end of the list, this captures
    ! also the case of empty lists.
    ! In this case FoundPos = me%nVals + 1 holds.
    if (FoundPos > me%ContainerSize) then
      ! Expand the array, if its boundary is reached
      call expand(me)
    end if

    me%nVals = max( FoundPos, me%nVals )
    me%val(FoundPos) = val

  end subroutine append_GA_?tname?

  subroutine expand_GA_?tname?(me, pos)
    type(grw_?tname?Array_type) :: me !< Array to resize
    integer, intent(in), optional :: pos

    ?tstring?, allocatable :: swpval(:)

    allocate(swpval(me%nVals))
    swpval(:me%nVals) = me%Val(:me%nVals)
    deallocate(me%Val)
    if( present( pos ) ) then
      me%ContainerSize = max(minLength, me%ContainerSize*2, pos)
    else
      me%ContainerSize = max(minLength, me%ContainerSize*2)
    endif
    allocate(me%Val(me%ContainerSize))
    me%Val(:me%nVals) = swpval(:me%nVals)
    deallocate(swpval)

  end subroutine expand_GA_?tname?
?? end text GA_impltxt
!
!------------------------------------------------------------------------------
!
! dynamic Arrays:
!
! declaration
!
?? text :: DA_decltxt(tname, tstring)
! \brief Smart dynamic array (DA) for ?tstring?
!
! This datatype implements a dynamic array, which is capable of
! growing and adding of unique elements. It is available for
! various types, here we deal with $tstring$.
! Sorted array contains the pointers to Val array, instead of
! the actual values in Val array. For example:
! Val:     8, 6, 7, 5
! sorted:  4, 2, 3, 1
  !> dynamic array (DA) type for ?tstring?
  type dyn_?tname?Array_type
    integer :: nVals = 0
    integer :: ContainerSize = 0
    logical :: Unique
    ?tstring?, allocatable :: Val(:)
    integer, allocatable :: sorted(:) !< pointers, NOT values
  end type

  !> Initialize the dynamic array
  interface init
    module procedure init_DA_?tname?
  end interface

  !> Destroy the dynamic array
  interface destroy
    module procedure destroy_DA_?tname?
  end interface

  !> Append a value to the dynamic array
  !! and return its position.
  interface append
    module procedure append_DA_?tname?
  end interface

  !> Truncate the array, meaning
  !! cut off the trailing empty entries
  interface truncate
    module procedure truncate_DA_?tname?
  end interface

  !> Truncate the array, meaning
  !! cut off the trailing empty entries
  interface empty
    module procedure empty_DA_?tname?
  end interface


  !> Fix the dynamic array, meaning:
  !! store the array in the sorted order and cut
  !! off the trailing empty entries
  interface sortTruncate
    module procedure sortTruncate_DA_?tname?
  end interface

  !> Increase the size of the container
  !! for the array.
  interface expand
    module procedure expand_DA_?tname?
  end interface

  !> Return the position of a given value
  !! in the array val, which is what you usually want to know.
  !! It is the index of a given value
  interface PositionOfVal
    module procedure PosOfVal_?tname?
  end interface

  !> Return the position of a given value
  !! in the list 'sorted'. This is mainly for internal usage.
  !! The sorted list is only a pointer list to the actual values
  !! Thus, in order to get the index of a given value, you
  !! need to look up the entry in the sorted list.
  !! This is done by the PositionOfVal routine
  interface SortedPosOfVal
    module procedure SortPosOfVal_?tname?
  end interface
?? end text DA_decltxt
!
! implementation
!
?? text :: DA_impltxt(tname, tstring, argstring)
!******************************************************************************!
  !> Initialization of a dynamic array
  !!
  !! Before a dynamic array can be used, it has to be initialized
  !! with this routine. The initial length provided here, can
  !! avoid reallocations and memory copying, if approximated
  !! correctly enough. If none is specified, the provided container
  !! initially will be of size 0.
  subroutine init_DA_?tname?(me, unique, length)
    !-----------------------------------------------------------------
    type(dyn_?tname?Array_type), intent(out) :: me !< Dynamic Array to init
    logical, intent(in) :: unique !< Flag if the values have to be unique
    integer, intent(in), optional :: length !< Initial length of the container
    !-----------------------------------------------------------------

    if (present(length)) me%ContainerSize = length

    me%Unique = unique
    allocate(me%Val(me%ContainerSize))
    allocate(me%sorted(me%ContainerSize))
  end subroutine init_DA_?tname?

  !> Destruction of a dynamic array
  !!
  !! This subroutine takes care of a proper destruction of a
  !! dynamic array, it frees the allocated memory and resets
  !! the internal counts to 0.
  subroutine destroy_DA_?tname?(me)
    type(dyn_?tname?Array_type), intent(inout) :: me !< Dynamic Array to init

    me%ContainerSize = 0
    me%nVals         = 0

    if( allocated( me%Val ))     &
      deallocate(me%Val)
    if( allocated( me%sorted ))     &
      deallocate(me%sorted)
  end subroutine destroy_DA_?tname?
!******************************************************************************!


!******************************************************************************!
  !> Appending a value to the dynamic array
  !!
  !! With this subroutine, a given value can be added to the
  !! dynamic array. The actual position of this value in the
  !! dynamic array will be returned, so it can be found again
  !! easily later. With the wasAdded flag, it is indicated,\n
  !! wasAdded = true,  if this entry had to be added,\n
  !! wasAdded = false, if this was already found in the array.
  subroutine append_DA_?tname?(me, val, pos, wasAdded )
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: me !< Array to append the value to
    ?argstring?, intent(in) :: val !< Value to append
    integer, intent(out) :: pos !< Position in the array, the value is found
    !> Flag to indicate, if val was newly added
    logical, intent(out), optional :: wasAdded
    !------------------------------------------------------------------------
    integer :: FoundPos
    integer :: i
    !------------------------------------------------------------------------

    ! Do a binary search on existing entries (returns closest entry next to
    ! it if not found).
    FoundPos = SortedPosOfVal(me, val, .true.)
    if( present( wasAdded ) ) wasAdded = .false.

    ! If it found the value, the position is smaller than nVals
    if (FoundPos <= me%nVals) then

      ! The returned position might actually be the right entry already or
      ! not, check for this here.
      if ((me%val(me%sorted(FoundPos)) == val) &
        & .and. me%Unique) then

        ! Found the value in a list of unique values,
        ! nothing to do, just return its position.
        pos = me%sorted(FoundPos)

      else

        ! Need to append a new value!

        if( present( wasAdded ) ) wasAdded = .true.
        if (me%nVals == me%ContainerSize) then

          ! Container is full, need to expand it
          call expand(me)
        end if
        me%nVals = me%nVals + 1

        ! Put the new value into the last position in the
        ! array.
        me%val(me%nVals) = val
        do while( (FoundPos < me%nVals) &
          & )
          if(me%val(me%sorted(FoundPos)) /= val) then
           exit
          end if
          ! In case of multiple entries with the same value
          ! move on to the first differing entry.
          FoundPos = FoundPos + 1
        end do
        ! Shift the sorted list of indices, to create a
        ! whole for the value to be inserted, at position
        ! FoundPos.
        do i=me%nVals-1,FoundPos,-1
          me%sorted(i+1) = me%sorted(i)
        end do
        ! Put the index of the new value into the
        ! sorted list at the now freed position.
        me%sorted(FoundPos) = me%nVals
        pos = me%nVals

      end if

    else

      ! Value to append is larger than all existing ones,
      ! just put it to the end of the list, this captures
      ! also the case of empty lists.
      ! In this case FoundPos = me%nVals + 1 holds.
      if( present( wasAdded ) ) wasAdded = .true.
      if (FoundPos > me%ContainerSize) then
        ! Expand the array, if its boundary is reached
        call expand(me)
      end if
      me%nVals = FoundPos
      me%val(FoundPos) = val
      me%sorted(FoundPos) = FoundPos
      pos = FoundPos

    end if

  end subroutine append_DA_?tname?
!******************************************************************************!


!******************************************************************************!
  !> Truncate the array after the last valid entry and hence cut off the empty
  !! trailing empty entries
  !!
  subroutine truncate_DA_?tname?(me)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: me !< Array to sortTruncate
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: tArray !< temporary Array
    integer :: iVal
    integer :: dPos
    !------------------------------------------------------------------------
    ! Allocate the temporary array
    call init( me = tArray, length = me%nVals, unique = me%unique )
    ! Copy the entries in a sorted fashion into the temporary array
    do iVal = 1, me%nVals
      call append( me = tArray, val = me%val( iVal ), &
           &       pos = dPos)
    enddo
    call destroy( me = me )

    me = tArray
    call destroy( me = tArray )

  end subroutine truncate_DA_?tname?
!******************************************************************************!


!******************************************************************************!
  !> Empty all contents of the array without changing the size or status of any
  !! array
  !!
  subroutine empty_DA_?tname?(me)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: me !< Array to sortTruncate
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: tArray !< temporary Array
    integer :: iVal
    integer :: dPos
    !------------------------------------------------------------------------
    ! Reset the number of entries 
    me%nVals = 0
  end subroutine empty_DA_?tname?
!******************************************************************************!


!******************************************************************************!
  !> Fixing the dynamic array
  !!
  !! Truncate the array after the last valid entry and hence cut off the empty
  !! trailing empty entries
  !! Store the array in the sorted order according to the sorted( ) array
  !!
  subroutine sortTruncate_DA_?tname?(me)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: me !< Array to sortTruncate
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: tArray !< temporary Array
    integer :: iVal
    integer :: dPos
    !------------------------------------------------------------------------
    ! Allocate the temporary array
    call init( me = tArray, length = me%nVals, unique = me%unique )
    ! Copy the entries in a sorted fashion into the temporary array
    do iVal = 1, me%nVals
      call append( me = tArray, val = me%val( me%sorted( iVal )), &
           &       pos = dPos)
    enddo
    call destroy( me = me )

    me = tArray
    call destroy( me = tArray )

  end subroutine sortTruncate_DA_?tname?
!******************************************************************************!




!******************************************************************************!
  !> Expanding the dynamic array
  !!
  !! This is a helping subroutine, which doubles the container
  !! of the given dynamic array. As the container might be
  !! initially 0-sized, a module variable minLength has been introduced, which
  !! is used here, to at least create a container of this size.
  subroutine expand_DA_?tname?(me)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type) :: me !< Array to resize
    !------------------------------------------------------------------------
    ?tstring?, allocatable :: swpval(:)
    integer, allocatable :: swpsort(:)
    !------------------------------------------------------------------------
    allocate(swpval(me%nVals))
    swpval = me%Val(1:me%nVals)
    if( allocated( me%val)) &
    deallocate(me%Val)

    me%ContainerSize = max(minLength, me%ContainerSize*2)
    allocate(me%Val(me%ContainerSize))
    me%Val(1:me%nVals) = swpval
    deallocate(swpval)

    allocate(swpsort(me%nVals))
    swpsort = me%sorted(:me%nVals)
    deallocate(me%sorted)
    allocate(me%sorted(me%ContainerSize))
    me%sorted(1:me%nVals) = swpsort
    deallocate(swpsort)
  end subroutine expand_DA_?tname?
!******************************************************************************!


!******************************************************************************!
  !> Return the sorted position of a value in the given dynamic array
  !!
  !! If the value was not found,
  !!  - return 0 if nextIfNotFound = .false.
  !!  - return position at the end if nextIfNotFound = .true.
  function SortPosOfVal_?tname?(me, val, nextIfNotFound, lower, upper) result(pos)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type), intent(in) :: me ! Dynamic Array
    ?argstring?, intent(in) :: val !< Value to look for
    !> Flag to indicate, if the next entry in the list should be returned,
    !! if the searched one is not found.
    logical, intent(in), optional :: nextIfNotFound
    integer, intent(in), optional :: lower !< lower search limit
    integer, intent(in), optional :: upper !< upper search limit
    integer :: pos !< Position of val in the sorted list, 0 if not found
    !------------------------------------------------------------------------
    logical :: retNext
    integer :: lb, ub
    integer :: mid
    ?tstring? :: lb_val, ub_val
    ?tstring? :: mid_val
    !------------------------------------------------------------------------

    retNext = .false.
    if (present(nextIfNotFound)) retNext = nextIfNotFound

    lb = 1
    ub = me%nVals

    if( present( lower ) ) lb = lower
    if( present( upper ) ) ub = upper

    pos = 0
    if (retNext) pos = lb

    !> Binary search on sorted list
    do while(ub >= lb)
      lb_val = me%Val(me%sorted(lb))

      if (val < lb_val) then
        if (retNext) pos = lb
        exit
      end if

      ub_val = me%Val(me%sorted(ub))

      if (val > ub_val) then
        if (retNext) pos = ub+1
        exit
      end if

      mid = (lb+ub) / 2
      mid_val = me%Val(me%sorted(mid))
      if (val == mid_val) then
        pos = mid
        exit
      end if
      if (val > mid_val) then
        lb = mid + 1
      else
        ub = mid - 1
      end if
    end do
  end function SortPosOfVal_?tname?
!******************************************************************************!


!******************************************************************************!
  !> The actual position of a given value in the dynamic array
  !!
  !! Most likely this, is what you need in codes, using this
  !! data structure, it first does the binary search on the sorted
  !! values with SortPosOfVal_?tname? and then returns the looked
  !! up position in the original unsorted array, which corresponds
  !! to the position returned by the append routine.
  function PosOfVal_?tname?(me, val, nextIfNotFound, lower, upper) result(pos)
    !------------------------------------------------------------------------
    type(dyn_?tname?Array_type), intent(in) :: me !< Dynamic Array
    ?argstring?, intent(in) :: val !< Value to search for
    !> Flag to indicate, if the position of the next entry in the sorted
    !! list should be returned instead, if val is not found.
    logical, intent(in), optional :: nextIfNotFound
    integer, intent(in), optional :: lower !< lower search limit
    integer, intent(in), optional :: upper !< upper search limit
    integer :: pos !< Position in the array of the searche value, 0 if not found
    !------------------------------------------------------------------------
    integer :: sortpos
    integer :: lb, ub
    !------------------------------------------------------------------------

    lb = 1
    ub = me%nVals

    if( present( lower ) ) lb = lower
    if( present( upper ) ) ub = upper

    sortpos = SortedPosOfVal(me, val, nextIfNotFound, lb, ub)

    ! If result (sorted pos)
    if ((sortpos <= me%nVals) .and. (sortpos > 0)) then
      pos = me%sorted(sortpos)
    else
      pos = sortpos
    end if
  end function PosOfVal_?tname?
!******************************************************************************!
?? end text DA_impltxt
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Copyright 2012 German Research School for Simulation Sciences GmbH, Aachen.
! All rights reserved.
! 
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
! 
! 1. Redistributions of source code must retain the above copyright notice, this
! list of conditions and the following disclaimer.
! 
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
! 
! THIS SOFTWARE IS PROVIDED BY GERMAN RESEARCH SCHOOL FOR SIMULATION SCIENCES
! GMBH, AACHEN AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
! PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GERMAN RESEARCH SCHOOL
! FOR SIMULATION SCIENCES GMBH, AACHEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
! INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
! OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
! NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 
! The views and conclusions contained in the software and documentation are
! those of the authors and should not be interpreted as representing official
! policies, either expressed or implied, of German Research School for
! Simulation Sciences GmbH, Aachen.
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!