Chunked List : List « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » ListScreenshots 
Chunked List
 
//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
using System.Collections.Generic;

namespace Isotope.Collections
{
    public class ChunkedList<T> : IEnumerable<Isotope.Collections.ChunkedList<T>.RowEnumerator>
    {
        private readonly List<T> data;
        private readonly int _chunkSize;

        public ChunkedList(int chunksize, int capacity)
        {
            if (chunksize < 1)
            {
                throw new System.ArgumentOutOfRangeException("chunksize""must be at least 1");
            }

            this._chunkSize = chunksize;
            this.data = new List<T>(chunksize * capacity);
        }

        public T[] ToArray()
        {
            return this.data.ToArray();
        }

        public void Add(params T[] items)
        {
            this.AddChunk(items);
        }

        public void AddChunk(ICollection<T> items)
        {
            if (items.Count != _chunkSize)
            {
                string msg = string.Format("must add exactly {0} items", items.Count);
                throw new System.ArgumentOutOfRangeException(msg);
            }

            this.data.AddRange(items);

            if (this.data.Count % this._chunkSize != 0)
            {
                throw new System.Exception("Did not maintain consistency of chunked list");
            }
        }

        public int Count
        {
            get
            {
                return this.data.Count / this._chunkSize;
            }
        }

        public int ChunkSize
        {
            get return _chunkSize; }
        }

        public void GetRow(int r, T[] row_array)
        {
            int startindex = r * this._chunkSize;
            for (int c = 0; c < this._chunkSize; c++)
            {
                row_array[cthis.data[startindex + c];
            }
        }

        public T[] GetRow(int r)
        {
            var a = new T[this._chunkSize];
            this.GetRow(r, a);
            return a;
        }

        public this[int row, int col]
        {
            get
            {
                if (row < 0)
                {
                    throw new System.ArgumentOutOfRangeException("row""must be greater than zero");
                }

                if (col < 0)
                {
                    throw new System.ArgumentOutOfRangeException("col""must be greater than zero");
                }

                if (row >= this.Count)
                {
                    throw new System.ArgumentOutOfRangeException("row");
                }

                if (col >= this._chunkSize)
                {
                    throw new System.ArgumentOutOfRangeException("col");
                }

                return this.data[(row * this._chunkSize+ col];
            }
        }



        // Implement IEnumerable<T>
        System.Collections.Generic.IEnumerator<RowEnumerator>
            System.Collections.Generic.IEnumerable<RowEnumerator>.GetEnumerator()
        {
            return this.AsEnumerable().GetEnumerator();
        }

        // Implement IEnumerable
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.AsEnumerable().GetEnumerator();
        }


        private T[] CreateRowBuffer()
        {
            var row = new T[this._chunkSize];
            return row;
        }

        private IEnumerable<RowEnumerator> AsEnumerable()
        {
            var row_array = this.CreateRowBuffer();

            for (int r = 0; r < this.Count; r++)
            {
                this.GetRow(r, row_array);
                yield return new RowEnumerator(r, row_array);
            }
        }

        public struct RowEnumerator
        {
            public readonly int Index;
            public readonly T[] ItemArray;

            internal RowEnumerator(int index, T[] itemarray)
            {
                this.Index = index;
                this.ItemArray = itemarray;
            }
        }
    }
}

   
  
Related examples in the same category
1.new List() Add(assembly1) Indexer
2.Get the size of a list
3.Add Item to a list object
4.Search Test
5.Remove Item from a List
6.Change Contents
7.Reverse Contents
8.Hash List to Hash Array
9.Load elements into a generic list from an array
10.Lazy List
11.Slice list
12.Flatten a List
13.Keyed List
14.Convert a collection of strings to a comma separated list.
15.Convert List To Array
16.Compare two Lists
17.Name Value Pair List
18.Get Distinct value from a List
19.Adds the elements of the specified collection to the specified generic IList.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.