-
Notifications
You must be signed in to change notification settings - Fork 0
/
intlist.c
120 lines (112 loc) · 3.58 KB
/
intlist.c
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
// src/aksl/intlist.c 2017-10-25 Alan U. Kennington.
/*-----------------------------------------------------------------------------
Copyright (C) 1989-2018, Alan U. Kennington.
You may distribute this software under the terms of Alan U. Kennington's
modified Artistic Licence, as specified in the accompanying LICENCE file.
-----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
Functions in this file:
intlist::
find
add
intkeylist::
insert_inc
insert_dec
find_data
find_key
------------------------------------------------------------------------------*/
#include "aksl/intlist.h"
//----------------------//
// intlist::find //
//----------------------//
intlink* intlist::find(long i) const {
intlink* p = 0;
forall(p, *this)
if (p->i == i)
break;
return p;
} // End of function intlist::find.
/*------------------------------------------------------------------------------
intlist::add() is a sorting list insert.
------------------------------------------------------------------------------*/
//----------------------//
// intlist::add //
//----------------------//
void intlist::add(long i) {
intlink* x = new intlink(i);
intlink* q = 0;
Forall(intlink, p, *this) {
if (p->i > i)
break;
q = p;
}
insertafter(q, x);
} // End of function intlist::add.
/*------------------------------------------------------------------------------
This function will over-write any previous value for the given key.
This function maintains increasing order by key value.
------------------------------------------------------------------------------*/
//--------------------------//
// intkeylist::insert_inc //
//--------------------------//
intkey* intkeylist::insert_inc(long data, long key) {
intkey *p = 0, *q = 0;
forall(p, *this) {
if (p->key >= key)
break;
q = p;
}
if (p && p->key == key) {
p->data = data;
return p;
}
p = new intkey(data, key);
insertafter(q, p);
return p;
} // End of function intkeylist::insert_inc.
/*------------------------------------------------------------------------------
This function will over-write any previous value for the given key.
This function maintains decreasing order by key value.
------------------------------------------------------------------------------*/
//--------------------------//
// intkeylist::insert_dec //
//--------------------------//
intkey* intkeylist::insert_dec(long data, long key) {
intkey *p = 0, *q = 0;
forall(p, *this) {
if (p->key <= key)
break;
q = p;
}
if (p && p->key == key) {
p->data = data;
return p;
}
p = new intkey(data, key);
insertafter(q, p);
return p;
} // End of function intkeylist::insert_dec.
//--------------------------//
// intkeylist::find_data //
//--------------------------//
intkey* intkeylist::find_data(long& data, long key) {
intkey* p = 0;
forall(p, *this)
if (p->key == key)
break;
if (p)
data = p->data;
return p;
} // End of function intkeylist::find_data.
//--------------------------//
// intkeylist::find_key //
//--------------------------//
intkey* intkeylist::find_key(long data, long& key) {
intkey* p = 0;
forall(p, *this)
if (p->data == data)
break;
if (p)
key = p->key;
return p;
} // End of function intkeylist::find_key.