Skip to content

Commit 175a99b

Browse files
author
Alexander Dreyer
committed
chg: simplified indexed type
1 parent 69e0f6a commit 175a99b

File tree

1 file changed

+72
-107
lines changed

1 file changed

+72
-107
lines changed

Singular/countedref.cc

Lines changed: 72 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class CountedRefPtr {
5555
enum { is_weak = isWeak, never_null = NeverNull };
5656
//}
5757

58+
/// Default constructor @note: exisis only if @c NeverNull is false
59+
CountedRefPtr(): m_ptr(NULL) {}
60+
5861
/// Convert from pointer
5962
CountedRefPtr(ptr_type ptr): m_ptr(ptr) { reclaim(); }
6063

@@ -106,6 +109,10 @@ class CountedRefPtr {
106109
ptr_type m_ptr;
107110
};
108111

112+
/// Default constructor only implemented if @c NeverNull is false
113+
//template <class PtrType, bool isWeak,bool val,class CountType>
114+
//inline CountedRefPtr<PtrType, isWeak, val, CountType>::CountedRefPtr():
115+
// m_ptr(NULL) { }
109116

110117
/** @class RefCounter
111118
* This class implements implements a refernce counter which we can use
@@ -162,11 +169,6 @@ class CountedRefEnv {
162169
static int g_sh_id = 0;
163170
return g_sh_id;
164171
}
165-
static int& idx_id() {
166-
static int g_sh_id = 0;
167-
return g_sh_id;
168-
}
169-
170172
};
171173

172174
/// Overloading ring destruction
@@ -297,20 +299,34 @@ class CountedRefData:
297299
typedef CountedRefData self;
298300
typedef RefCounter base;
299301

302+
/// Subscripted object
303+
CountedRefData(idhdl handle, CountedRefPtr<self*> back):
304+
base(), m_data(init(handle)), m_ring(back->Ring()), m_back(back) { }
305+
300306
public:
307+
/// Construct shared memory empty Singular object
308+
explicit CountedRefData():
309+
base(), m_data(), m_ring(), m_back() { }
310+
301311
/// Construct reference for Singular object
302312
explicit CountedRefData(leftv data):
303-
base(), m_data(data), m_ring(parent(data)) { }
313+
base(), m_data(data), m_ring(parent(data)), m_back() { }
304314

305315
CountedRefData(leftv data, BOOLEAN global, int):
306-
base(), m_data(data, global,0), m_ring(parent(data)) { }
316+
base(), m_data(data, global,0), m_ring(parent(data)), m_back() { }
307317

308318
/// Construct deep copy
309319
CountedRefData(const self& rhs):
310-
base(), m_data(rhs.m_data), m_ring(rhs.m_ring) { }
320+
base(), m_data(rhs.m_data), m_ring(rhs.m_ring), m_back() { }
311321

312322
/// Destruct
313-
~CountedRefData() { }
323+
~CountedRefData() {
324+
if (m_back) CountedRefEnv::clearid((idhdl)m_data.access()->data, root());
325+
}
326+
327+
CountedRefPtr<self*> subscripted() {
328+
return new self(CountedRefEnv::idify(m_data.access(), root()), this);
329+
}
314330

315331
/// Replace data
316332
self& operator=(const self& rhs) {
@@ -338,7 +354,7 @@ class CountedRefData:
338354

339355
BOOLEAN rering() {
340356
if (m_ring ^ m_data->RingDependend()) m_ring = (m_ring? NULL: currRing);
341-
return FALSE;
357+
return (m_back && m_back->rering());
342358
}
343359

344360
/// Get the current context
@@ -361,14 +377,17 @@ class CountedRefData:
361377
}
362378

363379
BOOLEAN assign(leftv result, leftv arg) {
380+
if (m_data->rtyp!=IDHDL) {
381+
set(arg);
382+
return FALSE;
383+
}
364384
return get(result) || iiAssign(result, arg) || rering();
365385
}
366386

367-
/// @note Enables write-access via identifier
368-
idhdl idify() { return CountedRefEnv::idify(m_data.access(), root()); }
369-
370-
/// @note Only call, if @c idify had been called before!
371-
void clearid() { CountedRefEnv::clearid((idhdl)m_data.access()->data, root()); }
387+
/// @note If we have a callback, our identifier was temporary, so remove it
388+
void clear() {
389+
if (m_back) CountedRefEnv::clearid((idhdl)m_data.access()->data, root());
390+
}
372391

373392
BOOLEAN retrieve(leftv res) {
374393
if (res->data == m_data.access()->data) {
@@ -399,12 +418,24 @@ class CountedRefData:
399418
static ring parent(leftv rhs) {
400419
return (rhs->RingDependend()? currRing: NULL);
401420
}
421+
422+
static leftv init(idhdl handle) {
423+
assume(handle);
424+
leftv res = (leftv)omAlloc0(sizeof(*res));
425+
res->data =(void*) handle;
426+
res->rtyp = IDHDL;
427+
return res;
428+
}
429+
402430
protected:
403431
/// Singular object
404432
LeftvDeep m_data;
405433

406434
/// Store namespace for ring-dependent objects
407435
CountedRefPtr<ring, true> m_ring;
436+
437+
/// Reference to actual object for indexed structures
438+
CountedRefPtr<self*> m_back;
408439
};
409440

410441
/// blackbox support - initialization
@@ -431,8 +462,7 @@ class CountedRef {
431462
/// Check whether argument is already a reference type
432463
static BOOLEAN is_ref(leftv arg) {
433464
int typ = arg->Typ();
434-
return ((typ==CountedRefEnv::ref_id()) ||(typ==CountedRefEnv::sh_id()) ||
435-
(typ==CountedRefEnv::idx_id()) );
465+
return ((typ==CountedRefEnv::ref_id()) ||(typ==CountedRefEnv::sh_id()) );
436466
// return ((typ > MAX_TOK) &&
437467
// (getBlackboxStuff(typ)->blackbox_Init == countedref_Init));
438468
}
@@ -463,10 +493,16 @@ class CountedRef {
463493
BOOLEAN assign(leftv result, leftv arg) {
464494
return m_data->assign(result,arg);
465495
}
466-
496+
BOOLEAN rering() { return m_data->rering(); }
467497
/// Extract (shallow) copy of stored data
468498
LeftvShallow operator*() { return m_data->operator*(); }
469499

500+
/// Construct reference data object marked by given identifier number
501+
BOOLEAN outcast(leftv res, int typ) {
502+
res->rtyp = typ;
503+
return outcast(res);
504+
}
505+
470506
/// Construct reference data object from
471507
BOOLEAN outcast(leftv res) {
472508
if (res->rtyp == IDHDL)
@@ -643,39 +679,6 @@ BOOLEAN countedref_Op2(int op, leftv res, leftv head, leftv arg)
643679
iiExprArith2(res, head, op, arg);
644680
}
645681

646-
class CountedRefDataIndexed:
647-
public CountedRefData {
648-
typedef CountedRefData base;
649-
typedef CountedRefDataIndexed self;
650-
651-
public:
652-
CountedRefDataIndexed(idhdl handle, CountedRefPtr<base*> back):
653-
base(init(handle)), m_back(back) {
654-
m_ring = back->Ring();
655-
}
656-
657-
CountedRefDataIndexed(const self& rhs): base(rhs), m_back(rhs.m_back) { }
658-
659-
~CountedRefDataIndexed() { clearid();}
660-
661-
BOOLEAN assign(leftv result, leftv arg) {
662-
return base::assign(result, arg) || m_back->rering();
663-
}
664-
665-
private:
666-
static leftv init(idhdl handle) {
667-
assume(handle);
668-
leftv res = (leftv)omAlloc0(sizeof(*res));
669-
res->data =(void*) handle;
670-
res->rtyp = IDHDL;
671-
return res;
672-
}
673-
674-
CountedRefPtr<CountedRefData*> m_back;
675-
};
676-
inline void CountedRefPtr_kill(CountedRefDataIndexed* data) { delete data; }
677-
678-
679682

680683
/// blackbox support - ternary operations
681684
BOOLEAN countedref_Op3(int op, leftv res, leftv head, leftv arg1, leftv arg2)
@@ -687,50 +690,23 @@ BOOLEAN countedref_Op3(int op, leftv res, leftv head, leftv arg1, leftv arg2)
687690
}
688691

689692

690-
691693
/// blackbox support - destruction
692694
void countedref_destroy(blackbox *b, void* ptr)
693695
{
694696
if (ptr) CountedRef::cast(ptr).destruct();
695697
}
696698

697699

698-
/// blackbox support - assign element
699-
BOOLEAN countedref_AssignIndexed(leftv result, leftv arg)
700-
{
701-
// Case: replace assignment behind reference
702-
if (result->Data() != NULL) {
703-
CountedRefPtr<CountedRefDataIndexed*> indexed =(CountedRefDataIndexed*)(result->Data());
704-
return CountedRef::resolve(arg) || indexed->assign(result, arg);
705-
}
706-
707-
// Case: copy reference
708-
if (result->Typ() == arg->Typ())
709-
return CountedRef::cast(arg).outcast(result);
710-
711-
Werror("Cannot generate subscripted shared from plain type. Use shared[i] or shared.attr");
712-
return TRUE;
713-
}
714-
715-
716-
/// blackbox support - destruction
717-
void countedref_destroyIndexed(blackbox *b, void* ptr)
718-
{
719-
if (ptr) {
720-
CountedRefPtr<CountedRefDataIndexed*> data =
721-
static_cast<CountedRefDataIndexed*>(ptr);
722-
data.release();
723-
}
724-
}
725-
726700
class CountedRefShared:
727701
public CountedRef {
728702
typedef CountedRefShared self;
729703
typedef CountedRef base;
730704

731705
CountedRefShared(const base& rhs): base(rhs) { }
706+
CountedRefShared(data_type* rhs): base(rhs) { }
732707

733708
public:
709+
CountedRefShared(): base(new data_type) { }
734710
CountedRefShared(leftv arg): base(new data_type(arg, FALSE,0)) { }
735711

736712
/// Construct copy
@@ -750,34 +726,30 @@ class CountedRefShared:
750726
static self cast(leftv arg) { return base::cast(arg); }
751727
static self cast(void* arg) { return base::cast(arg); }
752728

753-
CountedRefPtr<CountedRefDataIndexed*> subscripted() {
754-
return new CountedRefDataIndexed(m_data->idify(), m_data);
729+
self subscripted() {
730+
return self(m_data->subscripted());//new CountedRefDataIndexed(m_data->idify(), m_data);
731+
}
732+
733+
BOOLEAN retrieve(leftv res, int typ) {
734+
return (m_data->retrieve(res) && outcast(res, typ));
755735
}
756736
};
757737

758738
void* countedref_InitShared(blackbox*)
759739
{
760-
LeftvDeep empty;
761-
return CountedRefShared(LeftvShallow(empty).operator->()).outcast();
740+
return CountedRefShared().outcast();
762741
}
763742

764743
/// blackbox support - binary operations
765744
BOOLEAN countedref_Op2Shared(int op, leftv res, leftv head, leftv arg)
766745
{
767746
if ((op == '[') || (op == '.')) {
768747
if (countedref_CheckInit(res, head)) return TRUE;
769-
CountedRefPtr<CountedRefDataIndexed*> indexed = CountedRefShared::cast(head).subscripted();
770-
if(indexed->operator*().get(head)) return TRUE;
771-
772-
if (CountedRef::resolve(arg) || iiExprArith2(res, head, op, arg)) return
773-
TRUE;
748+
CountedRefShared indexed = CountedRefShared::cast(head).subscripted();
774749

775-
if(indexed->retrieve(res)) {
776-
indexed.reclaim();
777-
res->rtyp = CountedRefEnv::idx_id();
778-
res->data = (void *)indexed;
779-
}
780-
return FALSE;
750+
int typ = head->Typ();
751+
return indexed.dereference(head) || CountedRef::resolve(arg) ||
752+
iiExprArith2(res, head, op, arg) || indexed.retrieve(res, typ);
781753
}
782754

783755
return countedref_Op2(op, res, head, arg);
@@ -809,7 +781,7 @@ BOOLEAN countedref_OpM(int op, leftv res, leftv args)
809781
return TRUE;
810782
}
811783
if (op == LIST_CMD){
812-
res->rtyp=op;
784+
res->rtyp = op;
813785
return jjLIST_PL(res, args);
814786
}
815787

@@ -822,7 +794,7 @@ BOOLEAN countedref_AssignShared(leftv result, leftv arg)
822794
/// Case: replace assignment behind reference
823795
if ((result->Data() != NULL) && (CountedRefShared::cast(result).Typ() !=0 )) {
824796
if (CountedRefShared::resolve(arg)) return TRUE;
825-
CountedRefShared::cast(result) = arg;
797+
return CountedRefShared::cast(result).assign(result, arg);
826798
return FALSE;
827799
}
828800

@@ -833,7 +805,9 @@ BOOLEAN countedref_AssignShared(leftv result, leftv arg)
833805
return CountedRefShared::cast(arg).outcast(result);
834806
}
835807
if(CountedRefShared::cast(result).Typ() == 0) {
836-
CountedRefShared::cast(result) = arg;
808+
// CountedRefShared::cast(result) = arg;
809+
return CountedRefShared::cast(result).assign(result, arg);
810+
837811
return FALSE;
838812
}
839813

@@ -873,15 +847,6 @@ void countedref_init()
873847
bbxshared->blackbox_Init = countedref_InitShared;
874848
bbxshared->data = omAlloc0(newstruct_desc_size());
875849
CountedRefEnv::sh_id()=setBlackboxStuff(bbxshared, "shared");
876-
877-
878-
blackbox *bbxindexed =
879-
(blackbox*)memcpy(omAlloc(sizeof(blackbox)), bbx, sizeof(blackbox));
880-
bbxindexed->blackbox_destroy = countedref_destroyIndexed;
881-
bbxindexed->blackbox_Assign = countedref_AssignIndexed;
882-
bbxindexed->data = omAlloc0(newstruct_desc_size());
883-
CountedRefEnv::idx_id()=setBlackboxStuff(bbxindexed, "shared_subexpr");
884-
885850
}
886851

887852
#ifdef HAVE_DYNAMIC_COUNTEDREF

0 commit comments

Comments
 (0)