From 9bc982d2403e1d278367914c44f4c1e82a606841 Mon Sep 17 00:00:00 2001
From: Andrea Canciani <ranma42@gmail.com>
Date: Tue, 2 Nov 2010 12:19:36 +0100
Subject: [PATCH 1/3] Remove GooVector

GooVector is just a reimplementation of (part of) std::vector.
---
 goo/GooVector.h            |  159 --------------------------------------------
 goo/Makefile.am            |    1 -
 poppler/CachedFile.cc      |   17 ++---
 poppler/CachedFile.h       |   14 ++--
 poppler/CurlCachedFile.cc  |    4 +-
 poppler/CurlCachedFile.h   |    2 +-
 poppler/Form.h             |    1 -
 poppler/Gfx.h              |    4 +-
 poppler/StdinCachedFile.cc |    2 +-
 poppler/StdinCachedFile.h  |    2 +-
 poppler/Stream.h           |    1 -
 poppler/XRef.cc            |    6 +-
 poppler/XRef.h             |    6 +-
 utils/HtmlFonts.cc         |    6 +-
 utils/HtmlFonts.h          |    8 +-
 utils/HtmlLinks.cc         |    8 +-
 utils/HtmlLinks.h          |    4 +-
 17 files changed, 41 insertions(+), 204 deletions(-)
 delete mode 100644 goo/GooVector.h

diff --git a/goo/GooVector.h b/goo/GooVector.h
deleted file mode 100644
index e35fd8a..0000000
--- a/goo/GooVector.h
+++ /dev/null
@@ -1,159 +0,0 @@
-//========================================================================
-//
-// GooVector.h
-//
-// This file is licensed under the GPLv2 or later
-//
-// Copyright 2010 David Benjamin <davidben@mit.edu>
-// Copyright 2010 Albert Astals Cid <aacid@kde.org>
-//
-//========================================================================
-
-
-
-#ifndef GOO_GOOVECTOR_H
-#define GOO_GOOVECTOR_H
-
-#ifdef USE_GCC_PRAGMAS
-#pragma interface
-#endif
-
-#include <new> // vector implementations need placement-new
-
-#include <assert.h>
-#include <stdlib.h>
-
-/* Mostly STL-compatible vector class. Should correctly call constructors and
- * destructors, but does not carefully handle alignment requirements. */
-
-template<class T> class GooVector {
-public:
-  /* various STL-compatible typedefs */
-  typedef T value_type;
-  typedef T* pointer;
-  typedef T& reference;
-  typedef const T& const_reference;
-  typedef size_t size_type;
-  typedef int difference_type;
-  typedef T* iterator;
-  typedef const T* const_iterator;
-  // TODO: reverse_iterator, if we feel like it
-  
-  GooVector() : m_data(NULL), m_capacity(0), m_size(0) {}
-  explicit GooVector(size_type n) : m_data(NULL), m_capacity(0), m_size(0) {
-    resize(n);
-  }
-  explicit GooVector(size_type n, const T& t) : m_data(NULL), m_capacity(0), m_size(0) {
-    resize(n, t);
-  }
-  explicit GooVector(const GooVector& gv) : m_data(NULL), m_capacity(0), m_size(0) {
-    reserve(gv.size());
-    for (size_type i = 0; i < m_size; i++) {
-      push_back(gv[i]);
-    }
-  }
-
-  ~GooVector() {
-    clear();
-  }
-
-  iterator begin() { return m_data; }
-  const_iterator begin() const { return m_data; }
-  iterator end() { return m_data + m_size; }
-  const_iterator end() const { return m_data + m_size; }
-
-  size_type size() const { return m_size; }
-  size_type capacity() const { return m_capacity; }
-
-  bool empty() const { return m_size == 0; }
-
-  reference operator[] (size_type n) { return m_data[n]; }
-  const_reference operator[] (size_type n) const { return m_data[n]; }
-
-  reference at(size_type n) {
-    assert(n < m_size);
-    return m_data[n];
-  }
-  const_reference at(size_type n) const {
-    assert(n < m_size);
-    return m_data[n];
-  }
-
-  reference front() { assert(!empty()); return m_data[0]; }
-  const_reference front() const { assert(!empty()); return m_data[0]; }
-
-  reference back() { assert(!empty()); return m_data[m_size-1]; }
-  const_reference back() const { assert(!empty()); return m_data[m_size-1]; }
-
-  void push_back(const T& v) {
-    reserve(m_size + 1);
-    place_new(m_data + m_size, v);
-    m_size++;
-  }
-  void pop_back() {
-    assert(!empty());
-    m_size--;
-    destruct(m_data + m_size);
-  }
-
-  void clear() {
-    for (size_t i = 0; i < m_size; i++) {
-      destruct(m_data + i);
-    }
-    m_size = 0;
-    free(m_data);
-    m_data = NULL;
-    m_capacity = 0;
-  }
-
-  void reserve(size_type cap) {
-    if (m_capacity >= cap) return;
-    // make sure we always at least double
-    if (m_capacity*2 > cap)
-      cap = m_capacity*2;
-    resize_internal(cap);
-  }
-
-  void resize(size_type n) { resize(n, T()); }
-  void resize(size_type n, const T& t) {
-    reserve(n);
-    while (m_size < n)
-      push_back(t);
-    while (m_size > n)
-      pop_back();
-  }
-
-private:
-  T *m_data;
-  size_type m_capacity;
-  size_type m_size;
-
-  inline void destruct(T *obj) {
-    obj->~T();
-  }
-  inline void place_new(T *loc, const T& v) {
-    new (loc) T(v);
-  }
-
-  inline void resize_internal(size_type new_cap) {
-    assert(new_cap >= m_capacity);
-    // To be correct with ctors and dtors, we do not use realloc and friends.
-    // A more efficient implementation would specialize for POD types and just
-    // realloc() or something. Meh, if we care, we ought to use just STL's
-    T *new_data = (T*) malloc(sizeof(T) * new_cap);
-    assert(new_data);
-    // Move over old data
-    if (m_data) {
-      for (size_type i = 0; i < m_size; i++) {
-	place_new(new_data + i, m_data[i]);
-	destruct(m_data + i);
-      }
-      free(m_data);
-    }
-    // And set the new values
-    m_data = new_data;
-    m_capacity = new_cap;
-  }
-};
-
-#endif
diff --git a/goo/Makefile.am b/goo/Makefile.am
index e15c7ac..de894af 100644
--- a/goo/Makefile.am
+++ b/goo/Makefile.am
@@ -9,7 +9,6 @@ poppler_goo_include_HEADERS =			\
 	GooTimer.h				\
 	GooMutex.h				\
 	GooString.h				\
-	GooVector.h				\
 	gtypes.h				\
 	gmem.h					\
 	gfile.h					\
diff --git a/poppler/CachedFile.cc b/poppler/CachedFile.cc
index cc86c89..b5d1cf7 100644
--- a/poppler/CachedFile.cc
+++ b/poppler/CachedFile.cc
@@ -23,7 +23,7 @@ CachedFile::CachedFile(CachedFileLoader *cachedFileLoaderA, GooString *uriA)
   loader = cachedFileLoaderA;
 
   streamPos = 0;
-  chunks = new GooVector<Chunk>();
+  chunks = new std::vector<Chunk>();
   length = 0;
 
   length = loader->init(uri, this);
@@ -70,15 +70,15 @@ int CachedFile::seek(long int offset, int origin)
   return 0;
 }
 
-int CachedFile::cache(const GooVector<ByteRange> &origRanges)
+int CachedFile::cache(const std::vector<ByteRange> &origRanges)
 {
-  GooVector<int> loadChunks;
+  std::vector<int> loadChunks;
   int numChunks = length/CachedFileChunkSize + 1;
-  GooVector<bool> chunkNeeded(numChunks);
+  std::vector<bool> chunkNeeded(numChunks);
   int startChunk, endChunk;
-  GooVector<ByteRange> chunk_ranges, all;
+  std::vector<ByteRange> chunk_ranges, all;
   ByteRange range;
-  const GooVector<ByteRange> *ranges = &origRanges;
+  const std::vector<ByteRange> *ranges = &origRanges;
 
   if (ranges->empty()) {
     range.offset = 0;
@@ -87,7 +87,6 @@ int CachedFile::cache(const GooVector<ByteRange> &origRanges)
     ranges = &all;
   }
 
-  memset(&chunkNeeded[0], 0, sizeof(bool) * numChunks);
   for (size_t i = 0; i < ranges->size(); i++) {
 
     if ((*ranges)[i].length == 0) continue;
@@ -166,7 +165,7 @@ size_t CachedFile::read(void *ptr, size_t unitsize, size_t count)
 
 int CachedFile::cache(size_t offset, size_t length)
 {
-  GooVector<ByteRange> r;
+  std::vector<ByteRange> r;
   ByteRange range;
   range.offset = offset;
   range.length = length;
@@ -178,7 +177,7 @@ int CachedFile::cache(size_t offset, size_t length)
 // CachedFileWriter
 //------------------------------------------------------------------------
 
-CachedFileWriter::CachedFileWriter(CachedFile *cachedFileA, GooVector<int> *chunksA)
+CachedFileWriter::CachedFileWriter(CachedFile *cachedFileA, std::vector<int> *chunksA)
 {
    cachedFile = cachedFileA;
    chunks = chunksA;
diff --git a/poppler/CachedFile.h b/poppler/CachedFile.h
index 897ff4a..bd024cf 100644
--- a/poppler/CachedFile.h
+++ b/poppler/CachedFile.h
@@ -20,7 +20,7 @@
 #include "goo/gtypes.h"
 #include "Object.h"
 #include "Stream.h"
-#include "goo/GooVector.h"
+#include <vector>
 
 //------------------------------------------------------------------------
 
@@ -51,7 +51,7 @@ public:
   int seek(long int offset, int origin);
   size_t read(void * ptr, size_t unitsize, size_t count);
   size_t write(const char *ptr, size_t size, size_t fromByte);
-  int cache(const GooVector<ByteRange> &ranges);
+  int cache(const std::vector<ByteRange> &ranges);
 
   // Reference counting.
   void incRefCnt();
@@ -79,7 +79,7 @@ private:
   size_t length;
   size_t streamPos;
 
-  GooVector<Chunk> *chunks;
+  std::vector<Chunk> *chunks;
 
   int refCnt;  // reference count
 
@@ -99,7 +99,7 @@ public:
 
   // Construct a CachedFile Writer.
   // The caller is responsible for deleting the cachedFile and chunksA.
-  CachedFileWriter(CachedFile *cachedFile, GooVector<int> *chunksA);
+  CachedFileWriter(CachedFile *cachedFile, std::vector<int> *chunksA);
 
   ~CachedFileWriter();
 
@@ -109,8 +109,8 @@ public:
 private:
 
   CachedFile *cachedFile;
-  GooVector<int> *chunks;
-  GooVector<int>::iterator it;
+  std::vector<int> *chunks;
+  std::vector<int>::iterator it;
   size_t offset;
 
 };
@@ -136,7 +136,7 @@ public:
   // Loads speficified byte ranges and passes it to the writer to store them.
   // Returns 0 on success, Anything but 0 on failure.
   // The caller is responsible for deleting the writer.
-  virtual int load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
+  virtual int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer) = 0;
 
 };
 
diff --git a/poppler/CurlCachedFile.cc b/poppler/CurlCachedFile.cc
index 35f5104..aa9a823 100644
--- a/poppler/CurlCachedFile.cc
+++ b/poppler/CurlCachedFile.cc
@@ -15,7 +15,7 @@
 #include "CurlCachedFile.h"
 
 #include "goo/GooString.h"
-#include "goo/GooVector.h"
+#include <vector>
 
 //------------------------------------------------------------------------
 
@@ -68,7 +68,7 @@ size_t load_cb(const char *ptr, size_t size, size_t nmemb, void *data)
   return (writer->write) (ptr, size*nmemb);
 }
 
-int CurlCachedFileLoader::load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer)
+int CurlCachedFileLoader::load(const vector<ByteRange> &ranges, CachedFileWriter *writer)
 {
   CURLcode r = CURLE_OK;
   size_t fromByte, toByte;
diff --git a/poppler/CurlCachedFile.h b/poppler/CurlCachedFile.h
index b5f2e7d..49882ab 100644
--- a/poppler/CurlCachedFile.h
+++ b/poppler/CurlCachedFile.h
@@ -26,7 +26,7 @@ public:
   CurlCachedFileLoader();
   ~CurlCachedFileLoader();
   size_t init(GooString *url, CachedFile* cachedFile);
-  int load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer);
+  int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer);
 
 private:
 
diff --git a/poppler/Form.h b/poppler/Form.h
index 751a915..1d25ce8 100644
--- a/poppler/Form.h
+++ b/poppler/Form.h
@@ -19,7 +19,6 @@
 #endif
 
 #include "Object.h"
-#include "goo/GooVector.h"
 
 class GooString;
 class Array;
diff --git a/poppler/Gfx.h b/poppler/Gfx.h
index adabe7d..4d6abc9 100644
--- a/poppler/Gfx.h
+++ b/poppler/Gfx.h
@@ -36,10 +36,10 @@
 
 #include "goo/gtypes.h"
 #include "goo/GooList.h"
-#include "goo/GooVector.h"
 #include "GfxState.h"
 #include "Object.h"
 #include "PopplerCache.h"
+#include <vector>
 
 class GooString;
 class XRef;
@@ -202,7 +202,7 @@ private:
 
   GfxState *state;		// current graphics state
   int stackHeight;		// the height of the current graphics stack
-  GooVector<int> stateGuards;   // a stack of state limits; to guard against unmatched pops
+  std::vector<int> stateGuards;   // a stack of state limits; to guard against unmatched pops
   GBool fontChanged;		// set if font or text matrix has changed
   GfxClipType clip;		// do a clip?
   int ignoreUndef;		// current BX/EX nesting level
diff --git a/poppler/StdinCachedFile.cc b/poppler/StdinCachedFile.cc
index 3a91e62..db96637 100644
--- a/poppler/StdinCachedFile.cc
+++ b/poppler/StdinCachedFile.cc
@@ -40,7 +40,7 @@ size_t StdinCacheLoader::init(GooString *dummy, CachedFile *cachedFile)
   return size;
 }
 
-int StdinCacheLoader::load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer)
+int StdinCacheLoader::load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer)
 {
   return 0;
 }
diff --git a/poppler/StdinCachedFile.h b/poppler/StdinCachedFile.h
index 11b064b..5be6fa8 100644
--- a/poppler/StdinCachedFile.h
+++ b/poppler/StdinCachedFile.h
@@ -19,7 +19,7 @@ class StdinCacheLoader : public CachedFileLoader {
 public:
 
   size_t init(GooString *dummy, CachedFile* cachedFile);
-  int load(const GooVector<ByteRange> &ranges, CachedFileWriter *writer);
+  int load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer);
 
 };
 
diff --git a/poppler/Stream.h b/poppler/Stream.h
index e093dad..7bb10d7 100644
--- a/poppler/Stream.h
+++ b/poppler/Stream.h
@@ -34,7 +34,6 @@
 
 #include <stdio.h>
 #include "goo/gtypes.h"
-#include "goo/GooVector.h"
 #include "Object.h"
 
 class BaseStream;
diff --git a/poppler/XRef.cc b/poppler/XRef.cc
index 0cd4be0..694336f 100644
--- a/poppler/XRef.cc
+++ b/poppler/XRef.cc
@@ -306,7 +306,7 @@ XRef::XRef(BaseStream *strA, GBool *wasReconstructed, GBool reconstruct) {
 
     // read the xref table
     } else {
-      GooVector<Guint> followedXRefStm;
+      std::vector<Guint> followedXRefStm;
       while (readXRef(&pos, &followedXRefStm)) ;
 
       // if there was a problem with the xref table,
@@ -386,7 +386,7 @@ Guint XRef::getStartXref() {
 
 // Read one xref table section.  Also reads the associated trailer
 // dictionary, and returns the prev pointer (if any).
-GBool XRef::readXRef(Guint *pos, GooVector<Guint> *followedXRefStm) {
+GBool XRef::readXRef(Guint *pos, std::vector<Guint> *followedXRefStm) {
   Parser *parser;
   Object obj;
   GBool more;
@@ -435,7 +435,7 @@ GBool XRef::readXRef(Guint *pos, GooVector<Guint> *followedXRefStm) {
   return gFalse;
 }
 
-GBool XRef::readXRefTable(Parser *parser, Guint *pos, GooVector<Guint> *followedXRefStm) {
+GBool XRef::readXRefTable(Parser *parser, Guint *pos, std::vector<Guint> *followedXRefStm) {
   XRefEntry entry;
   GBool more;
   Object obj, obj2;
diff --git a/poppler/XRef.h b/poppler/XRef.h
index 1f4ec6a..ea038d2 100644
--- a/poppler/XRef.h
+++ b/poppler/XRef.h
@@ -32,8 +32,8 @@
 #endif
 
 #include "goo/gtypes.h"
-#include "goo/GooVector.h"
 #include "Object.h"
+#include <vector>
 
 class Dict;
 class Stream;
@@ -158,8 +158,8 @@ private:
   GBool ownerPasswordOk;	// true if owner password is correct
 
   Guint getStartXref();
-  GBool readXRef(Guint *pos, GooVector<Guint> *followedXRefStm);
-  GBool readXRefTable(Parser *parser, Guint *pos, GooVector<Guint> *followedXRefStm);
+  GBool readXRef(Guint *pos, std::vector<Guint> *followedXRefStm);
+  GBool readXRefTable(Parser *parser, Guint *pos, std::vector<Guint> *followedXRefStm);
   GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n);
   GBool readXRefStream(Stream *xrefStr, Guint *pos);
   GBool constructXRef(GBool *wasReconstructed);
diff --git a/utils/HtmlFonts.cc b/utils/HtmlFonts.cc
index 4b592d5..595a9f8 100644
--- a/utils/HtmlFonts.cc
+++ b/utils/HtmlFonts.cc
@@ -267,7 +267,7 @@ GooString* HtmlFont::simple(HtmlFont* font, Unicode* content, int uLen){
 }
 
 HtmlFontAccu::HtmlFontAccu(){
-  accu=new GooVector<HtmlFont>();
+  accu=new std::vector<HtmlFont>();
 }
 
 HtmlFontAccu::~HtmlFontAccu(){
@@ -275,7 +275,7 @@ HtmlFontAccu::~HtmlFontAccu(){
 }
 
 int HtmlFontAccu::AddFont(const HtmlFont& font){
- GooVector<HtmlFont>::iterator i; 
+ std::vector<HtmlFont>::iterator i; 
  for (i=accu->begin();i!=accu->end();i++)
  {
 	if (font.isEqual(*i)) 
@@ -317,7 +317,7 @@ GooString* HtmlFontAccu::CSStyle(int i, int j){
    GooString *iStr=GooString::fromInt(i);
    GooString *jStr=GooString::fromInt(j);
 
-   GooVector<HtmlFont>::iterator g=accu->begin();
+   std::vector<HtmlFont>::iterator g=accu->begin();
    g+=i;
    HtmlFont font=*g;
    GooString *Size=GooString::fromInt(font.getSize());
diff --git a/utils/HtmlFonts.h b/utils/HtmlFonts.h
index 54deaf8..c850e99 100644
--- a/utils/HtmlFonts.h
+++ b/utils/HtmlFonts.h
@@ -26,7 +26,7 @@
 
 #ifndef _HTML_FONTS_H
 #define _HTML_FONTS_H
-#include "goo/GooVector.h"
+#include <vector>
 #include "goo/GooString.h"
 #include "GfxState.h"
 #include "CharTypes.h"
@@ -92,16 +92,16 @@ public:
 
 class HtmlFontAccu{
 private:
-  GooVector<HtmlFont> *accu;
+  std::vector<HtmlFont> *accu;
   
 public:
   HtmlFontAccu();
   ~HtmlFontAccu();
   int AddFont(const HtmlFont& font);
   HtmlFont* Get(int i){
-    GooVector<HtmlFont>::iterator g=accu->begin();
+    std::vector<HtmlFont>::iterator g=accu->begin();
     g+=i;  
-    return g;
+    return &(*g);
   } 
   GooString* getCSStyle (int i,GooString* content, int j = 0);
   GooString* CSStyle(int i, int j = 0);
diff --git a/utils/HtmlLinks.cc b/utils/HtmlLinks.cc
index c0ca89a..d192bd8 100644
--- a/utils/HtmlLinks.cc
+++ b/utils/HtmlLinks.cc
@@ -118,7 +118,7 @@ GooString* HtmlLink::getLinkStart() {
    
 
 HtmlLinks::HtmlLinks(){
-  accu=new GooVector<HtmlLink>();
+  accu=new std::vector<HtmlLink>();
 }
 
 HtmlLinks::~HtmlLinks(){
@@ -128,7 +128,7 @@ HtmlLinks::~HtmlLinks(){
 
 GBool HtmlLinks::inLink(double xmin,double ymin,double xmax,double ymax,int& p)const {
   
-  for(GooVector<HtmlLink>::iterator i=accu->begin();i!=accu->end();i++){
+  for(std::vector<HtmlLink>::iterator i=accu->begin();i!=accu->end();i++){
     if (i->inLink(xmin,ymin,xmax,ymax)) {
         p=(i - accu->begin());
         return 1;
@@ -138,8 +138,8 @@ GBool HtmlLinks::inLink(double xmin,double ymin,double xmax,double ymax,int& p)c
 }
 
 HtmlLink* HtmlLinks::getLink(int i) const{
-  GooVector<HtmlLink>::iterator g=accu->begin();
+  std::vector<HtmlLink>::iterator g=accu->begin();
   g+=i; 
-  return g;
+  return &(*g);
 }
 
diff --git a/utils/HtmlLinks.h b/utils/HtmlLinks.h
index 1212844..4a48dfa 100644
--- a/utils/HtmlLinks.h
+++ b/utils/HtmlLinks.h
@@ -29,7 +29,7 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include "goo/GooVector.h"
+#include <vector>
 #include "goo/GooString.h"
 
 class HtmlLink{
@@ -59,7 +59,7 @@ public:
 
 class HtmlLinks{
 private:
- GooVector<HtmlLink> *accu;
+ std::vector<HtmlLink> *accu;
 public:
  HtmlLinks();
  ~HtmlLinks();
-- 
1.7.0.4

