/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/utils/range.vala

  • Committer: Gustav Hartvigsson
  • Date: 2022-06-01 12:14:52 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220601121452-ntu94w67q3dhhfeq
More work torwards inperementing the parser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contects of this file is in the Public Domain.
 
3
 *
 
4
 * Created by Gustav Hartivgsson.
 
5
 */
 
6
[CCode (cname = "V", cprefix = "v_")]
 
7
namespace Utils {
 
8
  [CCode (cname = "VRangeIterator", cprefix = "v_range_iterator_")]
 
9
  public class RangeIterator {
 
10
    private Range range;
 
11
    private int current;
 
12
    public RangeIterator (Range range) {
 
13
      this.range = range;
 
14
      this.current = this.range.start;
 
15
    }
 
16
     public int get () {
 
17
       return this.current;
 
18
     }
 
19
 
 
20
    public bool next () {
 
21
      if (!this.range.reverse) {
 
22
        if (this.current <= this.range.end) {
 
23
          this.current++;
 
24
          return true;
 
25
        }
 
26
      } else {
 
27
        if (this.current >= this.range.end){
 
28
          this.current--;
 
29
        }
 
30
      }
 
31
      return false;
 
32
    }
 
33
 
 
34
    public bool has_next () {
 
35
      return (this.current < this.range.end);
 
36
    }
 
37
  }
 
38
 
 
39
  [CCode (cname = "VRange", cprefix = "v_range_")]
 
40
  public class Range {
 
41
    public bool reverse {get; private set;}
 
42
    public int start {get; private set;}
 
43
    public int end {get; private set;}
 
44
 
 
45
    public Range (int start, int end) {
 
46
      if (start <= end) {
 
47
        this.reverse = false;
 
48
      } else {
 
49
        this.reverse = true;
 
50
      }
 
51
      this.start = start;
 
52
      this.end = end;
 
53
    }
 
54
 
 
55
    public Type element_type () {
 
56
      return typeof (int);
 
57
    }
 
58
     
 
59
    public RangeIterator iterator () {
 
60
      return new RangeIterator (this);
 
61
    }
 
62
  }
 
63
}