Skip to content

Instantly share code, notes, and snippets.

@shole
Created January 20, 2018 17:57
Show Gist options
  • Save shole/50d145ff6aa8b0763c5340d0f684a5db to your computer and use it in GitHub Desktop.
Save shole/50d145ff6aa8b0763c5340d0f684a5db to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
public class CombinedRefList<T> : IEnumerable {
public List<IList<T>> lists = new List<IList<T>>();
public T this[int index] {
get {
if ( lists.Count != 0 ) {
int indexes = 0;
foreach ( IList<T> list in lists ) {
if ( indexes + list.Count > index ) {
return list[index - indexes];
}
indexes += list.Count;
}
}
throw new Exception("Invalid index " + index);
}
set {
if ( lists.Count != 0 ) {
int indexes = 0;
foreach ( IList<T> list in lists ) {
if ( indexes + list.Count > index ) {
list[index - indexes] = value;
return;
}
indexes += list.Count;
}
}
throw new Exception("Invalid index " + index);
}
}
public int Count {
get {
int indexes = 0;
foreach ( IList<T> list in lists ) {
indexes += list.Count;
}
return indexes;
}
}
public IEnumerator GetEnumerator() {
foreach ( IList<T> list in lists ) {
foreach ( T item in list ) {
yield return item;
}
}
}
}
@shole
Copy link
Author

shole commented Jan 20, 2018

for testing;

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class combinedlisttest : MonoBehaviour {

void Start() {
	mixedtest();
	//arraytest();
	//listtest();
}

void mixedtest() {
	Debug.Log("list&array test with ints");
	CombinedRefList<int> refList = new CombinedRefList<int>();
	List<int> lista = new List<int>();
	lista.Add(1);
	lista.Add(2);
	lista.Add(3);
	refList.lists.Add(lista);
	int[] listb = new int[] {
		4,
		5,
		6
	};
	refList.lists.Add(listb);

	lista.Add(33);
	refList[4] = 44;

	Debug.Log("len " + refList.Count);
	foreach ( int i in refList ) {
		Debug.Log(i);
	}
}

void arraytest() {
	Debug.Log("array test with strings");
	CombinedRefList<string> refList = new CombinedRefList<string>();
	string[] lista = new string[] {
		"" + 1,
		"" + 2,
		"" + 3
	};
	refList.lists.Add(lista);
	string[] listb = new string[] {
		"" + 4,
		"" + 5,
		"" + 6
	};
	refList.lists.Add(listb);

	refList[2] = "33";
	listb[0] = "44";
	Debug.Log("len " + refList.Count);
	foreach ( string i in refList ) {
		Debug.Log(i);
	}
}

void listtest() {
	Debug.Log("list test with ints");
	CombinedRefList<int> refList = new CombinedRefList<int>();
	List<int> lista = new List<int>();
	lista.Add(1);
	lista.Add(2);
	lista.Add(3);
	refList.lists.Add(lista);
	List<int> listb = new List<int>();
	listb.Add(4);
	listb.Add(5);
	listb.Add(6);
	refList.lists.Add(listb);

	refList[2] = 33;
	lista.Add(44);

	Debug.Log("len " + refList.Count);
	foreach ( int i in refList ) {
		Debug.Log(i);
	}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment