forked from NWNX/nwnx2-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
148 lines (116 loc) · 3.45 KB
/
lists.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Miranda IM: the free IM client for Microsoft* Windows*
Copyright 2000-2007 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "commonheaders.h"
/* a simple sorted list implementation */
SortedList* List_Create( int p_limit, int p_increment )
{
SortedList* result = ( SortedList* )malloc( sizeof( SortedList ));
if ( result == NULL )
return(NULL);
result->increment = p_increment;
result->limit = p_limit;
return(result);
}
void List_Destroy( SortedList* p_list )
{
if ( p_list == NULL )
return;
if ( p_list->items != NULL ) {
free( p_list->items );
p_list->items = NULL;
}
p_list->realCount = p_list->limit = 0;
}
void* List_Find( SortedList* p_list, void* p_value )
{
int index;
if ( !List_GetIndex( p_list, p_value, &index ))
return(NULL);
return(p_list->items[ index ]);
}
int List_GetIndex( SortedList* p_list, void* p_value, int* p_index )
{
if ( p_list->sortFunc != NULL )
{
int low = 0;
int high = p_list->realCount-1;
while( low <= high )
{
int i = ( low+high )/2;
int result = p_list->sortFunc( p_list->items[ i ], p_value );
if ( result == 0 )
{ *p_index = i;
return 1;
}
if ( result < 0 )
low = i+1;
else
high = i-1;
}
*p_index = low;
}
return 0;
}
int List_IndexOf( SortedList* p_list, void* p_value )
{
int i;
for ( i=0; i < p_list->realCount; i++ )
if ( p_list->items[i] == p_value )
return i;
return -1;
}
int List_Insert( SortedList* p_list, void* p_value, int p_index)
{
if ( p_value == NULL || p_index > p_list->realCount )
return 0;
if ( p_list->realCount == p_list->limit )
{
p_list->items = ( void** )realloc( p_list->items, sizeof( void* )*(p_list->realCount + p_list->increment));
p_list->limit += p_list->increment;
}
if ( p_index < p_list->realCount )
memmove( p_list->items+p_index+1, p_list->items+p_index, sizeof( void* )*( p_list->realCount-p_index ));
p_list->realCount++;
p_list->items[ p_index ] = p_value;
return 1;
}
int List_InsertPtr( SortedList* list, void* p )
{
int idx = list->realCount;
List_GetIndex( list, p, &idx );
return List_Insert( list, p, idx );
}
int List_Remove( SortedList* p_list, int index )
{
if ( index < 0 || index > p_list->realCount )
return(0);
p_list->realCount--;
if ( p_list->realCount > index )
{
memmove( p_list->items+index, p_list->items+index+1, sizeof( void* )*( p_list->realCount-index ));
p_list->items[ p_list->realCount ] = NULL;
}
return 1;
}
int List_RemovePtr( SortedList* list, void* p )
{
int idx = -1;
if ( List_GetIndex( list, p, &idx ))
List_Remove( list, idx );
return idx;
}