我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

在c++于java项目中自定义Troop<T>泛型类达到方法

这篇文章给大家介绍在c++于java项目中自定义Troop泛型类达到方法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

寻甸网站建设公司创新互联,寻甸网站设计制作,有大型网站制作公司丰富经验。已为寻甸上千提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的寻甸做网站的公司定做!

Troop是一个泛型列表操作类,适用于非高性能和非大数据量的要求。包括了:取值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 : ICloneable
  {
    public override int GetHashCode()
    {
      return base.GetHashCode();
    }
    public override bool Equals(object obj) { return this == (Troop)obj; }
    public object Clone()
    {
      Troop oValue = new Troop();
      oValue.cloneFrom(this);
      return oValue;
    }
    protected bool equalsTo(Troop 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 aValue)
    {
      data.Clear();
      data.AddRange(aValue.data);
    }
    public static bool operator ==(Troop a0, Troop 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 a0, Troop 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 data = new List();
    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 range)
    {
      data.AddRange(range);
    }
    public bool insertRange(int index, ref List 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 getRange(int index, int count)
    {
      Troop output = new Troop();
      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 left = data.GetRange(offset - 1, data.Count - offset + 1);
      List 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 left = data.GetRange(offset, data.Count - offset);
      List 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 
#include 

namespace onelab
{
  class ISortable
  {
  public:
    virtual void sort(bool decending) = 0;
  };

  template 
  class Troop
  {    
  private:
    bool mHasError;
    static bool equals(const Troop &from, const Troop &to);
    
  public:
    QList 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& range);
    bool insertRange(int index, const QList& 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& 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 
#include 

namespace onelab
{
template 
bool Troop::equals(const Troop &from, const Troop &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 
Troop::Troop()
{
  mHasError = false;
}

template 
T Troop::get(int index)
{
  mHasError = false;
  if (index >= 0 && index < data.length())
    return data[index];
  mHasError = true;
  return (T)0;    
}

template 
bool Troop::set(int index, T value)
{
  if (index >= 0 && index < data.length())
  {
    data[index] = value;
    return true;
  }
  return false;    
}

template 
void Troop::append(T value)
{
  data.append(value);
}

template 
bool Troop::insert(int index, T value)
{
  if (index >= 0 && index < data.length())
  {
    data.insert(index, value);
    return true;
  }
  return false;    
}

template 
void Troop::appendRange(const QList &range)
{
  data.append(range);
}

template 
bool Troop::insertRange(int index, const QList &range)
{
  int length = data.length();
  if (index >= 0 && index < length)
  {
    QList left = data.mid(0, index);
    QList right = data.mid(index, -1);
    left.append(range);
    data.clear();
    data.append(left);
    data.append(right);
    return true;
  }
  return false;    
}

template 
void Troop::clear()
{
  data.clear();
}

template 
bool Troop::remove(int index)
{
  if (index >= 0 && index < data.length())
  {
    data.removeAt(index);
    return true;
  }
  return false;    
}

template 
bool Troop::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 
int Troop::length() const
{
  return data.length();
}

template 
void Troop::enqueue(T value)
{
  data.insert(0, value);
}

template 
T Troop::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 
void Troop::push(T value)
{
  data.append(value);
}

template 
T Troop::pop()
{
  return dequeue();
}

template 
bool Troop::getRange(int index, int count, Troop& output)
{
  mHasError = true;
  if (count > 0 && index >= 0 && ((index + count) <= data.length()))
  {
    output.data = data.mid(index, count);
    mHasError = false;
  }
  return mHasError; 
}

template 
void Troop::reverse()
{
  int length = data.length();
  QList newData;
  for (int n = length - 1; n >= 0; n--)
    newData.append(data[n]);
  
  data.clear();
  data = newData;    
}

template 
bool Troop::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 
T Troop::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 
void Troop::rollForward(int offset)
{
  if (offset >= data.length()) return;
  QList left = data.mid(offset - 1, -1);
  QList right = data.mid(0, offset - 1);
  data.clear();
  data.append(left);
  data.append(right);
}

template 
void Troop::rollBackward(int offset)
{
  if (offset >= data.length()) return;
  QList left = data.mid(offset, -1);
  QList right = data.mid(0, offset);    
  data.clear();
  data.append(left);
  data.append(right);
}

template 
bool Troop::operator!=(const Troop& input) const
{
  return !equals(input, *this);
}

template
bool Troop::hasError() const
{
  return mHasError;  
}

template
QString Troop::toString()
{
  return QString();  
}

template
int Troop::fromString(const QString&)
{
  return 0;  
}

template 
bool Troop::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 implements Cloneable {
 public Object clone() {
 Troop obj = new Troop();
 obj.data.addAll(data);
 return obj; 
 }

 private boolean mHasError = false;
 public boolean hasError() { return mHasError; }
 public ArrayList data = new ArrayList();
 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 range) {
    data.addAll(range);
  }
  public boolean insertRange(int index, ArrayList 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 getRange(int index, int count) {
    mHasError = true;
    Troop output = new Troop();
    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 newData = new ArrayList(); 
   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 left = new ArrayList();
    left.addAll(data.subList(offset - 1, data.size()));
    ArrayList right = new ArrayList();
    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 left = new ArrayList();
    left.addAll(data.subList(offset, data.size()));
    ArrayList right = new ArrayList();
    right.addAll(data.subList(0, offset));
    data.clear();
    data.addAll(left);
    data.addAll(right);
  }   
} 

关于在c++于java项目中自定义Troop泛型类达到方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享题目:在c++于java项目中自定义Troop<T>泛型类达到方法
网站链接:http://shouzuofang.com/article/gepjpd.html

其他资讯