/// <summary> /// Find the child value with the specified index. /// </summary> /// <param name="nIndex"> /// Index of the child value to find. /// </param> /// <param name="ofStart"> /// Offset within the parent buffer to start search from. /// </param> /// <param name="iStart"> /// Index of the child value to start search from. /// </param> /// <returns> /// The child value. /// </returns> protected override IPofValue FindChildInternal(int nIndex, int ofStart, int iStart) { BinaryMemoryStream buf = (BinaryMemoryStream)BinaryValue.GetStream(); buf.Position = ofStart; DataReader reader = new DataReader(buf); // skip children until we either find the one we are looking for, // or reach the end of the sparse array (index == -1) int ofLastIndex = ofStart; int iProp = reader.ReadPackedInt32(); while (iProp < nIndex && iProp >= 0) { SkipChild(reader); ofLastIndex = (int)buf.Position; iProp = reader.ReadPackedInt32(); } // child found. extract it from the parent buffer if (iProp == nIndex) { int of = (int)buf.Position; SkipChild(reader); int cb = (int)(buf.Position - of); return(ExtractChild(of, cb)); } // child not found return(InstantiateNullValue(ofLastIndex, nIndex)); }
/// <summary> /// Find the child value with the specified index. /// </summary> /// <param name="nIndex"> /// Index of the child value to find. /// </param> /// <param name="ofStart"> /// Offset within the parent buffer to start search from. /// </param> /// <param name="iStart"> /// Index of the child value to start search from. /// </param> /// <returns> /// The child value. /// </returns> protected override IPofValue FindChildInternal(int nIndex, int ofStart, int iStart) { BinaryMemoryStream buf = (BinaryMemoryStream)BinaryValue.GetStream(); buf.Position = ofStart; DataReader reader = new DataReader(buf); // check array bounds if (nIndex < 0 || nIndex >= Length) { throw new IndexOutOfRangeException( "Element index " + nIndex + " must be in the range [0 .. " + Length + ")."); } // skip children until we find the one we are looking for int iProp = iStart; while (iProp < nIndex) { SkipChild(reader); iProp++; } // child found. parse it and return it int of = (int)buf.Position; SkipChild(reader); int cb = (int)(buf.Position - of); return(ExtractChild(of, cb)); }