~ubuntu-branches/ubuntu/vivid/vowpal-wabbit/vivid

« back to all changes in this revision

Viewing changes to vowpalwabbit/v_array.h

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-08-27 20:52:23 UTC
  • mfrom: (1.2.1) (7.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130827205223-q005ps71tqinh25v
Tags: 7.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) by respective owners including Yahoo!, Microsoft, and
 
3
individual contributors. All rights reserved.  Released under a BSD
 
4
license as described in the file LICENSE.
 
5
 */
 
6
#ifndef VARRAY_H__
 
7
#define VARRAY_H__
 
8
#include <iostream>
 
9
#include <stdlib.h>
 
10
#include <string.h>
 
11
#include <assert.h>
 
12
#include <stdint.h>
 
13
 
 
14
#ifdef _WIN32
 
15
#define __INLINE 
 
16
#else
 
17
#define __INLINE inline
 
18
#endif
 
19
 
 
20
const size_t erase_point = ~ ((1 << 10) -1);
 
21
 
 
22
template<class T> class v_array{
 
23
 public:
 
24
  T* begin;
 
25
  T* end;
 
26
  T* end_array;
 
27
  size_t erase_count;
 
28
 
 
29
  T last() { return *(end-1);}
 
30
  T pop() { return *(--end);}
 
31
  bool empty() { return begin == end;}
 
32
  void decr() { end--;}
 
33
  v_array() { begin= NULL; end = NULL; end_array=NULL; erase_count = 0;}
 
34
  T& operator[](size_t i) { return begin[i]; }
 
35
  size_t size(){return end-begin;}
 
36
  void resize(size_t length, bool zero_everything=false)
 
37
    {
 
38
      if ((size_t)(end_array-begin) != length)
 
39
        {
 
40
          size_t old_len = end-begin;
 
41
          begin = (T *)realloc(begin, sizeof(T) * length);
 
42
          if ((begin == NULL) && ((sizeof(T)*length) > 0)) {
 
43
            std::cerr << "realloc of " << length << " failed in resize().  out of memory?" << std::endl;
 
44
            throw std::exception();
 
45
          }
 
46
          if (zero_everything && (old_len < length))
 
47
            memset(begin+old_len, 0, (length-old_len)*sizeof(T));
 
48
          end = begin+old_len;
 
49
          end_array = begin + length;
 
50
        }
 
51
    }
 
52
 
 
53
  void erase() 
 
54
  { if (++erase_count & erase_point)
 
55
      {
 
56
        resize(end-begin);
 
57
        erase_count = 0;
 
58
      }
 
59
    end = begin;
 
60
  }
 
61
  void delete_v()
 
62
  {
 
63
    if (begin != NULL)
 
64
      free(begin);
 
65
    begin = end = end_array = NULL;
 
66
  }
 
67
  void push_back(const T &new_ele)
 
68
  {
 
69
    if(end == end_array)
 
70
      resize(2 * (end_array-begin) + 3);
 
71
    *(end++) = new_ele;
 
72
  }
 
73
};
 
74
 
 
75
 
 
76
#ifdef _WIN32
 
77
#undef max
 
78
#undef min
 
79
#endif
 
80
 
 
81
inline size_t max(size_t a, size_t b)
 
82
{ if ( a < b) return b; else return a;
 
83
}
 
84
inline size_t min(size_t a, size_t b)
 
85
{ if ( a < b) return a; else return b;
 
86
}
 
87
 
 
88
template<class T> void copy_array(v_array<T>& dst, v_array<T> src)
 
89
{
 
90
  dst.erase();
 
91
  push_many(dst, src.begin, src.size());
 
92
}
 
93
 
 
94
template<class T> void copy_array(v_array<T>& dst, v_array<T> src, T(*copy_item)(T))
 
95
{
 
96
  dst.erase();
 
97
  for (T*item = src.begin; item != src.end; item++)
 
98
    dst.push_back(copy_item(*item));
 
99
}
 
100
 
 
101
template<class T> void push_many(v_array<T>& v, const T* begin, size_t num)
 
102
{
 
103
  if(v.end+num >= v.end_array)
 
104
    v.resize(max(2 * (size_t)(v.end_array - v.begin) + 3, 
 
105
                 v.end - v.begin + num));
 
106
  memcpy(v.end, begin, num * sizeof(T));
 
107
  v.end += num;
 
108
}
 
109
 
 
110
template<class T> void calloc_reserve(v_array<T>& v, size_t length)
 
111
{
 
112
  v.begin = (T *)calloc(length, sizeof(T));
 
113
  v.end = v.begin;
 
114
  v.end_array = v.begin + length;
 
115
}
 
116
 
 
117
template<class T> v_array<T> pop(v_array<v_array<T> > &stack)
 
118
{
 
119
  if (stack.end != stack.begin)
 
120
    return *(--stack.end);
 
121
  else
 
122
    return v_array<T>();
 
123
}
 
124
 
 
125
#endif  // VARRAY_H__