New (faster) representation for tags added; faster construction of parentheses
[SXSI/XMLTree.git] / libcds / src / coders / huff.cpp
index deb0e70..52b95f9 100644 (file)
@@ -1,4 +1,23 @@
+/* huff.cpp
+   Copyright (C) 2008, Gonzalo Navarro, all rights reserved.
 
+   Canonical Huffman
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+*/
 // implements canonical Huffman 
 
 #include <huff.h>
@@ -180,16 +199,17 @@ ulong decodeHuff (THuff H, uint *symb, uint *stream, ulong ptr)
 */
 void saveHuff (THuff H, FILE *f)
 
-   { uint *symb = (uint*)malloc((H.lim+1)*sizeof(uint));
+   { uint *symb = new uint[H.lim+1];
      uint i;
+                for(i=0;i<(H.lim+1);i++) symb[i] = 0;
      for (i=0;i<=H.max;i++) 
-        if (H.s.spos[i] != (uint)~0) symb[H.s.spos[i]] = i;
+                        if (H.s.spos[i] != (uint)~0) symb[H.s.spos[i]] = i;
      uint l=fwrite (&H.max,sizeof(uint),1,f); 
      l += fwrite (&H.lim,sizeof(uint),1,f); 
      l += fwrite (&H.depth,sizeof(uint),1,f); 
      l += fwrite (symb,sizeof(uint),H.lim+1,f); 
      l += fwrite (H.num,sizeof(uint),H.depth+1,f); 
-     free (symb);
+     delete [] (symb);
    }
 
 uint sizeHuff (THuff H)
@@ -235,5 +255,3 @@ THuff loadHuff (FILE *f, int enc)
          }
      return H;
    }
-
-