SOSpin is hosted by Hepforge, IPPP Durham
SOSpin  1.0.0
dlist.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // SOSpin Library
3 // Copyright (C) 2015 SOSpin Project
4 //
5 // Authors:
6 //
7 // Nuno Cardoso (nuno.cardoso@tecnico.ulisboa.pt)
8 // David Emmanuel-Costa (david.costa@tecnico.ulisboa.pt)
9 // Nuno Gonçalves (nunogon@deec.uc.pt)
10 // Catarina Simoes (csimoes@ulg.ac.be)
11 //
12 // ----------------------------------------------------------------------------
13 // This file is part of SOSpin Library.
14 //
15 // SOSpin Library is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or any
18 // later version.
19 //
20 // SOSpin Library is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License
26 // along with SOSpin Library. If not, see <http://www.gnu.org/licenses/>.
27 // ----------------------------------------------------------------------------
28 
29 // dllist.h created on 27/02/2015
30 //
31 // This file contains the functions necessary to do things
32 // in the SOSpin Library.
33 //
34 // Revision 1.1 28/02/2015 23:19:29 david
35 // Revision 1.2 29/04/2015 17:27:00 nunogon
36 // Revision 1.3 26/05/2015 00:04:00 nunogon
37 // License updated
38 //
39 
45 #ifndef CLASS_DList_H
46 #define CLASS_DList_H
47 
48 #include <iostream>
49 #include <string>
50 #include <cstring>
51 #include <sstream>
52 #include <cstdlib>
53 #include <iostream>
54 #include <string>
55 #include <vector>
56 #include <list>
57 
58 
59 
60 using namespace std;
61 
62 
63 namespace sospin {
64 
69  enum Elem {
70  b = 0,
71  bt = 1,
72  delta = 2,
73  ct = 3
74  };
75 
101  struct elemType {
102  //Type - 3 bits
103  //Sign - 1 bit
104  //id.x - 10 bits
105  //id.y - 10 bits
106  //Free bits - 8 bits
107  //Total - 32 bits
108  unsigned int dataField;
109 
111  unsigned int getType() {
112  unsigned int type_out;
113  type_out = dataField >> 29;
114  return type_out;
115  }
116 
118  bool getSign() {
119  unsigned int bool_out;
120  bool_out = dataField & 0x10000000;
121  bool_out = bool_out >> 28;
122  return bool_out;
123  }
124 
126  unsigned int getIdx1() {
127  unsigned int idx1_out;
128  idx1_out = dataField & 0x0FFC0000;
129  idx1_out = idx1_out >> 18;
130  return idx1_out;
131  }
132 
134  unsigned int getIdx2() {
135  unsigned int idx2_out;
136  idx2_out = dataField & 0x0003FF00;
137  idx2_out = idx2_out >> 8;
138  return idx2_out;
139  }
140 
142  static elemType make_elem(int type, int data) {
143  if (type == 2) {
144  cout << "Cannot make a delta elemType with only one index" << endl;
145  exit(0);
146  } // returns error if data.type = 2 (delta)
147  elemType elem;
148  elem.setType(type);
149  elem.setIdx1(data);
150  elem.setIdx2(0);
151  return elem;
152  }
153 
155  static elemType make_elem(int type, int data1, int data2) {
156  elemType elem;
157  elem.setType(type);
158  elem.setIdx1(data1);
159  elem.setIdx2(data2);
160  return elem;
161  }
162 
164  static elemType make_delta(elemType data1, elemType data2) {
165  elemType elem;
166  elem.setType(2);
167  elem.setIdx1(data1.getIdx1());
168  elem.setIdx2(data2.getIdx1());
169  return elem;
170  }
171 
173  void setType(unsigned int typ) {
174 
175  //Clean 3-bits of type
176  dataField = dataField & 0x1FFFFFFF;
177 
178  //Set type
179  //b
180  if (typ == 0) dataField = dataField | 0x00000000;
181  //b^\dagger
182  if (typ == 1) dataField = dataField | 0x20000000;
183  //delta
184  if (typ == 2) dataField = dataField | 0x40000000;
185  //constant
186  if (typ == 3) dataField = dataField | 0x60000000;
187  // For all other types (not used)
188  if (typ > 3) dataField = dataField | 0x80000000;
189 
190  }
191 
193  void setSign(bool sign) {
194  //Clean bit of sign
195  dataField = dataField & 0xEFFFFFFF;
196 
197  //Set sign (only if sign is true)
198  if (sign == 1) dataField = dataField | 0x10000000;
199 
200  }
201 
203  void setIdx1(unsigned int idx) {
204  unsigned int in;
205 
206  //Clean bits of data1 field
207  dataField = dataField & 0xF003FFFF;
208 
209  //Format in to accommodate 10 bits from the input
210  in = idx & 0x000003FF;
211 
212  //Shift to match filled bits
213  in = in << 18;
214 
215  //Set data1 field
216  dataField = dataField | in;
217  }
218 
220  void setIdx2(unsigned int idx) {
221  unsigned int in;
222 
223  //Clean bits of data2 field
224  dataField = dataField & 0xFFFC00FF;
225 
226  //Format in to accommodate 10 bits from the input
227  in = idx & 0x000003FF;
228 
229  //Shift to match filled bits
230  in = in << 8;
231 
232  //Set data2 field
233  dataField = dataField | in;
234  }
235 
237  elemType& operator=(const elemType &elem) {
238  elemType aux=elem;
239  setType(aux.getType());
240  setIdx1(aux.getIdx1());
241  setIdx2(aux.getIdx2());
242  return *this;
243  }
244 
245  };
246 
251  struct noList {
258  };
259 
264  class DList {
272  int sign;
273 
274 
275  public:
276  //Constructors and destructor
277 
279  DList(void);
280 
282  DList(int tp, int i);
283 
285  DList(int tp, int i, int j);
286 
288  DList(const DList &L);
289 
291  ~DList(void);
292 
293 
294  //Changing, writing and updating
295 
297  void clear();
298 
300  void negate() {
301  sign *= -1;
302  }
303 
304 
306  void add(elemType);
307 
309  void add_begin(elemType);
310 
312  void add_end(elemType);
313 
314 
316  void set(elemType i) {
317  actual->data = i;
318  }
319 
321  void set_begin() {
322  actual = begin;
323  }
324 
326  void set_end() {
327  actual = end;
328  }
329 
331  void set_sign(int ii) {
332  sign = ii;
333  }
334 
335 
337  void join(DList& L);
338 
340  void loop_right() {
341  if (actual == 0 || actual->nxt == 0) actual = begin;
342  else actual = actual->nxt;
343  }
344 
346  void loop_left() {
347  actual = actual -> prv;
348  if (actual == 0) actual = end;
349  }
350 
352  DList rearrange();
353 
355  void remove(unsigned int type);
356 
358  void remove_actual();
359 
361  void shift_right() {
362  if (actual == 0 || actual->nxt == 0) actual = end;
363  else actual = actual->nxt;
364  }
365 
367  void shift_left() {
368  actual = actual -> prv;
369  if (actual == 0) actual = begin;
370  }
371 
373  void swap_next();
374 
375 
376  //Reading and accessing data
377 
379  elemType get() {
380  return actual->data;
381  }
382 
384  int getSign() {
385  return sign;
386  }
387 
389  vector<int> getIds();
390 
392  void getBandBdaggerIds(bool &BandBdagger, vector<string> &id0, vector<string> &id1, int &sign);
393 
395  void getDeltaIds(bool &AllDeltas, vector<string> &id0, vector<string> &id1, int &sign);
396 
398  void getBandBdaggerAndDeltasIds(vector<string> &id0, vector<string> &id1, vector<string> &id2, vector<string> &id3, int &sign);
399 
401  int numDeltas();
402 
404  int numBs();
405 
406 
408  bool search_last(unsigned int type1);
409 
411  bool search_first(unsigned int type1);
412 
414  bool search_first(unsigned int type0, unsigned int type1);
415 
417  bool search_elem(unsigned int type1);
418 
419 
420  //Querying and booleans
421 
423  bool check();
424 
426  bool checkDeltaIndex();
427 
429  bool check_num();
430 
432  bool check_same_num();
433 
435  bool isActualLast() {
436  return (actual == end) ? true : false;
437  }
438 
440  bool isEmpty() {
441  bool epty = true;
442  if (begin != 0) epty = false;
443  return epty;
444  }
445 
447  bool hasNoDeltas();
448 
450  bool hasOnlyDeltas();
451 
453  bool hasRepeatedIndex();
454 
455 
456  //Operators
457 
459  DList& operator=(const DList& L);
460 
462  const DList operator-()const;
463 
464 
465  //Friends
466 
468  friend DList* copy(DList *L);
469 
479  friend DList contract_deltas(DList &L, bool braketmode);
480 
491  friend DList ordering(DList &L, bool braketmode);
492 
493 
495  friend string printDeltas(DList &L);
496 
497 
499  friend DList& operator*(DList& L, elemType j);
500 
502  friend DList operator*(const DList& L, const DList &M);
503 
505  friend DList& operator-(DList& L, elemType j);
506 
508  friend DList& operator,(DList& L, elemType j);
509 
511  friend ostream& operator<<(ostream& out, DList &L);
512 
514  friend ostream& operator<<(ostream& out, DList *L);
515 
516  //same as operator* and operator,
518  friend DList& operator<<(DList &L, elemType j);
519 
521  friend DList& operator<<(DList &L, DList &M);
522 
524  friend bool operator==(DList &L, DList &M);
525 
526  };
527 
528 }
529 
530 #endif
constant element
Definition: dlist.h:73
unsigned int getType()
Returns the type of the element. Ex.: $b$ - type=0; $b^{}$ - type=1; $$ - type=2; constant - type=3...
Definition: dlist.h:111
OPMode operator-(const OPMode a, const OPMode b)
calculate the mode for the subtraction
Definition: braket.cpp:291
void set_end()
Changes actual pointer to point at the last element of DList (end pointer).
Definition: dlist.h:326
static elemType make_elem(int type, int data1, int data2)
Creates and return an element of type "type" and data fields "data1" and "data2". ...
Definition: dlist.h:155
OPMode operator*(const OPMode a, const OPMode b)
calculate the mode for the multiplication
Definition: braket.cpp:317
noList * begin
Pointer to the first node.
Definition: dlist.h:266
int sign
Store the sign of the monomial.
Definition: dlist.h:272
DList & operator,(DList &L, elemType j)
Adds element "j" to the end of DList. Returns pointer to DList.
Definition: dlist.cpp:1055
#define delta(a, b)
C++ Macro for DList delta.
Definition: son.h:100
void setSign(bool sign)
Sets sign field of the element (not used in current version).
Definition: dlist.h:193
unsigned int getIdx2()
Returns the second data field of the element (id.y).
Definition: dlist.h:134
noList * actual
Pointer to the actual node.
Definition: dlist.h:270
void set_sign(int ii)
Sets the sign of DList.
Definition: dlist.h:331
#define bt(a)
C++ Macro for DList b\dagger operator.
Definition: son.h:93
void negate()
Changes the sign of DList.
Definition: dlist.h:300
DList * copy(DList *L)
Creates and returns a pointer to a new copy of a DList.
Definition: dlist.cpp:806
void shift_right()
Shifts actual pointer to next node. If actual node is the end node, stops.
Definition: dlist.h:361
Elem
Enumerator for Node symbol.
Definition: dlist.h:69
bool isEmpty()
Returns true if DList has no nodes.
Definition: dlist.h:440
void setType(unsigned int typ)
Sets the type of the element.
Definition: dlist.h:173
void shift_left()
Shifts actual pointer to previous node. If actual node if first node (begin), stops.
Definition: dlist.h:367
void loop_right()
Shifts actual pointer to next node. If actual node is the end node, shift to beg node.
Definition: dlist.h:340
void setIdx1(unsigned int idx)
Sets first data field of the element (idx1).
Definition: dlist.h:203
DList ordering(DList &L, bool braketmode)
Order only the b's (to the left hand side) and b's (to the right hand side) terms. Applies the following identity: input DList L keeps delta term and function returns the swapped term.
Definition: dlist.cpp:911
string printDeltas(DList &L)
Creates and returns a string with the deltas and constants of a DList.
Definition: dlist.cpp:975
DList with nodes.
Definition: dlist.h:264
noList * prv
Pointer to the previous (prv) node.
Definition: dlist.h:255
DList contract_deltas(DList &L, bool braketmode)
Applies the following identity: input DList L keeps delta term and function returns the swapped term...
Definition: dlist.cpp:845
noList * nxt
Pointer to the next (nxt) node.
Definition: dlist.h:257
bool operator==(DList &L, DList &M)
Returns true if two DLists are equal.
Definition: dlist.cpp:1170
int getSign()
Returns the sign of DList.
Definition: dlist.h:384
ostream & operator<<(ostream &out, const OPMode &a)
Get the mode of the expression.
Definition: braket.cpp:266
unsigned int dataField
Definition: dlist.h:108
elemType data
Store symbol type.
Definition: dlist.h:253
elemType & operator=(const elemType &elem)
Makes a copy of an element.
Definition: dlist.h:237
#define b(a)
C++ Macro for DList b operator.
Definition: son.h:87
bool isActualLast()
Returns true if actual pointer is pointing to the last (end) node of DList.
Definition: dlist.h:435
void setIdx2(unsigned int idx)
Sets second data field of the element (idx2).
Definition: dlist.h:220
static elemType make_elem(int type, int data)
Creates and return an element of type "type" and one data field "data". Second data field is set to z...
Definition: dlist.h:142
static elemType make_delta(elemType data1, elemType data2)
Creates and return $$ element using first data field of elements "a" and "b".
Definition: dlist.h:164
bool getSign()
Returns the sign of the element (not used in current version).
Definition: dlist.h:118
void loop_left()
Shifts actual pointer to previous node. If actual node is the beg node, shift to end node...
Definition: dlist.h:346
struct for the node of DList
Definition: dlist.h:251
void set(elemType i)
Sets data (elemtype) of the node being pointed by actual pointer.
Definition: dlist.h:316
noList * end
Pointer to the last node.
Definition: dlist.h:268
void set_begin()
Changes actual pointer to point at the first element of DList (beg pointer).
Definition: dlist.h:321
unsigned int getIdx1()
Returns the first data field of the element (id.x).
Definition: dlist.h:126