Drop of Mainsoft.System.Data
[mono.git] / mcs / class / System.Data / System.Data / Node.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 using System;
25
26 namespace System.Data
27 {
28         /// <summary>
29         /// Summary description for Node.
30         /// </summary>
31         internal class Node
32         {
33                 protected int _iBalance;    // currently, -2 means 'deleted'
34                 internal Node _nNext;       // node of next index (nNext==null || nNext.iId=iId+1)
35                 protected Node _nLeft;
36                 protected Node _nRight;
37                 protected Node _nParent;
38
39                 protected DataRow _row;
40                 
41                 public Node(DataRow row)
42                 {
43                         _row = row;
44                 }
45
46                 internal int GetBalance()
47                 {
48                         if (_iBalance == -2)
49                                 throw new SystemException ("Node is deleted.");
50
51                         return _iBalance;
52                 }
53
54                 internal void Delete()
55                 {
56                         _iBalance = -2;
57                         _nLeft = null;
58                         _nRight = null;
59                         _nParent = null;
60                 }
61
62
63                 internal DataRow Row
64                 {
65                         get {
66                                 return _row;
67                         }
68                 }
69
70                 internal Node Left
71                 {
72                         get {
73                                 if (_iBalance == -2)
74                                         throw new SystemException ("Node is deleted.");
75
76                                 return _nLeft;
77                         }
78
79                         set {
80                                 if (_iBalance == -2)
81                                         throw new SystemException ("Node is deleted.");
82
83                                 _nLeft = value;
84                         }
85                 }
86
87                 internal Node Right
88                 {
89                         get {
90                                 if (_iBalance == -2)
91                                         throw new SystemException ("Node is deleted.");
92                                 return _nRight;
93                         }
94
95                         set {
96                                 if (_iBalance == -2)
97                                         throw new SystemException ("Node is deleted.");
98
99                                 _nRight = value;
100                         }
101                 }
102
103
104                 internal Node Parent
105                 {
106                         get {
107                                 if (_iBalance == -2)
108                                         throw new SystemException ("Node is deleted.");
109
110                                 return _nParent;
111                         }
112
113                         set {
114                                 if (_iBalance == -2)
115                                         throw new SystemException ("Node is deleted.");
116                                 _nParent = value;
117                         }
118                 }
119
120                 internal bool IsRoot()
121                 {
122                         return _nParent == null;
123                 }
124
125
126                 internal void SetBalance(int b)
127                 {
128
129                         if (_iBalance == -2)
130                                 throw new SystemException ("Node is deleted.");
131
132                         _iBalance = b;
133                 }
134
135                 internal bool From()
136                 {
137
138                         if (this.IsRoot()){
139                                 return true;
140                         }
141
142                         if (_iBalance == -2)
143                                 throw new SystemException ("Node is deleted.");
144                         Node parent = Parent;
145
146                         return Equals(parent.Left);
147                 }
148
149                 internal Object[] GetData()
150                 {
151
152                         if (_iBalance == -2)
153                                 throw new SystemException ("Node is deleted.");
154                         return _row.ItemArray;
155                 }
156
157                 internal bool Equals(Node n)
158                 {
159
160                         if (_iBalance == -2)
161                                 throw new SystemException ("Node is deleted.");
162
163                         return n == this;
164                 }
165         }
166 }