]>
Commit | Line | Data |
---|---|---|
94f895e4 | 1 | |
2 | /* | |
3 | * $Id: List.h,v 1.1 2003/02/25 12:07:41 robertc Exp $ | |
4 | * | |
5 | * | |
6 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
7 | * ---------------------------------------------------------- | |
8 | * | |
9 | * Squid is the result of efforts by numerous individuals from | |
10 | * the Internet community; see the CONTRIBUTORS file for full | |
11 | * details. Many organizations have provided support for Squid's | |
12 | * development; see the SPONSORS file for full details. Squid is | |
13 | * Copyrighted (C) 2001 by the Regents of the University of | |
14 | * California; see the COPYRIGHT file for full details. Squid | |
15 | * incorporates software developed and/or copyrighted by other | |
16 | * sources; see the CREDITS file for full details. | |
17 | * | |
18 | * This program is free software; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation; either version 2 of the License, or | |
21 | * (at your option) any later version. | |
22 | * | |
23 | * This program is distributed in the hope that it will be useful, | |
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | * GNU General Public License for more details. | |
27 | * | |
28 | * You should have received a copy of the GNU General Public License | |
29 | * along with this program; if not, write to the Free Software | |
30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
31 | * | |
32 | */ | |
33 | ||
34 | #ifndef SQUID_LIST_H | |
35 | #define SQUID_LIST_H | |
36 | ||
37 | template <class C> | |
38 | class List | |
39 | { | |
40 | ||
41 | public: | |
42 | void *operator new (size_t); | |
43 | void operator delete (void *); | |
44 | void deleteSelf() const; | |
45 | List (C const &); | |
46 | ~List(); | |
47 | ||
48 | bool find(C const &)const; | |
49 | bool findAndTune(C const &); | |
50 | List *next; | |
51 | C element; | |
52 | private: | |
53 | static MemPool *Pool; | |
54 | }; | |
55 | ||
56 | /* implementation follows */ | |
57 | ||
58 | template <class C> | |
59 | MemPool *List<C>::Pool(NULL); | |
60 | ||
61 | template <class C> | |
62 | void * | |
63 | List<C>::operator new (size_t byteCount) | |
64 | { | |
65 | /* derived classes with different sizes must implement their own new */ | |
66 | assert (byteCount == sizeof (List<C>)); | |
67 | if (!Pool) | |
68 | Pool = memPoolCreate("List", sizeof (List<C>)); | |
69 | return memPoolAlloc(Pool); | |
70 | } | |
71 | ||
72 | template <class C> | |
73 | void | |
74 | List<C>::operator delete (void *address) | |
75 | { | |
76 | memPoolFree (Pool, address); | |
77 | } | |
78 | ||
79 | template <class C> | |
80 | void | |
81 | List<C>::deleteSelf() const | |
82 | { | |
83 | delete this; | |
84 | } | |
85 | ||
86 | template <class C> | |
87 | List<C>::List(C const &value) : next(NULL), element (value) | |
88 | { | |
89 | } | |
90 | ||
91 | template <class C> | |
92 | List<C>::~List() | |
93 | { | |
94 | if (next) | |
95 | next->deleteSelf(); | |
96 | } | |
97 | ||
98 | template <class C> | |
99 | bool | |
100 | List<C>::find (C const &toFind) const | |
101 | { | |
102 | List<C> const *node = NULL; | |
103 | for (node = this; node; node = node->next) | |
104 | if (node->element == toFind) | |
105 | return true; | |
106 | return false; | |
107 | } | |
108 | ||
109 | template <class C> | |
110 | bool | |
111 | List<C>::findAndTune(C const & toFind) | |
112 | { | |
113 | List<C> *prev = NULL; | |
114 | for (List<C> *node = this; node; node = node->next) { | |
115 | if (node->element == toFind) { | |
116 | if (prev != NULL) { | |
117 | /* shift the element just found to the second position | |
118 | * in the list */ | |
119 | prev->next = node->next; | |
120 | node->next = this->next; | |
121 | this->next = node; | |
122 | } | |
123 | return true; | |
124 | } | |
125 | prev = node; | |
126 | } | |
127 | return false; | |
128 | } | |
129 | ||
130 | #endif /* SQUID_LIST_H */ |