-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.f90
99 lines (67 loc) · 2.27 KB
/
vector.f90
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
MODULE vector
IMPLICIT NONE
PUBLIC :: VEC, VEC_INIT, VEC_SET, VEC_PUSH_BACK, VEC_POP_BACK, VEC_SWAP, VEC_ENLARGE, VEC_DEALLOCATE
TYPE :: VEC_ELEM
CLASS(*), ALLOCATABLE :: VALUE
END TYPE VEC_ELEM
TYPE :: VEC
TYPE(VEC_ELEM), ALLOCATABLE :: DATA(:)
INTEGER :: SIZE, CAPACITY
END TYPE VEC
CONTAINS
SUBROUTINE VEC_INIT(V)
CLASS(VEC), INTENT(INOUT) :: V
V%SIZE = 0
V%CAPACITY = 0
ALLOCATE (V%DATA(0))
END SUBROUTINE VEC_INIT
SUBROUTINE VEC_SET(V, INDEX, VALUE)
CLASS(VEC), INTENT(INOUT) :: V
INTEGER, INTENT(IN) :: INDEX
CLASS(*), INTENT(IN) :: VALUE
IF (ALLOCATED(V%DATA(INDEX)%VALUE)) THEN
DEALLOCATE (V%DATA(INDEX)%VALUE)
END IF
ALLOCATE (V%DATA(INDEX)%VALUE, SOURCE=VALUE)
END SUBROUTINE VEC_SET
SUBROUTINE VEC_PUSH_BACK(V, VALUE)
CLASS(VEC), INTENT(INOUT) :: V
CLASS(*), INTENT(IN) :: VALUE
IF (V%SIZE == V%CAPACITY) THEN
CALL VEC_ENLARGE(V, MAX(SHIFTL(V%CAPACITY, 1), 1))
END IF
V%SIZE = V%SIZE + 1
CALL VEC_SET(V, V%SIZE, VALUE)
END SUBROUTINE VEC_PUSH_BACK
SUBROUTINE VEC_POP_BACK(V)
CLASS(VEC), INTENT(INOUT) :: V
DEALLOCATE (V%DATA(V%SIZE)%VALUE)
V%SIZE = V%SIZE - 1
END SUBROUTINE VEC_POP_BACK
SUBROUTINE VEC_SWAP(V, I, J)
CLASS(VEC), INTENT(INOUT) :: V
INTEGER, INTENT(IN) :: I, J
CLASS(*), ALLOCATABLE :: TEMP
ALLOCATE (TEMP, SOURCE=V%DATA(I)%VALUE)
CALL VEC_SET(V, I, V%DATA(J)%VALUE)
CALL VEC_SET(V, J, TEMP)
END SUBROUTINE VEC_SWAP
SUBROUTINE VEC_ENLARGE(V, CAPACITY)
CLASS(VEC), INTENT(INOUT) :: V
INTEGER, INTENT(IN) :: CAPACITY
TYPE(VEC_ELEM), ALLOCATABLE :: TEMP(:)
ALLOCATE (TEMP(V%SIZE), SOURCE=V%DATA(:V%SIZE))
DEALLOCATE (V%DATA)
V%CAPACITY = CAPACITY
ALLOCATE (V%DATA(V%CAPACITY))
V%DATA(:V%SIZE) = TEMP
END SUBROUTINE VEC_ENLARGE
SUBROUTINE VEC_DEALLOCATE(V)
CLASS(VEC), INTENT(INOUT) :: V
INTEGER :: I
DO I = 1, V%SIZE
DEALLOCATE (V%DATA(I)%VALUE)
END DO
DEALLOCATE (V%DATA)
END SUBROUTINE VEC_DEALLOCATE
END MODULE vector