~ubuntu-branches/ubuntu/natty/gecode/natty

« back to all changes in this revision

Viewing changes to iter/ranges-cache.icc

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2005-12-24 07:51:25 UTC
  • Revision ID: james.westby@ubuntu.com-20051224075125-klkiqofvbfvusfvt
Tags: upstream-1.0.0.dfsg.1
ImportĀ upstreamĀ versionĀ 1.0.0.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Main authors:
 
3
 *     Christian Schulte <schulte@gecode.org>
 
4
 *
 
5
 *  Copyright:
 
6
 *     Christian Schulte, 2004
 
7
 *
 
8
 *  Last modified:
 
9
 *     $Date: 2005-07-29 21:55:06 +0200 (Fri, 29 Jul 2005) $ by $Author: schulte $
 
10
 *     $Revision: 2085 $
 
11
 *
 
12
 *  This file is part of Gecode, the generic constraint
 
13
 *  development environment:
 
14
 *     http://www.gecode.org
 
15
 *
 
16
 *  See the file "LICENSE" for information on usage and
 
17
 *  redistribution of this file, and for a
 
18
 *     DISCLAIMER OF ALL WARRANTIES.
 
19
 *
 
20
 */
 
21
 
 
22
#include "support/shared-array.hh"
 
23
 
 
24
namespace Gecode { namespace Iter { namespace Ranges {
 
25
 
 
26
  /**
 
27
   * \brief %Range iterator cache
 
28
   *
 
29
   * Allows to iterate the ranges as defined by the input iterator
 
30
   * several times provided the Cache is %reset by the reset member
 
31
   * function.
 
32
   *
 
33
   * Requires \code #include "iter.hh" \endcode
 
34
   * \ingroup FuncIterRanges
 
35
   */
 
36
  template <class I>
 
37
  class Cache  {
 
38
  protected:
 
39
    /// %Ranges stored in cache
 
40
    class Range {
 
41
    public:
 
42
      int min; int max;
 
43
    };
 
44
    /// Array for ranges
 
45
    Support::SharedArray<Range> r;
 
46
    /// Current range
 
47
    int c;
 
48
    /// Number of ranges in cache
 
49
    int n;
 
50
  public:
 
51
    /// \name Constructors and initialization
 
52
    //@{
 
53
    /// Default constructor
 
54
    Cache(void);
 
55
    /// Initialize with ranges from \a i
 
56
    Cache(I& i);
 
57
    /// Initialize with ranges from \a i
 
58
    void init(I& i);
 
59
    //@}
 
60
 
 
61
    /// \name Iteration control
 
62
    //@{
 
63
    /// Test whether iterator is still at a range or done
 
64
    bool operator()(void) const;
 
65
    /// Move iterator to next range (if possible)
 
66
    void operator++(void);
 
67
    /// Reset iterator to start from beginning
 
68
    void reset(void);
 
69
    //@}
 
70
 
 
71
    /// \name %Range access
 
72
    //@{
 
73
    /// Return smallest value of range
 
74
    int min(void) const;
 
75
    /// Return largest value of range
 
76
    int max(void) const;
 
77
    /// Return width of range (distance between minimum and maximum)
 
78
    unsigned int width(void) const;
 
79
    //@}
 
80
  };
 
81
 
 
82
 
 
83
  template <class I>
 
84
  forceinline
 
85
  Cache<I>::Cache(void)
 
86
    : r(8) {}
 
87
 
 
88
  template <class I>
 
89
  inline void
 
90
  Cache<I>::init(I& i) {
 
91
    int j = 0;
 
92
    while (i()) {
 
93
      r.ensure(j);
 
94
      r[j].min = i.min(); r[j].max = i.max();
 
95
      ++j; ++i;
 
96
    }
 
97
    c = 0;
 
98
    n = j;
 
99
  }
 
100
 
 
101
  template <class I>
 
102
  inline
 
103
  Cache<I>::Cache(I& i) : r(8) {
 
104
    init(i);
 
105
  }
 
106
 
 
107
  template <class I>
 
108
  forceinline void
 
109
  Cache<I>::operator++(void) {
 
110
    c++;
 
111
  }
 
112
  template <class I>
 
113
  forceinline bool
 
114
  Cache<I>::operator()(void) const {
 
115
    return c < n;
 
116
  }
 
117
 
 
118
  template <class I>
 
119
  forceinline void
 
120
  Cache<I>::reset(void) {
 
121
    c = 0;
 
122
  }
 
123
 
 
124
  template <class I>
 
125
  forceinline int
 
126
  Cache<I>::min(void) const {
 
127
    return r[c].min;
 
128
  }
 
129
  template <class I>
 
130
  forceinline int
 
131
  Cache<I>::max(void) const {
 
132
    return r[c].max;
 
133
  }
 
134
  template <class I>
 
135
  forceinline unsigned int
 
136
  Cache<I>::width(void) const {
 
137
    return r[c].max-r[c].min+1;
 
138
  }
 
139
 
 
140
}}}
 
141
 
 
142
// STATISTICS: iter-any
 
143