next up previous contents index
Next: The LEDA Tools for Up: The golden LEDA rules Previous: The LEDA rules in   Contents   Index


Code examples for the LEDA rules

  1. string s("Jumping Jack Flash");
    string t(s);  // definition with initialization by copying
    string u = s; // definition with initialization by copying
    
    stack<int> S;
    // ... fill S with some elements
    stack<int> T(S); // definition with initialization by copying
    

    1. list_item it1, it2;
      // ...
      it2 = it1; // it2 now references the same container as it1
      

    2. array<int> A, B;
      // ...fill A with some elements...
      B = A;
      

      Now B contains the same number of integers as A, in the same order, with the same values.

      However, A and B do not contain the same objects:

      int* p = A[0];
      int* q = B[0];
      p == q; // false
      

      A and B are different objects:

      A == B; // false
      

    3. list<int> L, M;
      list_item it1, it2;
      L.push(42);
      L.push(666);
      M = L;
      

      L and M now both contain the numbers 666 and 42. These numbers are not the same objects:

      it1 = L.first();
      it2 = M.first();
      it1 == it2; // false
      

      L and M are different objects as well:

      L == M; // false
      

    In the following assignment the rules c, b, and a are applied recursivley (in this order):

    list< array<int> > L, M;
    // ...fill L with some array<int>s
    // each of them filled with some elements...
    M = L;
    

    1. list_item it1, it2;
      // ...
      it2 = it1; // it2 now references the same container as it1
      it1 == it2; // true
      

    2. point p(2.0, 3.0);
      point q(2.0, 3.0);
      p == q; // true (as defined for class point)
      identical(p, q); // false
      point r;
      r = p;
      identical(p, r); // true
      

    3. list<int> L, M;
      // ...fill L with some elements...
      M = L; 
      L == M; // false
      

  2. list_item it = L.first();
    L.del_item(it);
    L.contents(it); // illegal access
    it = nil;
    L.contents(it); // illegal access
    

  3. point p(2.0, 3.0); // p has coordinates (2.0, 3.0)
    point q; // q has coordinates but it is not known which
    

  4. edge e;
    forall(e, G.all_edges()) // dangerous!
      { ... } 
    
    // do it like this
    list<edge> E = G.all_edges();
    forall(e, E) 
     { ... }
    

  5. list_item it;
    forall(it, L) {
      L.append(1); // illegal; results in infinite loop
      if(L[it] == 5 ) L.del(it); // legal
      if(L[it] == 6 ) L.del(L.succ(it)); // illegal
      L[it]++; // legal
    }
    

  6. class pair {
    public:
     int x, y;
    
     pair() { x = y = 0; }
     pair(const pair& p) { x = p.x; y = p.y; }
     pair& operator=(const pair& p) {
           if(this != &p) { x = p.x; y = p.y; }
           return *this;
           }
    };
    
    std::istream& operator>> (std::istream& is, pair& p) 
       { is >> p.x >> p.y; return is; }
    std::ostream& operator<< (std::ostream& os, const pair& p) 
       { os << p.x << " " << p.y; return os; }
    

  7. namespace leda {
    int compare(const pair& p, const pair& q)
    {
      if (p.x < q.x) return  -1;
      if (p.x > q.x) return   1; 
      if (p.y < q.y) return  -1;
      if (p.y > q.y) return   1;
      return 0;
    }
    };
    

  8. namespace leda {
    int Hash(const pair& p)
    {
      return p.x ^ p.y;
    }
    };
    
    bool operator == (const pair& p, const pair& q)
    {
       return (p.x == q.x && p.y == q.y) ? true : false;
    }
    


next up previous contents index
Next: The LEDA Tools for Up: The golden LEDA rules Previous: The LEDA rules in   Contents   Index
Christian Uhrig 2017-04-07