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
|
!// objective: test different variants of interfaces in Fortran
!// check: namespacestrings.xml
!// check: interfacestrings_1_1append.xml
!// check: interfacestrings_1_1operator_07_0a_0a_08.xml
!// config: OPTIMIZE_FOR_FORTRAN=YES
!// config: WARN_IF_UNDOCUMENTED=NO
! $Id: Strings.f90 2203 2016-06-09 06:25:07Z ian $
! ff08 source code copyright 2012 M.E.G.M.S. See LICENCE.txt for licence.
!> @file
!! Defines the Strings module.
!*******************************************************************************
!!
!> Routines for managing character strings, particularly one dimensional
!! arrays (vectors) of strings.
!!
!! The String type and associated procedures exposed by this module conform
!! to the typical interface for vectors:
!!
!! - The item component obtains the item for a particular element in the
!! vector.
!! - Append: add an object to the vector.
!! - Grow: create space for new objects at the end of the vector, but do not
!! define those objects.
!! - Shrink: remove objects from the end of the vector.
MODULE Strings
IMPLICIT NONE
PRIVATE
!-----------------------------------------------------------------------------
! Expose module procedures and interfaces
PUBLIC :: Append
PUBLIC :: AppendIfUnique
PUBLIC :: Find
PUBLIC :: Join
PUBLIC :: Grow
PUBLIC :: Replace
PUBLIC :: Len
PUBLIC :: Shrink
PUBLIC :: Pack
PUBLIC :: OPERATOR(==)
!-----------------------------------------------------------------------------
! The String derived type.
!> Strings store default kind characters.
INTEGER, PARAMETER :: ck = KIND('a')
!> Element in an array of strings of varying length.
TYPE, PUBLIC :: String
!> The item for a particular element.
CHARACTER(:,KIND=ck), ALLOCATABLE :: item
END TYPE String
!-----------------------------------------------------------------------------
! Interfaces
!> Append a string or vector of strings to another vector of strings.
INTERFACE Append
MODULE PROCEDURE Append_
MODULE PROCEDURE Append_scalar
MODULE PROCEDURE Append_vector
END INTERFACE Append
!> Append a string or vector of strings to another vector of strings
!! if the items are not already in that vector.
INTERFACE AppendIfUnique
MODULE PROCEDURE AppendIfUnique_
MODULE PROCEDURE AppendIfUnique_scalar
MODULE PROCEDURE AppendIfUnique_vector
END INTERFACE AppendIfUnique
!> Function to find a string in a vector of strings.
INTERFACE Find
MODULE PROCEDURE Find_
END INTERFACE Find
!> Elemental comparison of two strings or of a string and a character
!! scalar.
INTERFACE OPERATOR(==)
MODULE PROCEDURE compare
MODULE PROCEDURE compare_str
END INTERFACE OPERATOR(==)
!> Add undefined items to the end of the vector.
INTERFACE Grow
MODULE PROCEDURE Grow_
END INTERFACE Grow
!> Join an array of strings into one character string.
INTERFACE Join
MODULE PROCEDURE Join_
END INTERFACE Join
!> Function to pack an array into a vector.
INTERFACE Pack
MODULE PROCEDURE Pack_chvector
END INTERFACE Pack
!> Replace an item with other items
INTERFACE Replace
MODULE PROCEDURE Replace_
END INTERFACE Replace
!> Remove items from the end of the vector.
INTERFACE Shrink
MODULE PROCEDURE Shrink_
END INTERFACE Shrink
!> Add a single item to the end of the vector.
INTERFACE Push
MODULE PROCEDURE Append_
END INTERFACE Push
!> Get the length of a string.
!
! It is not permitted to reference the generic name in specification
! expressions prior to the LEN_ subprogram. Be careful with the
! LEN intrinsic!
INTERFACE Len
MODULE PROCEDURE Len_
END INTERFACE Len
CONTAINS
!*****************************************************************************
!!
!> Expands a vector of Strings with undefined items.
!!
!! @param[in] vector The vector to expand.
!!
!! @param[in] count Optional number of items to expand by.
!! If not present, the vector is expanded by one item. If negative, the
!! vector will shrink.
SUBROUTINE Grow_(vector, count)
USE CharUtils
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
INTEGER, INTENT(IN), OPTIONAL :: count
!---------------------------------------------------------------------------
! Local variables
INTEGER :: local_count ! Local copy of count
INTEGER :: i ! Vector index.
! Temporary for size change.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
IF (PRESENT(count)) THEN
local_count = count
ELSE
local_count = 1
END IF
!---------------------------------------------------------------------------
IF (local_count < 0) THEN
CALL Shrink(vector, -local_count)
RETURN
END IF
IF (.NOT. ALLOCATED(vector)) THEN
ALLOCATE(tmp(local_count))
ELSE
! Copy across existing items
ALLOCATE(tmp(SIZE(vector)+local_count))
DO i = 1, SIZE(vector)
CALL MOVE_ALLOC(vector(i)%item, tmp(i)%item)
END DO
END IF
CALL MOVE_ALLOC(tmp, vector)
END SUBROUTINE Grow_
!*****************************************************************************
!!
!> Shrinks a vector of Strings by removing items from the end.
!!
!! @param[in] vector The vector to expand.
!!
!! @param[in] count Optional number of items to shrink by.
!! If not present, the vector is shrunk by one item. If negative, the
!! vector will grow.
!!
!! This is perhaps a more performance way of going:
!! @code
!! vector = vector(:SIZE(vector)-count)
!! @endcode
!!
!! though it handles some edge cases nicely too.
SUBROUTINE Shrink_(vector, count)
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
INTEGER, INTENT(IN), OPTIONAL :: count
!---------------------------------------------------------------------------
! Local variables
INTEGER :: local_count ! Local copy of count
INTEGER :: i ! Vector index.
! Temporary for size change.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
IF (PRESENT(count)) THEN
local_count = count
ELSE
local_count = 1
END IF
!---------------------------------------------------------------------------
IF (local_count < 0) THEN
CALL Grow(vector, local_count)
RETURN
END IF
IF (.NOT. ALLOCATED(vector)) THEN
! Vector is already empty - leave it that way.
RETURN
ELSE
! Copy across existing items
ALLOCATE(tmp(MAX(0,SIZE(vector)-local_count)))
DO i = 1, SIZE(tmp)
CALL MOVE_ALLOC(vector(i)%item, tmp(i)%item)
END DO
END IF
CALL MOVE_ALLOC(tmp, vector)
END SUBROUTINE Shrink_
!*****************************************************************************
!!
!> Adds a single item to the end of the vector of Strings.
!!
!! @param[in] vector The vector of strings.
!!
!! @param[in] item The CHARACTER object to append as a
!! new item.
SUBROUTINE Append_(vector, item)
USE CharUtils
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
CHARACTER(*,KIND=ck), INTENT(IN) :: item
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i ! Vector index.
! Temporary for size change.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
IF (.NOT. ALLOCATED(vector)) THEN
ALLOCATE(tmp(1))
ELSE
ALLOCATE(tmp(SIZE(vector)+1))
DO i = 1, SIZE(vector)
CALL MOVE_ALLOC(vector(i)%item, tmp(i)%item)
END DO
END IF
tmp(SIZE(tmp))%item = item
CALL MOVE_ALLOC(tmp, vector)
END SUBROUTINE Append_
!*****************************************************************************
!!
!> Adds a single item to the end of the vector of Strings.
!!
!! @param[in] vector The vector of strings.
!!
!! @param[in] item The scalar String object to append as a
!! new item.
SUBROUTINE Append_scalar(vector, item)
USE CharUtils
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
TYPE(String), INTENT(IN) :: item
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i ! Vector index.
! Temporary for size change.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
IF (.NOT. ALLOCATED(vector)) THEN
ALLOCATE(tmp(1))
ELSE
ALLOCATE(tmp(SIZE(vector)+1))
DO i = 1, SIZE(vector)
CALL MOVE_ALLOC(vector(i)%item, tmp(i)%item)
END DO
END IF
tmp(SIZE(tmp)) = item
CALL MOVE_ALLOC(tmp, vector)
END SUBROUTINE Append_scalar
!*****************************************************************************
!!
!> Appends a vector of Strings to another vector of Strings.
!!
!! @param[in] vector The vector of Strings to be appended
!! to.
!!
!! @param[in] items The potential selection of Strings
!! to be appended.
!!
!! @param[in] mask A mask that designates which elements
!! from @a items will be appended. If not present then all elements from
!! @a items will be appended. If present must be the same size as @a items.
!!
!! Note that @a items is left unchanged.
SUBROUTINE Append_vector(vector, items, mask)
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
TYPE(String), INTENT(IN) :: items(:)
LOGICAL, INTENT(IN), OPTIONAL :: mask(:)
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i ! Vector index.
INTEGER :: count ! Number of elements to be appended.
! Temporary for size change.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
count = 0
DO i = 1, SIZE(items)
IF (PRESENT(mask)) THEN
IF (.NOT. mask(i)) CYCLE
END IF
count = count + 1
END DO
IF (.NOT. ALLOCATED(vector)) THEN
ALLOCATE(tmp(count))
ELSE
ALLOCATE(tmp(SIZE(vector) + count))
DO i = 1, SIZE(vector)
CALL MOVE_ALLOC(vector(i)%item, tmp(i)%item)
END DO
END IF
count = SIZE(vector)
DO i = 1, SIZE(items)
IF (PRESENT(mask)) THEN
IF (.NOT. mask(i)) CYCLE
END IF
count = count + 1
tmp(count)%item = items(i)%item
END DO
CALL MOVE_ALLOC(tmp, vector)
END SUBROUTINE Append_vector
!*****************************************************************************
!!
!> Append an item to a vector of strings if the item is not already in the
!! vector.
!!
!! @param[in] vector The vector of Strings to be appended to.
!!
!! @param[in] item The item to be appended, if it is unique.
!!
!! Appends @a item to @a vector if @a item is not already in @a vector.
SUBROUTINE AppendIfUnique_(vector, item)
USE CharUtils
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
CHARACTER(*,KIND=ck), INTENT(IN) :: item
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i ! Vector index.
!***************************************************************************
DO i = 1, SIZE(vector)
IF (vector(i)%item == item) RETURN ! already in vector.
END DO
! If we get to here then item isn't in vector.
CALL Append(vector, item)
END SUBROUTINE AppendIfUnique_
!*****************************************************************************
!!
!> Append an item to a vector of strings if the item is not already in the
!! vector.
!!
!! @param[in] vector The vector of Strings to be appended to.
!!
!! @param[in] item The scalar string to be appended, if it
!! is unique.
!!
!! Appends @a item to @a vector if @a item is not already in @a vector.
SUBROUTINE AppendIfUnique_scalar(vector, item)
USE CharUtils
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
TYPE(String), INTENT(IN) :: item
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i ! Vector index.
!***************************************************************************
DO i = 1, SIZE(vector)
IF (vector(i) == item) RETURN ! already in vector.
END DO
! If we get to here then item isn't in vector.
CALL Append(vector, item)
END SUBROUTINE AppendIfUnique_scalar
!*****************************************************************************
!!
!> Append items from a vector of strings to another vector if those items
!! are not already in the vector to be appended to.
!!
!! @param[in] vector The vector of Strings to be appended
!! to.
!!
!! @param[in] items The vector of items to be appended
!! if they are not already in @a vector.
SUBROUTINE AppendIfUnique_vector(vector, items)
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
TYPE(String), INTENT(IN) :: items(:)
!---------------------------------------------------------------------------
! Local variables
INTEGER :: i1 ! Index into items.
INTEGER :: i2 ! Index into vector.
! Mask of the items that will be appended.
LOGICAL :: mask(SIZE(items))
!***************************************************************************
outer_loop: DO i1 = 1, SIZE(items)
DO i2 = 1, SIZE(vector)
IF (items(i1)%item == vector(i2)%item) THEN
mask(i1) = .TRUE.
CYCLE outer_loop
END IF
END DO
mask(i1) = .FALSE.
END DO outer_loop
CALL Append(vector, items, mask)
END SUBROUTINE AppendIfUnique_vector
!*****************************************************************************
!!
!> Find the index in a vector of Strings of a particular character string.
!!
!! @param[in] vector The vector of Strings to search.
!!
!! @param[in] item The string to search for.
!!
!> @returns the element of @a vector that corresponds to @a item.
FUNCTION Find_(vector, item) RESULT(i)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: vector(:)
CHARACTER(*,KIND=ck), INTENT(IN) :: item
! Function result
INTEGER :: i
!***************************************************************************
DO i = 1, SIZE(vector)
IF (vector(i)%item == item) RETURN
END DO
i = 0
END FUNCTION Find_
!*****************************************************************************
!!
!> Implementation of OPERATOR(==) - compare two Strings.
!!
!! @param[in] lhs The left hand string.
!!
!! @param[in] rhs The right hand string.
!!
!! @return .TRUE. if the strings compare equal (Fortran rules), .FALSE.
!! otherwise.
ELEMENTAL FUNCTION compare(lhs, rhs) RESULT(b)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: lhs
TYPE(String), INTENT(IN) :: rhs
! Function result
LOGICAL :: b
!***************************************************************************
b = lhs%item == rhs%item
END FUNCTION compare
!*****************************************************************************
!!
!> Implementation of OPERATOR(==) - compare a String with a character
!! scalar.
!!
!! @param[in] lhs The string to compare.
!!
!! @param[in] rhs The character scalar to compare.
!!
!! @returns .TRUE. if string and the character variable compare equal
!! (Fortran rules), .FALSE. otherwise.
ELEMENTAL FUNCTION compare_str(lhs, rhs) RESULT(b)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: lhs
CHARACTER(*,KIND=ck), INTENT(IN) :: rhs
! Function result
LOGICAL :: b
!***************************************************************************
b = lhs%item == rhs
END FUNCTION compare_str
!*****************************************************************************
!!
!> Replace an item in a vector by one or more items.
!!
!! @param[in,out] vector The vector of Strings to operate on.
!!
!! @param[in] idx The index of the item to replace.
!!
!! @param[in] insert_items The vector of items to insert.
SUBROUTINE Replace_(vector, idx, insert_items)
!---------------------------------------------------------------------------
! Arguments
TYPE(String), INTENT(INOUT), ALLOCATABLE :: vector(:)
INTEGER, INTENT(IN) :: idx
TYPE(String), INTENT(IN) :: insert_items(:)
!---------------------------------------------------------------------------!
! Local variables
! Temporary for changing the size of vector.
TYPE(String), ALLOCATABLE :: tmp(:)
!***************************************************************************
ALLOCATE(tmp(SIZE(vector) - 1 + SIZE(insert_items)))
tmp(:idx-1) = vector(:idx-1)
tmp(idx:idx+SIZE(insert_items)-1) = insert_items
tmp(idx+SIZE(insert_items):) = vector(idx:)
END SUBROUTINE Replace_
!*****************************************************************************
!!
!> Pack function specialised for TYPE(String) as a replacement for
!! the intrinsic.
!!
!! @param[in] array The array of things to pack.
!!
!! @param[in] mask Shall be the same size as @a array. The
!! intrinsic form simply requires conformability, but that's a trivial case
!! to implement in client code.
!!
!! @param[in] vector Optional. Shall have at least as many
!! elements as there are .TRUE. elements in @a mask.
!!
!! @returns If @a vector is present, the result size is that of @a vector,
!! otherwise the result size is the number <i>t</i> of true elements in
!! @a mask.
!!
!! Element <i>i</i> of the result is the element of @a array that
!! corresponds to the <i>i</i>th true element of @a mask, taking elements in
!! array element order, for <i>i</i> = 1, 2, ..., <i>t</i>. If @a vector is
!! present and has size <i>n</i> > <i>t</i>, element <i>i</i> of the result
!! has the value @a vector(<i>i</i>), for <i>i</i> = <i>t</i> + 1, ...,
!! <i>n</i>.
!!
!! Delete and revert to the intrinsic once the ifort 12.1.0 bug reported at
!! http://software.intel.com/en-us/forums/showthread.php?t=85834
!! has been fixed.
FUNCTION Pack_chvector(array, mask, vector) RESULT(res)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: array(:)
LOGICAL, INTENT(IN) :: mask(:)
TYPE(String), INTENT(IN), OPTIONAL :: vector(:)
! Function result
TYPE(String), ALLOCATABLE :: res(:)
!---------------------------------------------------------------------------
! Local variables
INTEGER :: ia ! Array index
INTEGER :: im ! Mask index
!***************************************************************************
IF (PRESENT(vector)) THEN
! There is a requirement that SIZE(vector) be greater than or equal to
! COUNT(mask).
ALLOCATE(res(SIZE(vector)))
ELSE
ALLOCATE(res(COUNT(mask)))
END IF
! Copy the items across to the result under the control of the mask.
ia = 1
DO im = 1, SIZE(mask)
IF (mask(im)) THEN
res(ia) = array(im)
ia = ia + 1
END IF
END DO
! ia will come out of the above loop pointing to the next empty space
! in res, if it exists. SIZE(vector) == SIZE(res), so if ia > SIZE(res)
! then the sequence is empty on both sides of the assignment.
IF (PRESENT(vector)) res(ia:) = vector(ia:)
END FUNCTION Pack_chvector
!*****************************************************************************
!!
!> Get the length of a string.
!!
!! @param[in] str
!!
!! This is just a forwarding function to the LEN intrinsic.
ELEMENTAL FUNCTION Len_(str)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: str
! Function result.
INTEGER :: Len_
!***************************************************************************
Len_ = LEN(str%item)
END FUNCTION Len_
!*****************************************************************************
!!
!> Join a vector of strings into a single character variable.
!!
!! @param[in] vector The vector of strings.
!!
!! @param[in] separator Optional separator to appear between
!! each element in the string. If not present then ', ' is used.
!!
!! @returns The joined string.
FUNCTION Join_(vector, separator) RESULT(str)
!---------------------------------------------------------------------------
! Characteristics
TYPE(String), INTENT(IN) :: vector(:)
CHARACTER(*), INTENT(IN), OPTIONAL :: separator
! Function result.
CHARACTER(:), ALLOCATABLE :: str
!---------------------------------------------------------------------------
! Locals
INTEGER :: i ! vector index.
!***************************************************************************
IF (SIZE(vector) == 0) THEN
str = ''
RETURN
END IF
str = vector(1)%item
DO i = 2, SIZE(vector)
IF (PRESENT(separator)) THEN
str = str // separator // vector(i)%item
ELSE
str = str // ', ' // vector(i)%item
END IF
END DO
END FUNCTION Join_
END MODULE Strings
|