LinkedListNode.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Collections.Generic.LinkedListNode
  3. //
  4. // Author:
  5. // David Waite
  6. //
  7. // (C) 2005 David Waite ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2005 David Waite
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Runtime.InteropServices;
  34. namespace System.Collections.Generic
  35. {
  36. [ComVisible (false)]
  37. public sealed class LinkedListNode <T>
  38. {
  39. T item;
  40. LinkedList <T> container;
  41. internal LinkedListNode <T> forward, back;
  42. public LinkedListNode (T value)
  43. {
  44. item = value;
  45. }
  46. internal LinkedListNode (LinkedList <T> list, T value)
  47. {
  48. container = list;
  49. item = value;
  50. this.back = this.forward = this;
  51. }
  52. internal LinkedListNode (LinkedList <T> list, T value, LinkedListNode <T> previousNode, LinkedListNode <T> nextNode)
  53. {
  54. container = list;
  55. item = value;
  56. this.back = previousNode;
  57. this.forward = nextNode;
  58. previousNode.forward = this;
  59. nextNode.back = this;
  60. }
  61. internal void Detach ()
  62. {
  63. back.forward = forward;
  64. forward.back = back;
  65. forward = back = null;
  66. container = null;
  67. }
  68. internal void SelfReference (LinkedList <T> list)
  69. {
  70. forward = this;
  71. back = this;
  72. container = list;
  73. }
  74. internal void InsertBetween (LinkedListNode <T> previousNode, LinkedListNode <T> nextNode, LinkedList <T> list)
  75. {
  76. previousNode.forward = this;
  77. nextNode.back = this;
  78. this.forward = nextNode;
  79. this.back = previousNode;
  80. this.container = list;
  81. }
  82. public LinkedList <T> List {
  83. get { return container; }
  84. }
  85. public LinkedListNode <T> Next {
  86. get { return (container != null && forward != container.first) ? forward : null; }
  87. }
  88. public LinkedListNode <T> Previous {
  89. get { return (container != null && this != container.first) ? back : null ; }
  90. }
  91. public T Value {
  92. get { return item; }
  93. set { item = value; }
  94. }
  95. }
  96. }
  97. #endif