~ubuntu-branches/ubuntu/raring/notecase/raring

« back to all changes in this revision

Viewing changes to src/lib/DocumentIterator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nathan Handler
  • Date: 2008-12-21 13:09:58 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221130958-0ri77h0x7j1dclkq
Tags: 1.9.8-0ubuntu1
New upstream release (LP: #307752)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: Implements iterator/helper methods for traversing NoteDocument object
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
#include "DocumentIterator.h"
10
 
#include "debug.h"
11
 
 
12
 
DocumentIterator::DocumentIterator(NoteDocument &doc) : m_doc(doc)
13
 
{
14
 
}
15
 
 
16
 
DocumentIterator::~DocumentIterator()
17
 
{
18
 
}
19
 
 
20
 
int DocumentIterator::GetChildCount(int nParentID, bool bRecursive)
21
 
{
22
 
        int nCount = 0;
23
 
        for(unsigned int i=0; i<m_doc.GetNodeCount(); i++)
24
 
        {
25
 
                if(m_doc.m_lstNodes[i].m_nParentID == nParentID)
26
 
                {
27
 
                        nCount++;
28
 
                        if(bRecursive)
29
 
                                nCount += GetChildCount(m_doc.m_lstNodes[i].m_nID, true);
30
 
                }
31
 
        }
32
 
        return nCount;
33
 
}
34
 
 
35
 
int DocumentIterator::GetChildIdx(int nParentID, int nSiblingIdx)
36
 
{
37
 
        for(unsigned int i=0; i<m_doc.GetNodeCount(); i++)
38
 
        {
39
 
                if(m_doc.m_lstNodes[i].m_nParentID == nParentID)
40
 
                {
41
 
                        if(m_doc.m_lstNodes[i].m_nSiblingIdx == nSiblingIdx)
42
 
                                return i;
43
 
                }
44
 
        }
45
 
 
46
 
        return -1;
47
 
}
48
 
 
49
 
int DocumentIterator::NodeIdx2RecursiveIdx(int nIdx)
50
 
{
51
 
        int nRecursiveIdx = -1;
52
 
 
53
 
        while(nIdx >= 0)
54
 
        {
55
 
                int nParentID   = m_doc.GetNodeByIdx(nIdx).m_nParentID;
56
 
                int nSiblingIdx = m_doc.GetNodeByIdx(nIdx).m_nSiblingIdx;
57
 
 
58
 
                //count the current node
59
 
                nRecursiveIdx ++;
60
 
 
61
 
                //get "previous" node
62
 
                nIdx = -1;
63
 
                if(nSiblingIdx > 0){
64
 
                        nIdx = GetChildIdx(nParentID, nSiblingIdx-1);   //previous sibling
65
 
 
66
 
                        //recursively go to the last node child
67
 
                        int nID = m_doc.GetNodeByIdx(nIdx).m_nID;
68
 
                        int nChildCount = GetChildCount(nID);
69
 
                        while(nChildCount > 0)
70
 
                        {
71
 
                                nIdx = GetChildIdx(nID, nChildCount-1);
72
 
                                nID = m_doc.GetNodeByIdx(nIdx).m_nID;
73
 
                                nChildCount = GetChildCount(nID);
74
 
                        }
75
 
                }
76
 
                else
77
 
                        nIdx = m_doc.GetIdxFromID(nParentID);                                   //
78
 
                if(nIdx < 0)
79
 
                        break;
80
 
        }
81
 
 
82
 
        return nRecursiveIdx;
83
 
}
84
 
 
85
 
int DocumentIterator::RecursiveIdx2NodeIdx(int nIdx)
86
 
{
87
 
        int nCounter = -1;
88
 
        return RecursiveGetIdx(nIdx, ROOT_PARENT_ID, nCounter);
89
 
}
90
 
 
91
 
int DocumentIterator::RecursiveGetIdx(int nRIdx, int nParentID, int &nCounter)
92
 
{
93
 
        int nChildCnt = GetChildCount(nParentID);
94
 
        if(nChildCnt > 0)
95
 
        {
96
 
                //recurse into children
97
 
                for(int i=0; i<nChildCnt; i++)
98
 
                {
99
 
                        nCounter ++;
100
 
 
101
 
                        int nChildIdx = GetChildIdx(nParentID, i);
102
 
                        if(nChildIdx >= 0)
103
 
                        {
104
 
                                if(nCounter == nRIdx)
105
 
                                        return nChildIdx;
106
 
 
107
 
                                int nChildID = m_doc.GetNodeByIdx(nChildIdx).m_nID;
108
 
                                int nIdxNode = RecursiveGetIdx(nRIdx, nChildID, nCounter);
109
 
                                if(nIdxNode >= 0)
110
 
                                        return nIdxNode;
111
 
                        }
112
 
                }
113
 
        }
114
 
 
115
 
        return -1; //not found at this level
116
 
}
117
 
 
118
 
bool DocumentIterator::IsSiblingByIdx(int nIdx1, int nIdx2)
119
 
{
120
 
        //siblings are nodes with the same aprent
121
 
        return (m_doc.GetNodeByIdx(nIdx1).m_nParentID == m_doc.GetNodeByIdx(nIdx2).m_nParentID);
122
 
}
123
 
 
124
 
bool DocumentIterator::IsAncestorByIdx(int nAncestorIdx, int nIdx)
125
 
{
126
 
        int nAncestorID = m_doc.GetNodeByIdx(nAncestorIdx).m_nID;
127
 
 
128
 
        //check each parent node up to the tree root
129
 
        int nNodeIdx = nIdx;
130
 
        while (nNodeIdx >= 0)
131
 
        {
132
 
                int nPID = m_doc.GetNodeByIdx(nNodeIdx).m_nParentID;
133
 
                if(nPID == nAncestorID)
134
 
                        return true;    //parent or grandparent or ...
135
 
 
136
 
                nNodeIdx = m_doc.GetIdxFromID(nPID); //recurse to the parent node
137
 
        }
138
 
 
139
 
        return false; //not an ancestor
140
 
}
141
 
 
142
 
NoteNode &DocumentIterator::GetNodeByID(int nID)
143
 
{
144
 
        int nIdx = m_doc.GetIdxFromID(nID);
145
 
        ASSERT(nIdx >= 0);
146
 
        return m_doc.GetNodeByIdx(nIdx);
147
 
}
148
 
 
149
 
int DocumentIterator::CalcNodeLevel(int nIdx)
150
 
{
151
 
        int nLevel = 0;
152
 
        int nParentID = m_doc.GetNodeByIdx(nIdx).m_nParentID;
153
 
        while(nParentID >= 0){
154
 
                nLevel ++;
155
 
                nParentID = GetNodeByID(nParentID).m_nParentID;
156
 
        }
157
 
        return nLevel;
158
 
}
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Implements iterator/helper methods for traversing NoteDocument object
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "DocumentIterator.h"
 
10
#include "debug.h"
 
11
 
 
12
DocumentIterator::DocumentIterator(NoteDocument &doc) : m_doc(doc)
 
13
{
 
14
}
 
15
 
 
16
DocumentIterator::~DocumentIterator()
 
17
{
 
18
}
 
19
 
 
20
int DocumentIterator::GetChildCount(int nParentID, bool bRecursive)
 
21
{
 
22
        int nCount = 0;
 
23
        for(unsigned int i=0; i<m_doc.GetNodeCount(); i++)
 
24
        {
 
25
                if(m_doc.m_lstNodes[i].m_nParentID == nParentID)
 
26
                {
 
27
                        nCount++;
 
28
                        if(bRecursive)
 
29
                                nCount += GetChildCount(m_doc.m_lstNodes[i].m_nID, true);
 
30
                }
 
31
        }
 
32
        return nCount;
 
33
}
 
34
 
 
35
int DocumentIterator::GetChildIdx(int nParentID, int nSiblingIdx)
 
36
{
 
37
        for(unsigned int i=0; i<m_doc.GetNodeCount(); i++)
 
38
        {
 
39
                if(m_doc.m_lstNodes[i].m_nParentID == nParentID)
 
40
                {
 
41
                        if(m_doc.m_lstNodes[i].m_nSiblingIdx == nSiblingIdx)
 
42
                                return i;
 
43
                }
 
44
        }
 
45
 
 
46
        return -1;
 
47
}
 
48
 
 
49
int DocumentIterator::NodeIdx2RecursiveIdx(int nIdx)
 
50
{
 
51
        int nRecursiveIdx = -1;
 
52
 
 
53
        while(nIdx >= 0)
 
54
        {
 
55
                int nParentID   = m_doc.GetNodeByIdx(nIdx).m_nParentID;
 
56
                int nSiblingIdx = m_doc.GetNodeByIdx(nIdx).m_nSiblingIdx;
 
57
 
 
58
                //count the current node
 
59
                nRecursiveIdx ++;
 
60
 
 
61
                //get "previous" node
 
62
                nIdx = -1;
 
63
                if(nSiblingIdx > 0){
 
64
                        nIdx = GetChildIdx(nParentID, nSiblingIdx-1);   //previous sibling
 
65
 
 
66
                        //recursively go to the last node child
 
67
                        int nID = m_doc.GetNodeByIdx(nIdx).m_nID;
 
68
                        int nChildCount = GetChildCount(nID);
 
69
                        while(nChildCount > 0)
 
70
                        {
 
71
                                nIdx = GetChildIdx(nID, nChildCount-1);
 
72
                                nID = m_doc.GetNodeByIdx(nIdx).m_nID;
 
73
                                nChildCount = GetChildCount(nID);
 
74
                        }
 
75
                }
 
76
                else
 
77
                        nIdx = m_doc.GetIdxFromID(nParentID);                                   //
 
78
                if(nIdx < 0)
 
79
                        break;
 
80
        }
 
81
 
 
82
        return nRecursiveIdx;
 
83
}
 
84
 
 
85
int DocumentIterator::RecursiveIdx2NodeIdx(int nIdx)
 
86
{
 
87
        int nCounter = -1;
 
88
        return RecursiveGetIdx(nIdx, ROOT_PARENT_ID, nCounter);
 
89
}
 
90
 
 
91
int DocumentIterator::RecursiveGetIdx(int nRIdx, int nParentID, int &nCounter)
 
92
{
 
93
        int nChildCnt = GetChildCount(nParentID);
 
94
        if(nChildCnt > 0)
 
95
        {
 
96
                //recurse into children
 
97
                for(int i=0; i<nChildCnt; i++)
 
98
                {
 
99
                        nCounter ++;
 
100
 
 
101
                        int nChildIdx = GetChildIdx(nParentID, i);
 
102
                        if(nChildIdx >= 0)
 
103
                        {
 
104
                                if(nCounter == nRIdx)
 
105
                                        return nChildIdx;
 
106
 
 
107
                                int nChildID = m_doc.GetNodeByIdx(nChildIdx).m_nID;
 
108
                                int nIdxNode = RecursiveGetIdx(nRIdx, nChildID, nCounter);
 
109
                                if(nIdxNode >= 0)
 
110
                                        return nIdxNode;
 
111
                        }
 
112
                }
 
113
        }
 
114
 
 
115
        return -1; //not found at this level
 
116
}
 
117
 
 
118
bool DocumentIterator::IsSiblingByIdx(int nIdx1, int nIdx2)
 
119
{
 
120
        //siblings are nodes with the same aprent
 
121
        return (m_doc.GetNodeByIdx(nIdx1).m_nParentID == m_doc.GetNodeByIdx(nIdx2).m_nParentID);
 
122
}
 
123
 
 
124
bool DocumentIterator::IsAncestorByIdx(int nAncestorIdx, int nIdx)
 
125
{
 
126
        int nAncestorID = m_doc.GetNodeByIdx(nAncestorIdx).m_nID;
 
127
 
 
128
        //check each parent node up to the tree root
 
129
        int nNodeIdx = nIdx;
 
130
        while (nNodeIdx >= 0)
 
131
        {
 
132
                int nPID = m_doc.GetNodeByIdx(nNodeIdx).m_nParentID;
 
133
                if(nPID == nAncestorID)
 
134
                        return true;    //parent or grandparent or ...
 
135
 
 
136
                nNodeIdx = m_doc.GetIdxFromID(nPID); //recurse to the parent node
 
137
        }
 
138
 
 
139
        return false; //not an ancestor
 
140
}
 
141
 
 
142
NoteNode &DocumentIterator::GetNodeByID(int nID)
 
143
{
 
144
        int nIdx = m_doc.GetIdxFromID(nID);
 
145
        ASSERT(nIdx >= 0);
 
146
        return m_doc.GetNodeByIdx(nIdx);
 
147
}
 
148
 
 
149
int DocumentIterator::CalcNodeLevel(int nIdx)
 
150
{
 
151
        int nLevel = 0;
 
152
        int nParentID = m_doc.GetNodeByIdx(nIdx).m_nParentID;
 
153
        while(nParentID >= 0){
 
154
                nLevel ++;
 
155
                nParentID = GetNodeByID(nParentID).m_nParentID;
 
156
        }
 
157
        return nLevel;
 
158
}