Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / commands / Struct.groovy @ 2087

History | View | Annotate | Download (1.6 kB)

1
package org.txm.macro.commands;
2

    
3
class Struct implements Comparable<Struct> {
4

    
5
        String  name
6
        Integer start
7
        Integer end
8

    
9
        Struct(String n, Integer s, Integer e) {
10
                name  = n
11
                start = s
12
                end   = e
13
        }
14

    
15
        public int compareTo(Struct s) {
16
                if (start < s.start && end > s.end) {                         // self contains s : [ { } ]
17
                        //println sprintf("%s[%d, %d] ^ %s[%d, %d]", name, start, end, s.name, s.start, s.end)
18
                        return -1
19
                } else if (start > s.start && end < s.end) {         // s contains self : { [ ] }
20
                        //println sprintf("%s[%d, %d] v %s[%d, %d]", name, start, end, s.name, s.start, s.end)
21
                        return 1
22
                } else if (start == s.start && end == s.end) {         // self and s have the same intervals : [{ }]
23
                        //println sprintf("%s[%d, %d] = %s[%d, %d]", name, start, end, s.name, s.start, s.end)
24
                        return name.compareTo(s.name) // use the lexicographic order of the structure names
25
                } else if (start < s.start) {                                         // interval starting on the left comes first : [ { ...
26
                        return -1
27
                } else if (start > s.start) {                                         // interval starting on the right comes after : { [ ...
28
                        return 1
29
                } else if (end > s.end) {                                                 // same start, interval ending on the right comes before : [{ } ]...
30
                        return -1
31
                } else if (end < s.end) {                                                 // same start, interval ending on the right comes before : [{ ] }...
32
                        return -1
33
                } else {                                                                                // same start, same end : [{ ]}...
34
                        return name.compareTo(s.name) // use the lexicographic order of the structure names
35
                }
36
        }
37

    
38
        public toString(Struct s) {
39
                sprintf("%s[%d, %d]", s.name, s.start, s.end)
40
        }
41

    
42
        public print(Struct s) {
43
                print(s.toString())
44
        }
45
}