Advertisement
atm-irbis

List class

May 25th, 2014
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.68 KB | None | 0 0
  1. module list;
  2.  
  3. interface IList(T) {
  4.     @property T[] array_show();
  5.     @property bool contains(T x);
  6.     @property bool empty();
  7.     @property size_t length();
  8.     @property T[] reverse();
  9.     @property T[] uniq();
  10.  
  11.     void append(T x);
  12.     // void create(T[] ...);
  13.     void extend(T[] x...);
  14.     void insert(T x,size_t n);
  15.     void remove(T x);
  16.     void remove_nth(size_t n);
  17. }
  18.  
  19. class List(T) : IList!T {
  20.     private:
  21.         T[] list;
  22.  
  23.     public:
  24.  
  25.         T[] array_show() @property {
  26.             return list;
  27.         }
  28.  
  29.         bool contains(T x) @property {
  30.             foreach (elem; list) {
  31.                 if (elem == x) return true;
  32.             }
  33.             return false;
  34.         }
  35.  
  36.         bool empty() @property {
  37.             if (list.length == 0) return true;
  38.             else return false;
  39.         }
  40.  
  41.         size_t length() @property {
  42.             return list.length;
  43.         }
  44.  
  45.         T[] reverse() @property {
  46.             T[] tmp;
  47.             foreach_reverse(elem; list) {
  48.                 tmp ~= elem;
  49.             }
  50.             return tmp;
  51.         }
  52.  
  53.         T[] uniq() @property {
  54.             auto tmp = new List!T;
  55.             foreach (elem;list) {
  56.                 if (!tmp.contains(elem)) tmp.append(elem);
  57.             }
  58.             return tmp.array_show;
  59.         }
  60.  
  61.         void append(T x) {
  62.             list ~= x;
  63.         }
  64.  
  65.         void extend(T[] x...) {
  66.             list ~= x;
  67.         }
  68.  
  69.         void insert(T x,size_t n) {
  70.             T[] tmp;
  71.             if (n == list.length) list ~= x;
  72.             else {
  73.                 tmp = list[0..n] ~ x ~ list[n..$];
  74.                 list = tmp;
  75.             }
  76.         }
  77.  
  78.         void remove(T x) {
  79.             T[] tmp;
  80.             foreach (elem; list) {
  81.                 if (elem != x) tmp ~= elem;
  82.             }
  83.             list = tmp;
  84.         }
  85.  
  86.         void remove_nth(size_t n) {
  87.             T[] tmp;
  88.             tmp = list[0..n] ~ list[n+1..$];
  89.             list = tmp;
  90.         }      
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement