File: CaseInsensitiveKey.F90

package info (click to toggle)
gftl 1.3.0%2Bis-really-1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,328 kB
  • sloc: pascal: 4,163; f90: 3,551; sh: 27; fortran: 16; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download
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
module  CIStringIntegerMap_mod
#define _key_string_deferred
#define _key_equal_defined
#define _KEY_LESS_THAN(x,y) caseInsensitiveLessThan(x,y)

#define _value integer
#define _value_equal_defined
#define _value_less_than_defined


#include "templates/map.inc"


   logical function caseInsensitiveLessThan(x,y) result(less)
      character(len=*), intent(in) :: x
      character(len=*), intent(in) :: y

      integer :: i
      character(1) :: cx, cy

      integer, parameter :: UPPER_LOWER_DELTA = iachar('A') - iachar('a')
      
      do i = 1, min(len(x),len(y))
         cx = x(i:i)
         cy = y(i:i)

         if (cx >= 'A' .and. cx <= 'Z') then
            cx = achar(iachar(cx) - UPPER_LOWER_DELTA)
         end if

         if (cy >= 'A' .and. cy <= 'Z') then
            cy = achar(iachar(cy) - UPPER_LOWER_DELTA)
         end if

         less = (cx < cy)
         if (cx /= cy) then
            return
         end if

      end do

      less = (len(x) < len(y))

   end function caseInsensitiveLessThan

end module CIStringIntegerMap_mod


program main
   use CIStringIntegerMap_mod
   implicit none

   type (Map) :: m

   call m%insert('cat', 1)
   call m%insert('dog', 2)
   call m%insert('fish', 3)

   call check('cat', 1)
   call check('dog', 2)
   call check('fish', 3)

   call check('CAT', 1)
   call check('Cat', 1)
   call check('caT', 1)

contains

   subroutine check(str, expected)
      character(len=*), intent(in) :: str
      integer, intent(in) :: expected

      print*,"m%at('",str,"')  = ",m%at(str),"(should be",expected,")"
   end subroutine check

end program main