This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Patch: stl_vector.h


On Fri, 2004-10-15 at 21:49, Paolo Carlini wrote:
> Dhruv Matani wrote:
> 
> >Sorry, I attached the reverse patch last time around! Stupid me ;-)
> >  
> >
> And where is a short testcase 

vect_test.cpp

> and an explanation and a tentative 
> ChangeLog (which, would necessarily include a minimum of explanation)?

Done.

> 
> Paolo.
-- 
        -Dhruv Matani.
http://www.geocities.com/dhruvbird/

The price of freedom is responsibility, but it's a bargain, because
freedom is priceless. ~ Hugh Downs
#include <iostream>
#include <vector>
#include <ext/mt_allocator.h>
using namespace std;

// Do some cheating!
template<typename _Tp>
class alloc_base 
{
public:
  typedef size_t                    size_type;
  typedef ptrdiff_t                 difference_type;
  typedef _Tp*                      pointer;
  typedef const _Tp*                const_pointer;
  typedef _Tp&                      reference;
  typedef const _Tp&                const_reference;
  typedef _Tp                       value_type;

  pointer
  address(reference __x) const
  { return &__x; }

  const_pointer
  address(const_reference __x) const
  { return &__x; }

  size_type
  max_size() const throw() 
  { return size_t(-1) / sizeof(_Tp); }

  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 402. wrong new expression in [some_] allocator::construct
  void 
  construct(pointer __p, const _Tp& __val) 
  { ::new(__p) _Tp(__val); }

  void 
  destroy(pointer __p) { __p->~_Tp(); }
};



template<typename T>
  class The_Allocator : public alloc_base<T>
  {
  public:
    typedef size_t                    size_type;
    typedef ptrdiff_t                 difference_type;
    typedef T*                      pointer;
    typedef const T*                const_pointer;
    typedef T&                      reference;
    typedef const T&                const_reference;
    typedef T                       value_type;
    
    pointer allocate(size_t n, const void* = 0)
    {
      return (pointer)malloc(n*sizeof(T));
    }
    void deallocate(pointer p, size_t) { free(p); }
  };

#define ALLOCATOR_TEMPLATE The_Allocator
// std::allocator


typedef std::vector<double, ALLOCATOR_TEMPLATE<double> > dvector_t;

/**
 * @brief The matrix data type.
 */
typedef std::vector<dvector_t, 
		    ALLOCATOR_TEMPLATE<dvector_t> > matrix_t;


int main()
{
  int num_eqns = 23;
  matrix_t mat = matrix_t(num_eqns, num_eqns);
}

Attachment: ChangeLog
Description: Text document

Attachment: patch_stl_vector_15102004.diff
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]