… | |
… | |
333 | |
333 | |
334 | typedef tausworthe_random_generator rand_gen; |
334 | typedef tausworthe_random_generator rand_gen; |
335 | |
335 | |
336 | extern rand_gen rndm; |
336 | extern rand_gen rndm; |
337 | |
337 | |
|
|
338 | INTERFACE_CLASS (attachable) |
|
|
339 | struct refcnt_base |
|
|
340 | { |
|
|
341 | typedef int refcnt_t; |
|
|
342 | mutable refcnt_t ACC (RW, refcnt); |
|
|
343 | |
|
|
344 | MTH void refcnt_inc () const { ++refcnt; } |
|
|
345 | MTH void refcnt_dec () const { --refcnt; } |
|
|
346 | |
|
|
347 | refcnt_base () : refcnt (0) { } |
|
|
348 | }; |
|
|
349 | |
|
|
350 | extern refcnt_base::refcnt_t refcnt_dummy; |
|
|
351 | |
338 | template<class T> |
352 | template<class T> |
339 | struct refptr |
353 | struct refptr |
340 | { |
354 | { |
|
|
355 | // p if not null |
|
|
356 | refcnt_base::refcnt_t *refcnt_ref () { return p ? &p->refcnt : &refcnt_dummy; } |
|
|
357 | |
|
|
358 | void refcnt_dec () |
|
|
359 | { |
|
|
360 | if (!is_constant (p)) |
|
|
361 | --*refcnt_ref (); |
|
|
362 | else if (p) |
|
|
363 | --p->refcnt; |
|
|
364 | } |
|
|
365 | |
|
|
366 | void refcnt_inc () |
|
|
367 | { |
|
|
368 | if (!is_constant (p)) |
|
|
369 | ++*refcnt_ref (); |
|
|
370 | else if (p) |
|
|
371 | ++p->refcnt; |
|
|
372 | } |
|
|
373 | |
341 | T *p; |
374 | T *p; |
342 | |
375 | |
343 | refptr () : p(0) { } |
376 | refptr () : p(0) { } |
344 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
377 | refptr (const refptr<T> &p) : p(p.p) { refcnt_inc (); } |
345 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
378 | refptr (T *p) : p(p) { refcnt_inc (); } |
346 | ~refptr () { if (p) p->refcnt_dec (); } |
379 | ~refptr () { refcnt_dec (); } |
347 | |
380 | |
348 | const refptr<T> &operator =(T *o) |
381 | const refptr<T> &operator =(T *o) |
349 | { |
382 | { |
|
|
383 | // if decrementing ever destroys we need to reverse the order here |
350 | if (p) p->refcnt_dec (); |
384 | refcnt_dec (); |
351 | p = o; |
385 | p = o; |
352 | if (p) p->refcnt_inc (); |
386 | refcnt_inc (); |
353 | |
|
|
354 | return *this; |
387 | return *this; |
355 | } |
388 | } |
356 | |
389 | |
357 | const refptr<T> &operator =(const refptr<T> o) |
390 | const refptr<T> &operator =(const refptr<T> &o) |
358 | { |
391 | { |
359 | *this = o.p; |
392 | *this = o.p; |
360 | return *this; |
393 | return *this; |
361 | } |
394 | } |
362 | |
395 | |
363 | T &operator * () const { return *p; } |
396 | T &operator * () const { return *p; } |
364 | T *operator ->() const { return p; } |
397 | T *operator ->() const { return p; } |
365 | |
398 | |
366 | operator T *() const { return p; } |
399 | operator T *() const { return p; } |
367 | }; |
400 | }; |
368 | |
401 | |
369 | typedef refptr<maptile> maptile_ptr; |
402 | typedef refptr<maptile> maptile_ptr; |
… | |
… | |
405 | return !strcmp (a, b); |
438 | return !strcmp (a, b); |
406 | } |
439 | } |
407 | }; |
440 | }; |
408 | |
441 | |
409 | // Mostly the same as std::vector, but insert/erase can reorder |
442 | // Mostly the same as std::vector, but insert/erase can reorder |
410 | // the elements, making insret/remove O(1) instead of O(n). |
443 | // the elements, making append(=insert)/remove O(1) instead of O(n). |
411 | // |
444 | // |
412 | // NOTE: only some forms of erase/insert are available |
445 | // NOTE: only some forms of erase are available |
413 | template<class T> |
446 | template<class T> |
414 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
447 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
415 | { |
448 | { |
416 | typedef typename unordered_vector::iterator iterator; |
449 | typedef typename unordered_vector::iterator iterator; |
417 | |
450 | |
… | |
… | |
458 | return obj->*indexmember |
491 | return obj->*indexmember |
459 | ? this->begin () + obj->*indexmember - 1 |
492 | ? this->begin () + obj->*indexmember - 1 |
460 | : this->end (); |
493 | : this->end (); |
461 | } |
494 | } |
462 | |
495 | |
|
|
496 | void push_back (T *obj) |
|
|
497 | { |
|
|
498 | std::vector<T *, slice_allocator<T *> >::push_back (obj); |
|
|
499 | obj->*indexmember = this->size (); |
|
|
500 | } |
|
|
501 | |
463 | void insert (T *obj) |
502 | void insert (T *obj) |
464 | { |
503 | { |
465 | push_back (obj); |
504 | push_back (obj); |
466 | obj->*indexmember = this->size (); |
|
|
467 | } |
505 | } |
468 | |
506 | |
469 | void insert (T &obj) |
507 | void insert (T &obj) |
470 | { |
508 | { |
471 | insert (&obj); |
509 | insert (&obj); |