Changeset 2cdc84 in git


Ignore:
Timestamp:
Aug 31, 2012, 11:33:33 AM (10 years ago)
Author:
Alexander Dreyer <alexander.dreyer@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', '2234726c50d679d6664181a5c72f75a6fd64a787')
Children:
9373f05657970e6e7e18aac67a18d1245fb6bb4e
Parents:
b8346741b9c72d735f018ce57120e6782a5d4d2d
git-author:
Alexander Dreyer <alexander.dreyer@itwm.fraunhofer.de>2012-08-31 11:33:33+02:00
git-committer:
Oleksandr Motsak <motsak@mathematik.uni-kl.de>2012-09-05 15:52:17+02:00
Message:
fix: better distingish between LeftvDeep and LeftvShallow
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/countedref.cc

    rb83467 r2cdc84  
    1212**/
    1313//*****************************************************************************
    14 
    1514
    1615
     
    143142
    144143public:
    145   static idhdl idify(leftv head, idhdl* root) {
     144  static leftv idify(leftv head, idhdl* root) {
     145    idhdl handle = newid(head, root);
     146    leftv res = (leftv)omAlloc0(sizeof(*res));
     147    res->data =(void*) handle;
     148    res->rtyp = IDHDL;
     149    return res;
     150  }
     151
     152  static idhdl newid(leftv head, idhdl* root) {
     153
    146154    static unsigned int counter = 0;
    147155    char* name = (char*) omAlloc0(512);
     
    175183inline void CountedRefPtr_kill(ring r) { rKill(r); }
    176184
    177 
    178 class LeftvShallow {
     185class LeftvHelper {
     186public:
     187  template <class Type>
     188  static Type* cpy(Type* result, Type* data)  {
     189    return (Type*)memcpy(result, data, sizeof(Type));
     190  }
     191  template <class Type>
     192  static Type* cpy(Type* data)  {
     193    return cpy((Type*)omAlloc0(sizeof(Type)), data);
     194  }
     195  template <class Type>
     196  static Type* recursivecpy(Type* data)  {
     197    if (data == NULL) return data;
     198    Type* result = cpy(data);
     199    result->next = recursivecpy(data->next);
     200    return result;
     201  }
     202  template <class Type>
     203  static Type* shallowcpy(Type* result, Type* data)  {
     204    cpy(result, data)->e = recursivecpy(data->e);
     205    return result;
     206  }
     207  template <class Type>
     208  static Type* shallowcpy(Type* data)  {
     209    return shallowcpy((Type*) omAlloc0(sizeof(Type)), data);
     210  }
     211  template <class Type>
     212  static void recursivekill(Type* current) {
     213    if(current == NULL) return;
     214    recursivekill(current->next);
     215    omFree(current);
     216  }
     217  static leftv allocate() { return (leftv)omAlloc0(sizeof(sleftv)); }
     218
     219};
     220
     221class LeftvShallow:
     222  public LeftvHelper {
    179223  typedef LeftvShallow self;
    180224 
    181225public:
    182226  LeftvShallow(): m_data(allocate()) { }
    183   LeftvShallow(leftv data):
    184     m_data(allocate()) { init(data); }
    185 
    186   LeftvShallow(const self& rhs):
    187     m_data(allocate()) {
    188     copy(m_data, rhs.m_data);
    189   }
     227  LeftvShallow(leftv data): m_data(shallowcpy(data)) { }
     228  LeftvShallow(const self& rhs):  m_data(shallowcpy(rhs.m_data)) { }
    190229
    191230  ~LeftvShallow() { 
    192     kill(m_data->e);
     231    recursivekill(m_data->e);
    193232    omFree(m_data);
    194233  }
    195234  self& operator=(leftv rhs) {
    196     kill(m_data->e);
    197     return init(rhs);
     235    recursivekill(m_data->e);
     236    shallowcpy(m_data, rhs);
     237    return *this;
    198238  }
    199239
    200240  self& operator=(const self& rhs) { return (*this) = rhs.m_data; }
    201 
    202 
    203 
    204241
    205242  /// Access to object
     
    208245
    209246protected:
    210   static leftv allocate() { return (leftv)omAlloc0(sizeof(sleftv)); }
    211 
    212   self& init(leftv data) {
    213     memcpy(m_data, data, sizeof(sleftv));
    214     data->e = NULL;
    215     m_data->next = NULL;
    216     return *this;
    217   }
    218 
    219   static void copy(leftv result, leftv data)  {
    220     memcpy(result, data, sizeof(sleftv));
    221     copy(result->e, data->e);
    222   }
    223 
    224  static void copy(Subexpr& current, Subexpr rhs)  {
    225     if (rhs == NULL) return;
    226     current = (Subexpr)memcpy(omAlloc0Bin(sSubexpr_bin), rhs, sizeof(*rhs));
    227     copy(current->next, rhs->next);
    228   }
    229 
    230   static void kill(Subexpr current) {
    231     if(current == NULL) return;
    232     kill(current->next);
    233     omFree(current);
    234   }
    235 
    236 protected:
    237247  leftv m_data;
    238248};
    239249
    240250
    241 class LeftvDeep:
    242   protected LeftvShallow {
     251class LeftvDeep: public LeftvHelper {
    243252  typedef LeftvDeep self;
    244   typedef LeftvShallow base;
    245 
     253
     254  /// @name Do not permit copying
     255  //@{
    246256  self& operator=(const self&);
    247 public:
    248   LeftvDeep(): base() {}
    249   LeftvDeep(leftv data): base(data) {  if(!isid()) m_data->data=data->CopyD(); }
    250 
    251   LeftvDeep(leftv data,int,int): base() {  m_data->Copy(data);  }
    252 
    253   LeftvDeep(const self& rhs): base(rhs) {
    254     if(m_data->rtyp != IDHDL)
    255       m_data->Copy(rhs.m_data);
    256   }
     257  LeftvDeep(const self&);
     258  //@}
     259
     260public:
     261  LeftvDeep(): m_data(allocate()) {}
     262  LeftvDeep(leftv data): m_data(cpy(data)) {
     263    data->e = NULL;   // occupy subexpression
     264    if(!isid()) m_data->data=data->CopyD();
     265  }
     266
     267  LeftvDeep(leftv data,int,int): m_data(allocate()) {  m_data->Copy(data);  }
    257268
    258269  ~LeftvDeep() { m_data->CleanUp(); }
    259   operator const base&() { return static_cast<const base&>(*this); }
     270  operator LeftvShallow() { return m_data;}
    260271
    261272  bool like(const self& rhs) const { return m_data->data == rhs.m_data->data; }
     
    289300    result->CleanUp();
    290301
    291     copy(result, m_data);
     302    shallowcpy(result, m_data);
    292303    result->next = next;
    293304    return FALSE;
    294305  }
     306
     307  BOOLEAN retrieve(leftv res) {
     308    if (res->data == m_data->data)  {
     309      if(m_data->e != res->e) recursivekill(m_data->e);
     310      cpy(m_data, res);
     311      res->Init();
     312      return TRUE;
     313    }
     314    return FALSE;
     315  }
     316
     317
    295318
    296319  BOOLEAN isid() const { return m_data->rtyp==IDHDL;}
    297320  BOOLEAN ringed() { return m_data->RingDependend(); }
    298321  BOOLEAN unassigned() const { return m_data->Typ()==0; }
     322
     323  leftv idify(idhdl* root) {
     324    leftv res = (isid()? m_data: CountedRefEnv::idify(m_data, root));
     325    ++(((idhdl)res->data)->ref);
     326    return res;
     327  }
     328
     329
     330  void clearid(idhdl* root) {
     331    assume(isid());
     332    if (--((idhdl)m_data->data)->ref <= 0)  // clear only if we own
     333      CountedRefEnv::clearid((idhdl)m_data->data, root);
     334  }
     335
     336private:
     337  leftv m_data;
    299338};
    300339
     
    311350  /// Subscripted object
    312351  CountedRefData(leftv wrapped, CountedRefPtr<self*> back):
    313     base(), m_data(wrapped), m_ring(back->m_ring), m_back(back) {
    314 
    315     assume(wrapped->rtyp == IDHDL);
    316     ++((idhdl)m_data.access()->data)->ref;
    317   }
    318 
    319   /// Disallow assignment
     352    base(), m_data(wrapped), m_ring(back->m_ring), m_back(back) {  }
     353
     354  /// Disallow copying to avoid inconsistence
     355  //@{
    320356  self& operator=(const self&);
     357  CountedRefData(const self&);
     358  //@}
    321359public:
    322360  /// Construct shared memory empty Singular object
     
    331369    base(), m_data(data, global,0), m_ring(parent(data)), m_back() { }
    332370
    333   /// Construct deep copy
    334   CountedRefData(const self& rhs):
    335     base(), m_data(rhs.m_data), m_ring(rhs.m_ring), m_back() { }
    336  
    337371  /// Destruct
    338   ~CountedRefData() {
    339     if (m_back && (--((idhdl)m_data.access()->data)->ref <= 0))  // clear only if we own
    340       CountedRefEnv::clearid((idhdl)m_data.access()->data, root());     
    341   }
    342 
     372  ~CountedRefData() {  if (m_back)  m_data.clearid(root()); }
     373
     374  /// Generate
    343375  CountedRefPtr<self*> subscripted() {
    344     if (m_back)
    345       return new self(operator*().operator->(), m_back);
    346 
    347     return new self(init(CountedRefEnv::idify(m_data.access(), root())), this);
     376    return new self(m_data.idify(root()),
     377                    m_back? m_back: CountedRefPtr<self*>(this));
    348378  }
    349379
     
    361391  LeftvShallow operator*() const { return (broken()? LeftvShallow(): (const LeftvShallow&)m_data); }
    362392
    363 
     393  /// Determine active ring when ring dependency changes
    364394  BOOLEAN rering() {
    365395    if (m_ring ^ m_data.ringed()) m_ring = (m_ring? NULL: currRing);
     
    386416  }
    387417
     418  /// Reassign actual object
    388419  BOOLEAN assign(leftv result, leftv arg) {
    389420    if (!m_data.isid()) {
     
    393424    return get(result) || iiAssign(result, arg) || rering();
    394425  }
    395 
    396   BOOLEAN retrieve(leftv res) {
    397     if (res->data == m_data.access()->data)  {
    398       memcpy(m_data.access(), res, sizeof(sleftv));
    399       res->Init();
    400       return TRUE;
    401     }
    402     return FALSE;
    403   }
    404 
     426  /// Recover additional information (e.g. subexpression) from likewise object
     427  BOOLEAN retrieve(leftv res) { return m_data.retrieve(res); }
     428
     429  /// Check whether data is all-zero
    405430  BOOLEAN unassigned() const { return m_data.unassigned(); }
     431
    406432private:
    407 
    408433  /// Raise error message and return @c TRUE
    409434  BOOLEAN complain(const char* text) const  {
     
    412437  }
    413438
    414 
    415 
    416439  /// Store ring for ring-dependent objects
    417440  static ring parent(leftv rhs) {
    418441    return (rhs->RingDependend()? currRing: NULL);
    419   }
    420 
    421  static leftv init(idhdl handle) {
    422     assume(handle);
    423     leftv res = (leftv)omAlloc0(sizeof(*res));
    424     res->data =(void*) handle;
    425     res->rtyp = IDHDL;
    426     return res;
    427442  }
    428443
     
    574589    return (arg->next != NULL) && resolve(arg->next);
    575590  }
    576   //  int Typ() const {return m_data->Typ(); }
     591
    577592protected:
    578593  /// Construct integer value
Note: See TracChangeset for help on using the changeset viewer.