collections 1.0.0
COLLECTIONS is a library providing a set of types supporting collections in Fortran.
Loading...
Searching...
No Matches
collections.f90
1
90
94 use iso_fortran_env
95 use ferror
96 implicit none
97 private
98 public :: list
99 public :: linked_list
100
101! ------------------------------------------------------------------------------
102 integer(int32), parameter :: FL_NO_ERROR = 0
103 integer(int32), parameter :: FL_INVALID_ARGUMENT_ERROR = 1000
104 integer(int32), parameter :: FL_OUT_OF_MEMORY_ERROR = 1001
105 integer(int32), parameter :: FL_INDEX_OUT_OF_RANGE_ERROR = 1002
106 integer(int32), parameter :: FL_INVALID_ITERATOR_ERROR = 1003
107
108! ------------------------------------------------------------------------------
110 type container
111 ! A pointer to an unlimited polymorphic variable allowing storage of
112 ! any type.
113 class(*), private, pointer :: m_item => null()
114 ! Set to true to delete @ref item when this container object goes out
115 ! of scope; else, set to false to persist.
116 logical, private :: m_delete = .true.
117
118 contains
128 procedure, public :: get => c_get
141 procedure, public :: delete_on_cleanup => c_get_delete
143 final :: c_destroy
152 procedure, public :: free => c_cleanup
153 end type
154
156 type, extends(container) :: node
158 type(node), private, pointer :: next => null()
160 type(node), private, pointer :: previous => null()
161 end type
162
163 ! collections_container.f90
164 interface
165 module function c_get(this) result(rst)
166 class(container), intent(in) :: this
167 class(*), pointer :: rst
168 end function
169
170 pure module function c_get_delete(this) result(rst)
171 class(container), intent(in) :: this
172 logical :: rst
173 end function
174
175 module subroutine c_destroy(this)
176 type(container), intent(inout) :: this
177 end subroutine
178
179 module subroutine c_cleanup(this)
180 class(container), intent(inout) :: this
181 end subroutine
182 end interface
183
259 type list
260 ! A collection of container objects.
261 type(container), private, allocatable, dimension(:) :: m_list
262 ! The actual number of items in m_list.
263 integer(int32), private :: m_count = 0
264 contains
274 procedure, public :: count => list_get_count
291 procedure, public :: get_capacity => list_get_capacity
307 procedure, public :: set_capacity => list_set_capacity
330 procedure, public :: push => list_push
339 procedure, public :: pop => list_pop
356 procedure, public :: get => list_get
382 procedure, public :: set => list_set
408 procedure, public :: insert => list_insert
423 procedure, public :: remove => list_remove
432 procedure, public :: clear => list_clear
433 end type
434
435 ! collections_list.f90
436 interface
437 pure module function list_get_count(this) result(rst)
438 class(list), intent(in) :: this
439 integer(int32) :: rst
440 end function
441
442 pure module function list_get_capacity(this) result(rst)
443 class(list), intent(in) :: this
444 integer(int32) :: rst
445 end function
446
447 module subroutine list_set_capacity(this, n, err)
448 class(list), intent(inout) :: this
449 integer(int32), intent(in) :: n
450 class(errors), intent(inout), optional, target :: err
451 end subroutine
452
453 module subroutine list_push(this, x, manage, err)
454 class(list), intent(inout) :: this
455 class(*), intent(in), target :: x
456 logical, intent(in), optional :: manage
457 class(errors), intent(inout), optional, target :: err
458 end subroutine
459
460 module subroutine list_pop(this)
461 class(list), intent(inout) :: this
462 end subroutine
463
464 module function list_get(this, i, err) result(rst)
465 class(list), intent(in) :: this
466 integer(int32), intent(in) :: i
467 class(errors), intent(inout), optional, target :: err
468 class(*), pointer :: rst
469 end function
470
471 module subroutine list_set(this, i, x, manage, err)
472 class(list), intent(inout) :: this
473 integer(int32), intent(in) :: i
474 class(*), intent(in), target :: x
475 logical, intent(in), optional :: manage
476 class(errors), intent(inout), optional, target :: err
477 end subroutine
478
479 module subroutine list_insert(this, i, x, manage, err)
480 class(list), intent(inout) :: this
481 integer(int32) :: i
482 class(*), intent(in) :: x
483 logical, intent(in), optional :: manage
484 class(errors), intent(inout), optional, target :: err
485 end subroutine
486
487 module subroutine list_remove(this, i, err)
488 class(list), intent(inout) :: this
489 integer(int32) :: i
490 class(errors), intent(inout), optional, target :: err
491 end subroutine
492
493 module subroutine list_clear(this)
494 class(list), intent(inout) :: this
495 end subroutine
496 end interface
497
498! ------------------------------------------------------------------------------
576 type linked_list
577 ! The number of nodes in the container.
578 integer(int32), private :: m_count = 0
579 ! A pointer to the first node in the container.
580 type(node), private, pointer :: m_first => null()
581 ! A pointer to the last node in the container.
582 type(node), private, pointer :: m_last => null()
583 ! A pointer to the current node selected by the user.
584 type(node), private, pointer :: m_current => null()
585 contains
595 procedure, public :: count => ll_count
604 procedure, public :: move_to_first => ll_move_to_first
613 procedure, public :: move_to_last => ll_move_to_last
625 procedure, public :: next => ll_move_to_next
637 procedure, public :: previous => ll_move_to_previous
648 procedure, public :: get => ll_get
671 procedure, public :: set => ll_set
694 procedure, public :: push => ll_push
703 procedure, public :: pop => ll_pop
712 procedure, public :: clear => ll_clear
715 final :: ll_destroy
716 end type
717
718 ! collections_linked_list.f90
719 interface
720 pure module function ll_count(this) result(rst)
721 class(linked_list), intent(in) :: this
722 integer(int32) :: rst
723 end function
724
725 module subroutine ll_move_to_first(this)
726 class(linked_list), intent(inout) :: this
727 end subroutine
728
729 module subroutine ll_move_to_last(this)
730 class(linked_list), intent(inout) :: this
731 end subroutine
732
733 module function ll_move_to_next(this) result(rst)
734 class(linked_list), intent(inout) :: this
735 logical :: rst
736 end function
737
738 module function ll_move_to_previous(this) result(rst)
739 class(linked_list), intent(inout) :: this
740 logical :: rst
741 end function
742
743 module function ll_get(this) result(rst)
744 class(linked_list), intent(in) :: this
745 class(*), pointer :: rst
746 end function
747
748 module subroutine ll_set(this, x, manage, err)
749 class(linked_list), intent(inout) :: this
750 class(*), intent(in), target :: x
751 logical, intent(in), optional :: manage
752 class(errors), intent(inout), optional, target :: err
753 end subroutine
754
755 module subroutine ll_push(this, x, manage, err)
756 class(linked_list), intent(inout) :: this
757 class(*), intent(in), target :: x
758 logical, intent(in), optional :: manage
759 class(errors), intent(inout), optional, target :: err
760 end subroutine
761
762 module subroutine ll_pop(this)
763 class(linked_list), intent(inout) :: this
764 end subroutine
765
766 module subroutine ll_clear(this)
767 class(linked_list), intent(inout) :: this
768 end subroutine
769
770 module subroutine ll_destroy(this)
771 type(linked_list), intent(inout) :: this
772 end subroutine
773 end interface
774end module
A module containing various collections using Fortran's unlimited polymorphic functionallity.
Definition: collections.f90:93
Defines a generic, linked-list container.
Defines a generic, dynamically sizable list.