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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****************************************************************************
List.h
This file implements singly and doubly linked list templates for
homomorphic lists.
There are two main data structures defined for each list, a link cell
and a list descriptor. Both are parameterized by template object class.
The link cell and list descriptor are named as follows:
list type 1-linked list 2-linked list queue
--------- ------------- ------------- -----
link cell SLink<C> Link<C> Link<C>
list descriptor SLL<C> DLL<C> Queue<C>
The list descriptor contains state about the lists (for example: head and
tail pointers) and supports list manipulation methods.
The link cell strings objects together in the list, and is normally part
of the object itself. An SLink only points to the next object. A Link
points both to the previous and the next object in a list.
The link() method can help find the location of the location of the link
cell within an object, given the location of the link cell in another
object. This is useful when iterating along lists.
****************************************************************************/
#ifndef _List_h_
#define _List_h_
#include <stdint.h>
#include "ink_bool.h"
#include "ink_assert.h"
#include "ink_queue.h"
#include "ink_resource.h"
#include "Compatability.h"
#include "defalloc.h"
//
// Link cell for singly-linked list of objects of type C.
//
template <class C> class SLink {
public:
C *next;
SLink() : next(NULL) {};
};
#define SLINK(_c,_f) class Link##_##_f : public SLink<_c> { public: \
static _c *& next_link(_c *c) { return c->_f.next; } \
}; SLink<_c> _f
#define SLINKM(_c,_m,_f) class Link##_##_m##_##_f : public SLink<_c> { public: \
static _c *& next_link(_c *c) { return c->_m._f.next; } \
};
//
// Link cell for doubly-linked list of objects of type C.
//
template <class C> struct Link : public SLink<C> {
C *prev;
Link() : prev(NULL) {}
};
#define LINK(_c,_f) class Link##_##_f : public Link<_c> { public: \
static _c *& next_link(_c *c) { return c->_f.next; } \
static _c *& prev_link(_c *c) { return c->_f.prev; } \
}; Link<_c> _f
#define LINKM(_c,_m,_f) class Link##_##_m##_##_f : public Link<_c> { public: \
static _c *& next_link(_c *c) { return c->_m._f.next; } \
static _c *& prev_link(_c *c) { return c->_m._f.prev; } \
};
#define LINK_FORWARD_DECLARATION(_c,_f) class Link##_##_c##_##_f : public Link<_c> { public: \
static _c *& next_link(_c *c); \
static _c *& prev_link(_c *c); \
};
#define LINK_DEFINITION(_c,_f) \
inline _c *& Link##_##_c##_##_f::next_link(_c *c) { return c->_f.next; } \
inline _c *& Link##_##_c##_##_f::prev_link(_c *c) { return c->_f.prev; } \
//
// List descriptor for singly-linked list of objects of type C.
//
template <class C, class L = typename C::Link_link> class SLL {
public:
C *head;
bool empty() { return head == NULL; }
void push(C *e);
C *pop();
void clear() { head = NULL; }
C *& next(C *e) { return L::next_link(e); }
SLL() : head(NULL) {}
SLL(C *c) : head(c) {}
};
#define SList(_c, _f) SLL<_c, _c::Link##_##_f>
#define SListM(_c, _m, _ml, _l) SLL<_c, _c::Link##_##_ml##_##_l>
#define forl_LL(_c, _p, _l) for (_c *_p = (_l).head; _p; _p = (_l).next(_p))
template <class C, class L> inline void
SLL<C,L>::push(C *e) {
next(e) = head;
head = e;
}
template <class C, class L> inline C *
SLL<C,L>::pop() {
C *ret = head;
if (ret) {
head = next(ret);
next(ret) = NULL;
}
return ret;
}
//
// List descriptor for doubly-linked list of objects of type C.
//
template <class C, class L = typename C::Link_link> struct DLL {
C *head;
bool empty() { return head == NULL; }
void push(C *e);
C *pop();
void remove(C *e);
void insert(C *e, C *after);
bool in(C *e) { return head == e || next(e) || prev(e); }
void clear() { head = NULL; }
C *&next(C *e) { return *(C**)&L::next_link(e); }
C *&prev(C *e) { return *(C**)&L::prev_link(e); }
DLL() : head(NULL) {}
};
#define DList(_c, _f) DLL<_c, _c::Link##_##_f>
#define DListM(_c, _m, _ml, _l) DLL<_c, _c::Link##_##_ml##_##_l>
template <class C, class L> inline void
DLL<C,L>::push(C *e) {
if (head)
prev(head) = e;
next(e) = head;
head = e;
}
template <class C, class L> inline void
DLL<C,L>::remove(C *e) {
if (!head) return;
if (e == head) head = next(e);
if (prev(e)) next(prev(e)) = next(e);
if (next(e)) prev(next(e)) = prev(e);
prev(e) = NULL;
next(e) = NULL;
}
template <class C, class L> inline C *
DLL<C,L>::pop() {
C *ret = head;
if (ret) {
head = next(ret);
if (head)
prev(head) = NULL;
next(ret) = NULL;
return ret;
} else
return NULL;
}
template <class C, class L> inline void
DLL<C,L>::insert(C *e, C *after) {
if (!after) { push(e); return; }
prev(e) = after;
next(e) = next(after);
next(after) = e;
if (next(e)) prev(next(e)) = e;
}
//
// List descriptor for queue of objects of type C.
//
template <class C, class L = typename C::Link_link> class Queue : public DLL<C,L> {
public:
using DLL<C,L>::head;
C *tail;
void push(C *e);
C *pop();
void enqueue(C *e);
void in_or_enqueue(C *e);
C *dequeue();
void remove(C *e);
void insert(C *e, C *after);
void append(Queue<C,L> q);
void append(DLL<C,L> q);
void clear() { head = NULL; tail = NULL; }
Queue() : tail(NULL) {}
};
#define Que(_c, _f) Queue<_c, _c::Link##_##_f>
#define QueM(_c, _m, _mf, _f) Queue<_c, _c::Link##_##_mf##_##_f>
template <class C, class L> inline void
Queue<C,L>::push(C *e) {
DLL<C,L>::push(e);
if (!tail) tail = head;
}
template <class C, class L> inline C *
Queue<C,L>::pop() {
C *ret = DLL<C,L>::pop();
if (!head) tail = NULL;
return ret;
}
template <class C, class L> inline void
Queue<C,L>::insert(C *e, C *after) {
DLL<C,L>::insert(e, after);
if (!tail)
tail = head;
else if (tail == after)
tail = e;
}
template <class C, class L> inline void
Queue<C,L>::remove(C *e) {
if (tail == e)
tail = (C*)this->prev(e);
DLL<C,L>::remove(e);
}
template <class C, class L> inline void
Queue<C,L>::append(DLL<C,L> q) {
C *qtail = q.head;
if (qtail)
while (this->next(qtail))
qtail = this->next(qtail);
if (!head) {
head = q.head;
tail = qtail;
} else {
if (q.head) {
this->next(tail) = q.head;
this->prev(q.head) = tail;
tail = qtail;
}
}
}
template <class C, class L> inline void
Queue<C,L>::append(Queue<C,L> q) {
if (!head) {
head = q.head;
tail = q.tail;
} else {
if (q.head) {
this->next(tail) = q.head;
this->prev(q.head) = tail;
tail = q.tail;
}
}
}
template <class C, class L> inline void
Queue<C,L>::enqueue(C *e) {
if (tail)
insert(e, tail);
else
push(e);
}
template <class C, class L> inline void
Queue<C,L>::in_or_enqueue(C *e) {
if (!this->in(e)) enqueue(e);
}
template <class C, class L> inline C *
Queue<C,L>::dequeue() {
return pop();
}
//
// Adds sorting, but requires that elements implement <
//
template<class C, class L = typename C::Link_link> struct SortableQueue: public Queue<C,L>
{
using DLL<C,L>::head;
using Queue<C,L>::tail;
void sort() {
if (!head) return;
bool clean = false;
while (!clean) {
clean = true;
C *v = head;
C *n = this->next(head);
while (n) {
C *f = this->next(n);
if (*n < *v) {
clean = false;
// swap 'em
if (head == v)
head = n;
if (tail == n)
tail = v;
// fix prev (p)
C *p = this->prev(v);
if (p) {
this->next(p) = n;
this->prev(n) = p;
} else
this->prev(n) = NULL;
// fix follow (f)
if (f) {
this->prev(f) = v;
this->next(v) = f;
} else
this->next(v) = NULL;
// fix interior
this->prev(v) = n;
this->next(n) = v;
} else
v = n;
n = f;
}
}
}
};
#define SortableQue(_c, _l) SortableQueue<_c, _c::Link##_##_f>
//
// Adds counting to the Queue
//
template <class C, class L = typename C::Link_link> struct CountQueue : public Queue<C,L> {
int size;
inline CountQueue(void) : size(0) {}
inline void push(C *e);
inline C *pop();
inline void enqueue(C *e);
inline C *dequeue();
inline void remove(C *e);
inline void insert(C *e, C *after);
inline void append(CountQueue<C,L> &q);
inline void append_clear(CountQueue<C,L> &q);
};
#define CountQue(_c, _f) CountQueue<_c, _c::Link##_##_f>
#define CountQueM(_c, _m, _mf, _f) CountQueue<_c, _c::Link##_##_mf##_##_f>
template <class C, class L> inline void
CountQueue<C,L>::push(C *e) {
Queue<C,L>::push(e);
size++;
}
template <class C, class L> inline C *
CountQueue<C,L>::pop() {
C *ret = Queue<C,L>::pop();
if (ret)
size--;
return ret;
}
template <class C, class L> inline void
CountQueue<C,L>::remove(C *e) {
Queue<C,L>::remove(e);
size--;
}
template <class C, class L> inline void
CountQueue<C,L>::enqueue(C *e) {
Queue<C,L>::enqueue(e);
size++;
}
template <class C, class L> inline C *
CountQueue<C,L>::dequeue() {
return pop();
}
template <class C, class L> inline void
CountQueue<C,L>::insert(C *e, C *after) {
Queue<C,L>::insert(e, after);
size++;
}
template <class C, class L> inline void
CountQueue<C,L>::append(CountQueue<C,L> &q) {
Queue<C,L>::append(q);
size += q.size;
};
template <class C, class L> inline void
CountQueue<C,L>::append_clear(CountQueue<C,L> &q) {
append(q);
q.head = q.tail = 0;
q.size = 0;
}
//
// List using cons cells
//
template <class C, class A = DefaultAlloc>
struct ConsCell {
C car;
ConsCell *cdr;
ConsCell(C acar, ConsCell *acdr) : car(acar), cdr(acdr) {}
ConsCell(C acar) : car(acar), cdr(NULL) {}
ConsCell(ConsCell *acdr) : cdr(acdr) {}
static void *operator new(size_t size) { return A::alloc(size); }
static void operator delete(void *p, size_t size) { A::free(p); }
};
template <class C, class A = DefaultAlloc>
struct List {
ConsCell<C,A> *head;
C first() { if (head) return head->car; else return 0; }
C car() { return first(); }
ConsCell<C,A> *rest() { if (head) return head->cdr; else return 0; }
ConsCell<C,A> *cdr() { return rest(); }
void push(C a) { head = new ConsCell<C,A>(a, head); }
void push() { head = new ConsCell<C,A>(head); }
C pop() { C a = car(); head = cdr(); return a; }
void clear() { head = NULL; }
void reverse();
List(C acar) : head(new ConsCell<C,A>(acar)) {}
List(C a, C b) : head(new ConsCell<C,A>(a, new ConsCell<C,A>(b))) {}
List(C a, C b, C c) : head(new ConsCell<C,A>(a, new ConsCell<C,A>(b, new ConsCell<C,A>(c)))) {}
List() : head(0) {}
};
#define forc_List(_c, _p, _l) if ((_l).head) for (_c *_p = (_l).head; _p; _p = _p->cdr)
template <class C,class A> void
List<C,A>::reverse() {
ConsCell<C,A> *n, *t;
for (ConsCell<C,A> *p = head; p; p = n) {
n = p->cdr;
p->cdr = t;
t = p;
}
head = t;
}
//
// Atomic lists
//
template<class C, class L = typename C::Link_link> struct AtomicSLL
{
void push(C * c) { ink_atomiclist_push(&al, c); }
C *pop() { return (C *) ink_atomiclist_pop(&al); }
C *popall() { return (C *) ink_atomiclist_popall(&al); }
bool empty() { return INK_ATOMICLIST_EMPTY(al); }
/*
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING
* only if only one thread is doing pops it is possible to have a "remove"
* which only that thread can use as well.
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING
*/
C *remove(C * c) { return (C *) ink_atomiclist_remove(&al, c); }
C *head() { return (C *) TO_PTR(FREELIST_POINTER(al.head)); }
C *next(C * c) { return (C *) TO_PTR(c); }
InkAtomicList al;
AtomicSLL();
};
#define ASLL(_c, _l) AtomicSLL<_c, _c::Link##_##_l>
#define ASLLM(_c, _m, _ml, _l) AtomicSLL<_c, _c::Link##_##_ml##_##_l>
template<class C, class L> inline AtomicSLL<C,L>::AtomicSLL() {
ink_atomiclist_init(&al, "AtomicSLL", (uint32_t)(uintptr_t)&L::next_link((C*)0));
}
#endif /*_List_h_*/
|