/// <summary> /// Combines this <see cref="DataRegion"/> with another one. /// </summary> /// <param name="otherRegion">The other <see cref="DataRegion"/> that intersects /// with, or is adjacent to, this one.</param> /// <returns>The combined <see cref="DataRegion"/>.</returns> /// <exception cref="ArgumentException">Thrown if the <paramref name="otherRegion"/> /// does not intersect with this one and is not adjacent to this one. /// See <see cref="CanCombineWith(DataRegion)"/>.</exception> public DataRegion CombineWith(DataRegion otherRegion) { if (!this.CanCombineWith(otherRegion)) { throw new ArgumentException("Cannot combine non-intersecting " + nameof(DataRegion) + "s.", nameof(otherRegion)); } return(new DataRegion(Math.Min(this.FirstIndex, otherRegion.FirstIndex), Math.Max(this.LastIndex, otherRegion.LastIndex))); }
/// <summary> /// Checks whether this <see cref="DataRegion"/> intersects another. /// </summary> /// <param name="otherRegion">The other <see cref="DataRegion"/>.</param> /// <returns>True if this <see cref="DataRegion"/> contains at least one /// index that is also contained by the <paramref name="otherRegion"/>, /// otherwise false.</returns> public bool Intersects(DataRegion otherRegion) { return(this.LastIndex >= otherRegion.FirstIndex && otherRegion.LastIndex >= this.FirstIndex); }
/// <summary> /// Checks whether this <see cref="DataRegion"/> completely contains another. /// </summary> /// <param name="otherRegion">The other <see cref="DataRegion"/>.</param> /// <returns>True if the <paramref name="otherRegion"/> completely fits within /// this <see cref="DataRegion"/>, otherwise false.</returns> public bool Contains(DataRegion otherRegion) { return(otherRegion.FirstIndex >= this.FirstIndex && otherRegion.LastIndex <= this.LastIndex); }
/// <summary> /// Checks whether this <see cref="DataRegion"/> can be combined with another. /// </summary> /// <param name="otherRegion">The other <see cref="DataRegion"/>.</param> /// <returns>True if this <see cref="DataRegion"/> can be combined with the <paramref name="otherRegion"/>, /// otherwise false.</returns> /// <remarks> /// Two <see cref="DataRegion"/>s can be combined only if they are adjacent or /// intersecting. See <see cref="Intersects(DataRegion)"/> and /// <see cref="IsAdjacent(DataRegion)"/>. /// </remarks> public bool CanCombineWith(DataRegion otherRegion) { return(IsAdjacent(otherRegion) || Intersects(otherRegion)); }
/// <summary> /// Checks whether this <see cref="DataRegion"/> is adjacent to another. /// </summary> /// <param name="otherRegion">The other <see cref="DataRegion"/>.</param> /// <returns>True if this <see cref="DataRegion"/> is adjacent to <paramref name="otherRegion"/>, /// otherwise false.</returns> /// <remarks> /// Two <see cref="DataRegion"/>s are considered adjacent only if there is /// no gap between them. Intersecting <see cref="DataRegion"/>s are <em>not</em> /// considered adjacent. /// </remarks> public bool IsAdjacent(DataRegion otherRegion) { return(otherRegion.LastIndex == this.FirstIndex - 1 ||//'otherRegion' is left adjacent to 'this' otherRegion.FirstIndex == this.LastIndex + 1);//'otherRegion' is right adjacent to 'this' }