]>
cygwin.com Git - cygwin-apps/setup.git/blob - list.h
2 * Copyright (c) 2001, Robert Collins.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Robert Collins <rbtcollins@hotmail.com>
20 #include "sys/types.h"
22 /* Creates a list/array of type T (must have a field key of type U),
23 * with index type U and U comparison Ucmp.
25 template < class T
, class U
, int
34 list ():pointerblock (0), _number (0), _space (0)
38 /* get an element if it exists */
39 T
*getbykey (const U
);
40 /* add an element if it does not exist and return a reference */
41 T
& registerbykey (U
);
42 /* add an element if one with the same key does not exist */
43 T
& registerbyobject (T
&);
45 T
*removebyindex (size_t);
46 size_t number () const
50 /* get by offset - not thread safe - starts at 1 */
51 T
*operator [] (size_t n
) const
53 return n
&& n
<= _number
? pointerblock
[n
- 1] : 0;
57 // ensure there is enough array space.
58 void checksize (void);
59 // perform an insertion.
60 void insert (size_t, T
*);
63 template < class T
, class U
, int
65 T
* list
< T
, U
, Ucmp
>::getbykey (const U key
)
67 for (size_t n
= 0; n
< _number
; n
++)
68 if (Ucmp (pointerblock
[n
]->key
, key
) == 0)
69 return pointerblock
[n
];
74 template < class T
, class U
, int
76 T
& list
< T
, U
, Ucmp
>::registerbykey (U key
)
78 T
*tempT
= getbykey (key
);
84 n
< _number
&& Ucmp (pointerblock
[n
]->key
, tempT
->key
) < 0; n
++);
90 /* TODO: factor both insertion routines into one */
91 template < class T
, class U
, int
93 T
& list
< T
, U
, Ucmp
>::registerbyobject (T
& newobj
)
95 T
*tempT
= getbykey (newobj
.key
);
101 n
< _number
&& Ucmp (pointerblock
[n
]->key
, tempT
->key
) < 0; n
++);
107 template < class T
, class U
, int
109 T
* list
< T
, U
, Ucmp
>::removebyindex (size_t index
)
111 if (index
&& index
<= _number
)
113 T
*rv
= pointerblock
[index
- 1];
114 /* remove from index - 1 */
115 for (size_t i
= index
; i
< _number
; ++i
)
116 pointerblock
[i
- 1] = pointerblock
[i
];
123 template < class T
, class U
, int
125 list
< T
, U
, Ucmp
>::checksize ()
127 if (_number
== _space
)
129 T
**newptr
= new (T
*)[_space
+ 20];
132 //die - todo enable exceptions
135 for (size_t i
= 0; i
< _number
; ++i
)
136 newptr
[i
] = pointerblock
[i
];
137 delete[]pointerblock
;
138 pointerblock
= newptr
;
139 /* TODO: Parameterise this too */
143 template < class T
, class U
, int
145 list
< T
, U
, Ucmp
>::insert (size_t where
, T
* someT
)
147 // doesn't change where
149 /* insert at where */
150 for (size_t i
= _number
; where
< i
; --i
)
151 pointerblock
[i
] = pointerblock
[i
- 1];
152 pointerblock
[where
] = someT
;
157 #endif /* _LIST_H_ */
This page took 0.042142 seconds and 5 git commands to generate.