001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009 010package imagingbook.common.util; 011 012import java.util.Iterator; 013 014/** 015 * This class implements a 1D map for arbitrary objects with flexible bottom and top index, similar to an array in 016 * Pascal. Containers are immutable (apart from element values). 017 * 018 * @param <T> the container's element type 019 */ 020public class LinearContainer<T> implements Iterable<T> { 021 022 private final int botIndex, topIndex; 023 private final T[] data; 024 025 /** 026 * Creates a LinearContainer with the index range [0, n - 1], like an ordinary array. 027 * 028 * @param n size of the container. 029 */ 030 public LinearContainer(int n) { 031 this(0, n - 1); 032 } 033 034 /** 035 * Creates a LinearContainer with the index range [botIndex, topIndex]. 036 * 037 * @param botIndex bottom (smallest) index. 038 * @param topIndex top (largest) index. 039 */ 040 @SuppressWarnings("unchecked") 041 public LinearContainer(int botIndex, int topIndex) { 042 if (botIndex > topIndex) { 043 throw new IllegalArgumentException("LinearContainer: botIndex > topIndex"); 044 } 045 this.botIndex = botIndex; 046 this.topIndex = topIndex; 047 int n = topIndex - botIndex + 1; 048 data = (T[]) new Object[n]; 049 } 050 051 /** 052 * Returns the k-th element of this container. 053 * 054 * @param k the element index 055 * @return the k-th element 056 */ 057 public T getElement(int k) { 058 // TODO: check for out-of-bounds index k 059 return data[k - botIndex]; 060 } 061 062 /** 063 * Sets (replaces) the k-th element of this container. 064 * 065 * @param k the element index 066 * @param elem the new element 067 */ 068 public void setElement(int k, T elem) { 069 // TODO: check for out-of-bounds index k 070 data[k - botIndex] = elem; 071 } 072 073 /** 074 * Returns the bottom index of this container. 075 * 076 * @return the bottom index 077 */ 078 public int getBotIndex() { 079 return botIndex; 080 } 081 082 /** 083 * Returns the top index of this container. 084 * 085 * @return the top index 086 */ 087 public int getTopIndex() { 088 return topIndex; 089 } 090 091 @Override 092 public Iterator<T> iterator() { 093 //return new ArrayIterator<T>(data); 094 return ArrayUtils.getIterator(data); 095 } 096 097}