home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / c / WLIB.ZIP / WVEC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-07  |  8.4 KB  |  466 lines

  1. #include "VecLocal.h"
  2. #pragma hdrstop
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5.  
  6. //.parse
  7.  
  8. BitSetRef::BitSetRef(Byte* BP, int BitOffset)
  9.   {
  10.     P=BP+size_t(BitOffset/8);
  11.     M=Byte(1<<int(BitOffset%8));
  12.   }
  13.  
  14. //.parse
  15.  
  16. ByteVector::ByteVector(File& F)
  17.   {
  18.     Extra=DefaultVectorExtra;
  19.     if (F.CurPos()>F.Size())
  20.       {
  21.         Len=0;
  22.         Alloc=Extra;
  23.         P = VecNew(Alloc);
  24.       }
  25.     else
  26.       {
  27.         F.ReadThing(Len);
  28.         if (Len)
  29.           {
  30.             Alloc=Len+Extra;
  31.             P =VecNew(Alloc);
  32.             if (Len)
  33.               {
  34.                 F.Read(P,int(Len));
  35.               }
  36.           }
  37.       }
  38.   }
  39.  
  40. //.parse
  41.  
  42. long ByteVector::Sum()
  43.   {
  44.     long I;
  45.     long A=0;
  46.     For(I,Len) A+=*PtrInc(P,I);
  47.     return A;
  48.   }
  49.  
  50. //.parse
  51.  
  52. long ByteVector::Index(Byte SearchByte, long StartIndex)
  53.   {
  54.     if (StartIndex>=Len) return(VecObjNotFound);
  55.     long I=StartIndex;
  56.     while ((I<Len) && ((*PtrInc(P,I))!=SearchByte)) I++;
  57.     if (I==Len) return(VecObjNotFound);
  58.     else return(I);
  59.   }
  60.  
  61. //.parse
  62.  
  63. ByteVector ByteVector::From(long Index)
  64.   {
  65.     ByteVector BV;
  66.     if (Len>Index) BV=At(Index,Len-Index);
  67.     return BV;
  68.   }
  69.  
  70. //.parse
  71.  
  72. ByteVector ByteVector::After(long Index)
  73.   {
  74.     ByteVector BV;
  75.     if (Len>(Index+1)) BV=At(Index+1,Len-Index-1);
  76.     return BV;
  77.   }
  78.  
  79. //.parse
  80.  
  81. Bool ByteVector::operator==(const ByteVector& B)
  82.   {
  83.     if (Len != B.Len) return False;
  84.     if (P==B.P) return True;
  85.     long I;
  86.     For(I,Len)
  87.         if ((*PtrInc(P,I))!=(*PtrInc(B.P,I))) return False;
  88.     return True;
  89.   }
  90.  
  91. //.parse
  92.  
  93. void ByteVector::Delete(long Index,long Length)
  94.   {
  95.     if (Length<1) return;
  96.     if (Index<0) Index=0;
  97.     if (Index+Length>=Len) Len=Index;
  98.     else
  99.       {
  100.         Len-=Length;
  101.         long I;
  102.         for(I=Index;I<Len;I++) *PtrInc(P,I)=*PtrInc(P,I+Length);
  103.       }
  104.   }
  105.  
  106. //.parse
  107.  
  108. void ByteVector::Clip(long NewSize)
  109.   {
  110.     if (NewSize<Len) Len=NewSize;
  111.   }
  112.  
  113. //.parse
  114.  
  115. void ByteVector::Assign(const ByteVector& BV)
  116.   {
  117.     if (P == BV.P) return;  // they're the same object
  118.     Len = BV.Len;
  119.     if (Len > Alloc)
  120.       {
  121.         FreeMemory();
  122.         Alloc = Len+Extra;
  123.         P = VecNew(Alloc);
  124.       }
  125.     BigMemCopy(P,BV.P,Len);
  126.   }
  127.  
  128. //.parse
  129.  
  130. ByteVector::ByteVector(const ByteVector& B)
  131.   {
  132.     Extra=DefaultVectorExtra;
  133.     Len = B.Len;
  134.     Alloc = Extra+Len;
  135.     P = VecNew(Alloc);
  136.     BigMemCopy(P,B.P,Len);
  137.   }
  138.  
  139. //.parse
  140.  
  141. ByteVector::ByteVector(void* B, long Length)
  142.   {
  143.     Extra=DefaultVectorExtra;
  144.     Len = Length;
  145.     Alloc = Extra+Len;
  146.     P = VecNew(Alloc);
  147.     BigMemCopy(P,B,Len);
  148.   }
  149.  
  150. //.parse
  151.  
  152. void ByteVector::WriteTo(File& F)
  153.   {
  154.     F.WriteThing(Len);
  155.     if (Len)
  156.       {
  157.         F.Write(P,size_t(Len));
  158.       }
  159.   }
  160.  
  161. //.parse
  162.  
  163. Byte* VecNew(long Quan)
  164.   {
  165.     Byte* B;
  166.     B=(Byte*)farmalloc(Quan);
  167.     if (B==NULL) FatalError("vector out of memory");
  168.     return B;
  169.   }
  170.  
  171. //.parse
  172.  
  173. Bool BigVectorJump=True;
  174.  
  175. void ByteVector::MinimizeMemory()
  176.   {
  177.     Bool Temp=BigVectorJump;
  178.     BigVectorJump=False;
  179.     ReNew(Len);
  180.     BigVectorJump=Temp;
  181.   }
  182.  
  183. //.parse
  184.  
  185. void ByteVector::ReNew(long NewCapacity)  // replaces ANSI-C realloc
  186.   {
  187.     P=(Byte*)farrealloc(P,NewCapacity);
  188.     if (P==NULL)
  189.       {
  190.         Byte huge* X=(Byte huge*)farmalloc(NewCapacity);
  191.         if (X==NULL)
  192.             FatalError("vector out of memory: "+Str(NewCapacity));
  193.         BigMemCopy(X,P,Len);
  194.         farfree(P);
  195.         P=X;
  196.       }
  197.     Alloc=NewCapacity;
  198.   }
  199.  
  200. long ByteVector::ReAlloc(long NewCapacity)
  201.   {
  202.     if (NewCapacity < Len) NewCapacity = Len;
  203.     if (Alloc != NewCapacity) ReNew(NewCapacity);
  204.     return Alloc;
  205.   }
  206.  
  207. //.parse
  208.  
  209. void ByteVector::Insert(const ByteVector& B,long Index)
  210.   {
  211.     *this=Before(Index)+B+From(Index);
  212.   }
  213.  
  214. void ByteVector::Insert(Byte B,long Index)
  215.   {
  216.     *this=Before(Index)+B+From(Index);
  217.   }
  218.  
  219. void ByteVector::Clear(Byte B)
  220.   {
  221.     long I;
  222.     For(I,Alloc) *PtrInc(P,I)=B;  // other funcs depend on clearing to alloc
  223.   }
  224.  
  225. //.parse
  226.  
  227. #define ParenGuts if (I>=Len) return 0; return (*PtrInc(P,I));
  228.  
  229. Byte ByteVector::At(long I) {ParenGuts}
  230. Byte ByteVector::operator()(long I) {ParenGuts}
  231.  
  232. void ByteVector::FreeMemory()
  233.   {
  234.     farfree(P);
  235.   }
  236.  
  237. ByteVector::ByteVector()
  238.   {
  239.     Extra=DefaultVectorExtra;
  240.     Len = 0;
  241.     Alloc = Extra;
  242.     P = VecNew(Alloc);
  243.   }
  244.  
  245. //.parse
  246.  
  247. ByteVector::ByteVector(int B)
  248.   {
  249.     Extra=DefaultVectorExtra;
  250.     Len = 1;
  251.     Alloc = Extra+1;
  252.     P = VecNew(Alloc);
  253.     P[0]=Byte(B);
  254.   }
  255.  
  256. //.parse
  257.  
  258. ByteVector::ByteVector(int B1,int B2)
  259.   {
  260.     Extra=DefaultVectorExtra;
  261.     Len = 2;
  262.     Alloc = Extra+Len;
  263.     P = VecNew(Alloc);
  264.     P[0]=Byte(B1);
  265.     P[1]=Byte(B2);
  266.   }
  267.  
  268. //.parse
  269.  
  270. ByteVector::ByteVector(int B1,int B2,int B3)
  271.   {
  272.     Extra=DefaultVectorExtra;
  273.     Len = 3;
  274.     Alloc = Extra+Len;
  275.     P = VecNew(Alloc);
  276.     P[0]=Byte(B1);
  277.     P[1]=Byte(B2);
  278.     P[2]=Byte(B3);
  279.   }
  280.  
  281. //.parse
  282.  
  283. ByteVector::ByteVector(int B1,int B2,int B3,int B4)
  284.   {
  285.     Extra=DefaultVectorExtra;
  286.     Len = 4;
  287.     Alloc = Extra+Len;
  288.     P = VecNew(Alloc);
  289.     P[0]=Byte(B1);
  290.     P[1]=Byte(B2);
  291.     P[2]=Byte(B3);
  292.     P[3]=Byte(B4);
  293.   }
  294.  
  295. //.parse
  296.  
  297. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5)
  298.   {
  299.     Extra=DefaultVectorExtra;
  300.     Len = 5;
  301.     Alloc = Extra+Len;
  302.     P = VecNew(Alloc);
  303.     P[0]=Byte(B1);
  304.     P[1]=Byte(B2);
  305.     P[2]=Byte(B3);
  306.     P[3]=Byte(B4);
  307.     P[4]=Byte(B5);
  308.   }
  309.  
  310. //.parse
  311.  
  312. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6)
  313.   {
  314.     Extra=DefaultVectorExtra;
  315.     Len = 6;
  316.     Alloc = Extra+Len;
  317.     P = VecNew(Alloc);
  318.     P[0]=Byte(B1);
  319.     P[1]=Byte(B2);
  320.     P[2]=Byte(B3);
  321.     P[3]=Byte(B4);
  322.     P[4]=Byte(B5);
  323.     P[5]=Byte(B6);
  324.   }
  325.  
  326. //.parse
  327.  
  328. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7)
  329.   {
  330.     Extra=DefaultVectorExtra;
  331.     Len = 7;
  332.     Alloc = Extra+Len;
  333.     P = VecNew(Alloc);
  334.     P[0]=Byte(B1);
  335.     P[1]=Byte(B2);
  336.     P[2]=Byte(B3);
  337.     P[3]=Byte(B4);
  338.     P[4]=Byte(B5);
  339.     P[5]=Byte(B6);
  340.     P[6]=Byte(B7);
  341.   }
  342.  
  343. //.parse
  344.  
  345. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7,int B8)
  346.   {
  347.     Extra=DefaultVectorExtra;
  348.     Len = 8;
  349.     Alloc = Extra+Len;
  350.     P = VecNew(Alloc);
  351.     P[0]=Byte(B1);
  352.     P[1]=Byte(B2);
  353.     P[2]=Byte(B3);
  354.     P[3]=Byte(B4);
  355.     P[4]=Byte(B5);
  356.     P[5]=Byte(B6);
  357.     P[6]=Byte(B7);
  358.     P[7]=Byte(B8);
  359.   }
  360.  
  361. //.parse
  362.  
  363. ByteVector::ByteVector(int B1,int B2,int B3,int B4,int B5,int B6,int B7,
  364.     int B8,int B9)
  365.   {
  366.     Extra=DefaultVectorExtra;
  367.     Len = 9;
  368.     Alloc = Extra+Len;
  369.     P = VecNew(Alloc);
  370.     P[0]=Byte(B1);
  371.     P[1]=Byte(B2);
  372.     P[2]=Byte(B3);
  373.     P[3]=Byte(B4);
  374.     P[4]=Byte(B5);
  375.     P[5]=Byte(B6);
  376.     P[6]=Byte(B7);
  377.     P[7]=Byte(B8);
  378.     P[8]=Byte(B9);
  379.   }
  380.  
  381. //.parse
  382.  
  383. void ByteVector::CopyTo(void* Dest, long Bytes)
  384.   {
  385.     if (Bytes>Len) Bytes=Len;
  386.     BigMemCopy(Dest,P,Bytes);
  387.   }
  388.  
  389. void ByteVector::CopyFrom(void* Source, long Bytes)
  390.   {
  391.     if (Alloc<Bytes)
  392.       {
  393.         FreeMemory();
  394.         Alloc=Bytes+Extra;
  395.         P=VecNew(Alloc);
  396.       }
  397.     Len=Bytes;
  398.     BigMemCopy(P,Source,Bytes);
  399.   }
  400.  
  401. //.parse
  402.  
  403. void ByteVector::operator+=(const ByteVector& BV)
  404.   {
  405.     if (Alloc <= Len + BV.Len) ReAlloc(Len+BV.Len+Extra);
  406.     BigMemCopy(PtrInc(P,Len),BV.P,BV.Len);
  407.     Len += BV.Len;
  408.   }
  409.  
  410. ByteVector operator+(Byte B, const ByteVector& BV)
  411.   {
  412.     ByteVector V(B);
  413.     V+=BV;
  414.     return V;
  415.   }
  416.  
  417. //.parse
  418.  
  419. ByteVector ByteVector::operator+(const ByteVector& B)
  420.   {
  421.     ByteVector BV=*this;
  422.     BV+=B;
  423.     return BV;
  424.   }
  425.  
  426. ByteVector ByteVector::operator+(Byte B)
  427.   {
  428.     ByteVector BV=*this;
  429.     BV+=B;
  430.     return BV;
  431.   }
  432.  
  433. //.parse
  434.  
  435. ByteVector ByteVector::operator()(long Index, long Length)
  436.   {
  437.     ByteVector B;
  438.     if ((Index>=Len)||(Length==0)) return B;
  439.     else if (Index+Length>Len) Length=Len-Index;
  440.     B.ReAlloc(Length+Extra);
  441.     B.Len=Length;
  442.     BigMemCopy(B.P,PtrInc(P,Index),Length);
  443.     return B;
  444.   }
  445.  
  446. ByteVector ByteVector::At(long Index, long Length)
  447.   {
  448.     ByteVector B=operator()(Index,Length);
  449.     return B;
  450.   }
  451.  
  452. //.parse
  453.  
  454. Byte huge& ByteVector::Ref(long Index)
  455.   {
  456.     if (Index>=Alloc)
  457.       {
  458.         ReNew(Index+Extra);
  459.         Len=Index+1;
  460.       }
  461.     else if (Index>=Len) Len=Index+1;
  462.     Byte huge* BP=PtrInc(P,Index);
  463.     return *BP;
  464.   }
  465.  
  466.