Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9896 akron1 1
(*
2
    Copyright 2018, 2020 Anton Krotov
3
 
4
    This file is part of fb2read.
5
 
6
    fb2read is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    fb2read is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with fb2read. If not, see .
18
*)
19
 
20
MODULE LISTS;
21
 
22
 
23
TYPE
24
 
25
    LIST* = POINTER TO rLIST;
26
 
27
    ITEM* = POINTER TO rITEM;
28
 
29
    rITEM* = RECORD
30
 
31
        prev*, next*: ITEM;
32
 
33
        destroy*: PROCEDURE (VAR item: ITEM)
34
 
35
    END;
36
 
37
    rLIST* = RECORD
38
 
39
        first*, last*: ITEM
40
 
41
    END;
42
 
43
 
44
PROCEDURE push* (list: LIST; item: ITEM);
45
BEGIN
46
    ASSERT(list # NIL);
47
    ASSERT(item # NIL);
48
 
49
    IF list.first = NIL THEN
50
        list.first := item;
51
        list.last  := item;
52
        item.prev  := NIL;
53
        item.next  := NIL
54
    ELSE
55
        ASSERT(list.last # NIL);
56
        item.prev := list.last;
57
        list.last.next := item;
58
        item.next := NIL;
59
        list.last := item
60
    END
61
END push;
62
 
63
 
64
PROCEDURE get* (list: LIST; n: INTEGER): ITEM;
65
VAR
66
    cur: ITEM;
67
 
68
BEGIN
69
    cur := list.first;
70
    WHILE (cur # NIL) & (n > 0) DO
71
        cur := cur.next;
72
        DEC(n)
73
    END
74
 
75
    RETURN cur
76
END get;
77
 
78
 
79
PROCEDURE idx* (list: LIST; item: ITEM): INTEGER;
80
VAR
81
    cur: ITEM;
82
    n: INTEGER;
83
 
84
BEGIN
85
    ASSERT(item # NIL);
86
    n := 0;
87
    cur := list.first;
88
    WHILE (cur # NIL) & (cur # item) DO
89
        cur := cur.next;
90
        INC(n)
91
    END;
92
 
93
    IF cur = NIL THEN
94
        n := -1
95
    END
96
 
97
    RETURN n
98
END idx;
99
 
100
 
101
PROCEDURE create* (list: LIST): LIST;
102
BEGIN
103
    IF list = NIL THEN
104
        NEW(list)
105
    END;
106
 
107
    list.first := NIL;
108
    list.last  := NIL
109
 
110
    RETURN list
111
END create;
112
 
113
 
114
PROCEDURE destroy* (VAR list: LIST);
115
VAR
116
    item, next: ITEM;
117
 
118
BEGIN
119
    IF list # NIL THEN
120
        item := list.first;
121
        WHILE item # NIL DO
122
            next := item.next;
123
            IF item.destroy # NIL THEN
124
                item.destroy(item)
125
            ELSE
126
                DISPOSE(item)
127
            END;
128
            item := next
129
        END;
130
        DISPOSE(list)
131
    END
132
END destroy;
133
 
134
 
135
END LISTS.