这篇文章给大家介绍在c++于java项目中自定义Troop<T>泛型类达到方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Troop<T>是一个泛型列表操作类,适用于非高性能和非大数据量的要求
这篇文章给大家介绍在c++于java项目中自定义Troop<T>泛型类达到方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Troop<T>是一个泛型列表操作类,适用于非高性能和非大数据量的要求。包括了:取值get,赋值set,追加append,插入insert,清除remove,进队enqueue,出队dequeue,交换swap,滚动roll,进栈push,出栈pop等日常操作。
//for more infORMation, please access Http://www.one-lab.netusing System;using System.Collections.Generic;using System.Text;namespace onelab{ public interface ISortable { void sort(bool decending); } public interface IStringable { int fromString(String input); String toString(); } public class Troop<T> : ICloneable { public override int GetHashCode() { return base.GetHashCode(); } public override bool Equals(object obj) { return this == (Troop<T>)obj; } public object Clone() { Troop<T> oValue = new Troop<T>(); oValue.cloneFrom(this); return oValue; } protected bool equalsTo(Troop<T> aValue) { int len = aValue.length(); if (len == this.length()) { for (int n = 0; n < len; n++) { if (!aValue.data[n].Equals(data[n])) return false; } return true; } return false; } protected void cloneFrom(Troop<T> aValue) { data.Clear(); data.AddRange(aValue.data); } public static bool operator ==(Troop<T> a0, Troop<T> a1) { object o1 = (object)a0; object o2 = (object)a1; if (o1 == null && o2 == null) return true; return (o1 == null || o2 == null) ? false : a0.equalsTo(a1); } public static bool operator !=(Troop<T> a0, Troop<T> a1) { object o1 = (object)a0; object o2 = (object)a1; if (o1 == null && o2 == null) return false; return (o1 == null || o2 == null) ? true : !a0.equalsTo(a1); } private bool mHasError = false; public bool hasError() { return mHasError; } public List<T> data = new List<T>(); public T get(int index) { mHasError = false; if (index >= 0 && index < data.Count) return data[index]; mHasError = true; return default(T); } public bool set(int index, T value) { if (index >= 0 && index < data.Count) { data[index] = value; return true; } return false; } public void append(T value) { data.Add(value); } public bool insert(int index, T value) { if (index >= 0 && index < data.Count) { data.Insert(index, value); return true; } return false; } public void appendRange(ref List<T> range) { data.AddRange(range); } public bool insertRange(int index, ref List<T> range) { if (index >= 0 && index < data.Count) { data.InsertRange(index, range); return true; } return false; } public void clear() { data.Clear(); } public bool remove(int index) { if (index >= 0 && index < data.Count) { data.RemoveAt(index); return true; } return false; } public bool removeRange(int index, int count) { if (count > 0 && index >= 0 && ((index + count) <= data.Count)) { data.RemoveRange(index, count); return true; } return false; } public int length() { return data.Count; } public void enqueue(T value) { data.Insert(0, value); } public T dequeue() { mHasError = false; int length = data.Count; if (length > 0) { T b = data[length - 1]; data.RemoveAt(length - 1); return b; } mHasError = true; return default(T); } public void push(T value) { data.Add(value); } public T pop() { return dequeue(); } public Troop<T> getRange(int index, int count) { Troop<T> output = new Troop<T>(); mHasError = true; if (count > 0 && index >= 0 && ((index + count) <= data.Count)) { output.data = data.GetRange(index, count); mHasError = false; } return output; } public void reverse() { data.Reverse(); } public bool swap(int index0, int index1) { int length = data.Count; if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length) { T v = data[index0]; data[index0] = data[index1]; data[index1] = v; return true; } return false; } public T take(int index) { mHasError = false; if (index >= 0 && index < data.Count) { T v = data[index]; data.RemoveAt(index); return v; } mHasError = true; return default(T); } public void rollForward(int offset) { if (offset >= data.Count) return; List<T> left = data.GetRange(offset - 1, data.Count - offset + 1); List<T> right = data.GetRange(0, offset - 1); data.Clear(); data.AddRange(left); data.AddRange(right); } public void rollBackward(int offset) { if (offset >= data.Count) return; List<T> left = data.GetRange(offset, data.Count - offset); List<T> right = data.GetRange(0, offset); data.Clear(); data.AddRange(left); data.AddRange(right); } }}
--结束END--
本文标题: 在c++于java项目中自定义Troop<T>泛型类达到方法
本文链接: https://lsjlt.com/news/226029.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0