自定义的Troop泛型类( c++, java和c#)的实现代码
程序员文章站
2023-12-21 20:56:16
troop是一个泛型列表操作类,适用于非高性能和非大数据量的要求。包括了:取值get,赋值set,追加append,插入insert,清除remove,进...
troop<t>是一个泛型列表操作类,适用于非高性能和非大数据量的要求。包括了:取值get,赋值set,追加append,插入insert,清除remove,进队enqueue,出队dequeue,交换swap,滚动roll,进栈push,出栈pop等日常操作。
//for more information, please access http://www.one-lab.net using 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); } } }
//for more information, please access http://www.one-lab.net #ifndef values_h #define values_h #include <qlist> #include <qdebug> namespace onelab { class isortable { public: virtual void sort(bool decending) = 0; }; template <typename t> class troop { private: bool mhaserror; static bool equals(const troop<t> &from, const troop<t> &to); public: qlist<t> data; troop(); t get(int index); bool set(int index, t value); void append(t value); bool insert(int index, t value); void appendrange(const qlist<t>& range); bool insertrange(int index, const qlist<t>& range); void clear(); bool remove(int index); bool removerange(int index, int count); int length() const; void enqueue(t value); t dequeue(); void push(t value); t pop(); bool getrange(int index, int count, troop<t>& output); void reverse(); bool swap(int index0, int index1); t take(int index); void rollforward(int offset); void rollbackward(int offset); bool operator==(const troop& input) const; bool operator!=(const troop& input) const; bool haserror() const; virtual qstring tostring(); virtual int fromstring(const qstring& input); }; } #endif // values_h
//for more information, please access http://www.one-lab.net #include "values.h" #include <qstringlist> #include <qdatetime> namespace onelab { template <typename t> bool troop<t>::equals(const troop<t> &from, const troop<t> &to) { int len = from.length(); if (len == to.length()) { for (int n = 0; n < len; n++) { if (from.data[n] != to.data[n]) return false; } return true; } return false; } template <typename t> troop<t>::troop() { mhaserror = false; } template <typename t> t troop<t>::get(int index) { mhaserror = false; if (index >= 0 && index < data.length()) return data[index]; mhaserror = true; return (t)0; } template <typename t> bool troop<t>::set(int index, t value) { if (index >= 0 && index < data.length()) { data[index] = value; return true; } return false; } template <typename t> void troop<t>::append(t value) { data.append(value); } template <typename t> bool troop<t>::insert(int index, t value) { if (index >= 0 && index < data.length()) { data.insert(index, value); return true; } return false; } template <typename t> void troop<t>::appendrange(const qlist<t> &range) { data.append(range); } template <typename t> bool troop<t>::insertrange(int index, const qlist<t> &range) { int length = data.length(); if (index >= 0 && index < length) { qlist<t> left = data.mid(0, index); qlist<t> right = data.mid(index, -1); left.append(range); data.clear(); data.append(left); data.append(right); return true; } return false; } template <typename t> void troop<t>::clear() { data.clear(); } template <typename t> bool troop<t>::remove(int index) { if (index >= 0 && index < data.length()) { data.removeat(index); return true; } return false; } template <typename t> bool troop<t>::removerange(int index, int count) { if (count > 0 && index >= 0 && ((index + count) <= data.length())) { for (int n = 0; n < count; n++) data.removeat(index); return true; } return false; } template <typename t> int troop<t>::length() const { return data.length(); } template <typename t> void troop<t>::enqueue(t value) { data.insert(0, value); } template <typename t> t troop<t>::dequeue() { mhaserror = false; int length = data.length(); if (length > 0) { t b = data[length - 1]; data.removeat(length - 1); return b; } mhaserror = true; return (t)0; } template <typename t> void troop<t>::push(t value) { data.append(value); } template <typename t> t troop<t>::pop() { return dequeue(); } template <typename t> bool troop<t>::getrange(int index, int count, troop<t>& output) { mhaserror = true; if (count > 0 && index >= 0 && ((index + count) <= data.length())) { output.data = data.mid(index, count); mhaserror = false; } return mhaserror; } template <typename t> void troop<t>::reverse() { int length = data.length(); qlist<t> newdata; for (int n = length - 1; n >= 0; n--) newdata.append(data[n]); data.clear(); data = newdata; } template <typename t> bool troop<t>::swap(int index0, int index1) { int length = data.length(); if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length) { t v = data[index0]; data[index0] = data[index1]; data[index1] = v; return true; } return false; } template <typename t> t troop<t>::take(int index) { mhaserror = false; if (index >= 0 && index < data.length()) { t v = data[index]; data.removeat(index); return v; } mhaserror = true; return (t)0; } template <typename t> void troop<t>::rollforward(int offset) { if (offset >= data.length()) return; qlist<t> left = data.mid(offset - 1, -1); qlist<t> right = data.mid(0, offset - 1); data.clear(); data.append(left); data.append(right); } template <typename t> void troop<t>::rollbackward(int offset) { if (offset >= data.length()) return; qlist<t> left = data.mid(offset, -1); qlist<t> right = data.mid(0, offset); data.clear(); data.append(left); data.append(right); } template <typename t> bool troop<t>::operator!=(const troop& input) const { return !equals(input, *this); } template<typename t> bool troop<t>::haserror() const { return mhaserror; } template<typename t> qstring troop<t>::tostring() { return qstring(); } template<typename t> int troop<t>::fromstring(const qstring&) { return 0; } template <typename t> bool troop<t>::operator==(const troop& input) const { return equals(input, *this); } } //for more information, please access http://www.one-lab.net package net.onelab; import java.util.arraylist; public class troop<t> implements cloneable { public object clone() { troop<t> obj = new troop<t>(); obj.data.addall(data); return obj; } private boolean mhaserror = false; public boolean haserror() { return mhaserror; } public arraylist<t> data = new arraylist<t>(); public t get(int index) { mhaserror = false; if (index >= 0 && index < data.size()) return data.get(index); mhaserror = true; return null; } public boolean set(int index, t value) { if (index >= 0 && index < data.size()) { data.set(index, value); return true; } return false; } public void append(t value) { data.add(value); } public boolean insert(int index, t value) { if (index >= 0 && index < data.size()) { data.add(index, value); return true; } return false; } public void appendrange(arraylist<t> range) { data.addall(range); } public boolean insertrange(int index, arraylist<t> range) { if (index >= 0 && index < data.size()) { data.addall(index, range); return true; } return false; } public void clear() { data.clear(); } public boolean remove(int index) { if (index >= 0 && index < data.size()) { data.remove(index); return true; } return false; } public boolean removerange(int index, int count) { if (count > 0 && index >= 0 && ((index + count) <= data.size())) { for (int n = 0; n < count; n++) data.remove(index); return true; } return false; } public int length() { return data.size(); } public void enqueue(t value) { data.add(0, value); } public t dequeue() { mhaserror = false; int length = data.size(); if (length > 0) { t b = data.get(length - 1); data.remove(length - 1); return b; } mhaserror = true; return null; } public void push(t value) { data.add(value); } public t pop() { return dequeue(); } public troop<t> getrange(int index, int count) { mhaserror = true; troop<t> output = new troop<t>(); if (count > 0 && index >= 0 && ((index + count) <= data.size())) { output.data.addall(data.sublist(index, index + count)); mhaserror = false; } return output; } public void reverse() { int length = data.size(); arraylist<t> newdata = new arraylist<t>(); for (int n = length - 1; n >= 0; n--) newdata.add(data.get(n)); data.clear(); data = newdata; } boolean swap(int index0, int index1) { int length = data.size(); if (index0 >= 0 && index0 < length && index1 >= 0 && index1 < length) { t v = data.get(index0); data.set(index0, data.get(index1)); data.set(index1, v); return true; } return false; } t take(int index) { mhaserror = false; if (index >= 0 && index < data.size()) { t v = data.get(index); data.remove(index); return v; } mhaserror = true; return null; } void rollforward(int offset) { if (offset >= data.size()) return; arraylist<t> left = new arraylist<t>(); left.addall(data.sublist(offset - 1, data.size())); arraylist<t> right = new arraylist<t>(); right.addall(data.sublist(0, offset - 1)); data.clear(); data.addall(left); data.addall(right); } void rollbackward(int offset) { if (offset >= data.size()) return; arraylist<t> left = new arraylist<t>(); left.addall(data.sublist(offset, data.size())); arraylist<t> right = new arraylist<t>(); right.addall(data.sublist(0, offset)); data.clear(); data.addall(left); data.addall(right); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!