home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / gen / VQueue.ccP < prev    next >
Text File  |  1996-10-12  |  2KB  |  84 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include <stream.h>
  23. #include "<T>.VQueue.h"
  24.  
  25. <T>VQueue::<T>VQueue(<T>VQueue& b)
  26. :size(b.size), cnt(b.cnt), inp(b.inp), outp(b.outp), s(new <T> [b.size])
  27. {
  28.   int j = outp;
  29.   for (int i = 0; i < cnt; ++i)
  30.   {
  31.     s[j] = b.s[j];
  32.     if (++j == size) j = 0;
  33.   }
  34. }
  35.  
  36. void <T>VQueue::operator = (<T>VQueue& b)
  37. {
  38.   if (&b == this) return;
  39.   if (size != b.size)
  40.   {
  41.     delete [] s;
  42.     s = new <T> [b.size];
  43.     size = b.size;
  44.   }
  45.   inp = b.inp; outp = b.outp; cnt = b.cnt;
  46.   int j = outp;
  47.   for (int i = 0; i < cnt; ++i)
  48.   {
  49.     s[j] = b.s[j];
  50.     if (++j == size) j = 0;
  51.   }
  52. }
  53.  
  54.  
  55. void <T>VQueue::resize(int newsz)
  56. {
  57.   if (newsz < cnt)
  58.     error("resize: new size too small");
  59.   <T>* news = new <T> [newsz];
  60.   int j = outp;
  61.   for (int i = 0; i < cnt; ++i)
  62.   {
  63.     news[i] = s[j];
  64.     if (++j == size) j = 0;
  65.   }
  66.   inp = cnt;
  67.   outp = 0;
  68.   delete [] s;
  69.   s = news;
  70.   size = newsz;
  71. }
  72.  
  73. int <T>VQueue::OK()
  74. {
  75.   int v = s != 0;               // have space
  76.   v &= size >= 0;               // a legal size
  77.   v &= inp >= 0 && inp <= size; // pointers with bounds  
  78.   v &= outp >= 0 && outp <= size;
  79.   int c = (size + inp - outp) % size;
  80.   v &= cnt == size || cnt == c; // correct count
  81.   if (!v) error("invariant failure");
  82.   return v;
  83. }
  84.