// inserts element with key x into list
void skip_list::insert(int x) {
    vector<element*> update(max_height+1);
    element* p = &head;

    for (int i = height; i>=0; --i) {
        while (p->next[i]->key < x) {
            p = p->next[i];
        }
        update[i] = p;
    }
    p = p->next[0];

    if (p->key == x) return;    // key x exists already in list

    int new_height = random_height();
    if (new_height > height) {  // link new element to head, adjust list height
        for (int i = height+1; i<=new_height; ++i) {
            update[i] = &head;
        }
        height = new_height;
    }

    // create new element with height new_height and key x
    p = new element(x,new_height);

    // insert p into level i lists immediately after element update[i]
    for (int i = 0; i<=new_height; ++i) {
        p->next[i] = update[i]->next[i];
        update[i]->next[i] = p;
    }
}
Exemple #2
0
// Internal method of rope_insert.
// This function creates a new node in the rope at the specified position and fills it with the
// passed string.
static void insert_at(rope *r, rope_iter *iter,
    const uint8_t *str, size_t num_bytes, size_t num_chars) {
#if ROPE_WCHAR
  size_t num_wchars = count_wchars_in_utf8(str, num_chars);
#endif

  // This describes how many levels of the iter are filled in.
  uint8_t max_height = r->head.height;
  uint8_t new_height = random_height();
  rope_node *new_node = alloc_node(r, new_height);
  new_node->num_bytes = num_bytes;
  memcpy(new_node->str, str, num_bytes);

  assert(new_height < ROPE_MAX_HEIGHT);

  // Max height (the rope's head's height) must be 1+ the height of the largest node.
  while (max_height <= new_height) {
    r->head.height++;
    r->head.nexts[max_height] = r->head.nexts[max_height - 1];

    // This is the position (offset from the start) of the rope.
    iter->s[max_height] = iter->s[max_height - 1];
    max_height++;
  }

  // Fill in the new node's nexts array.
  int i;
  for (i = 0; i < new_height; i++) {
    rope_skip_node *prev_skip = &iter->s[i].node->nexts[i];
    new_node->nexts[i].node = prev_skip->node;
    new_node->nexts[i].skip_size = num_chars + prev_skip->skip_size - iter->s[i].skip_size;


    prev_skip->node = new_node;
    prev_skip->skip_size = iter->s[i].skip_size;

    // & move the iterator to the end of the newly inserted node.
    iter->s[i].node = new_node;
    iter->s[i].skip_size = num_chars;
#if ROPE_WCHAR
    new_node->nexts[i].wchar_size = num_wchars + prev_skip->wchar_size - iter->s[i].wchar_size;
    prev_skip->wchar_size = iter->s[i].wchar_size;
    iter->s[i].wchar_size = num_wchars;
#endif
  }

  for (; i < max_height; i++) {
    iter->s[i].node->nexts[i].skip_size += num_chars;
    iter->s[i].skip_size += num_chars;
#if ROPE_WCHAR
    iter->s[i].node->nexts[i].wchar_size += num_wchars;
    iter->s[i].wchar_size += num_wchars;
#endif
  }

  r->num_chars += num_chars;
  r->num_bytes += num_bytes;
}
Exemple #3
0
// Internal method of rope_insert.
static void insert_at(rope *r, size_t pos, const uint8_t *str,
    size_t num_bytes, size_t num_chars, rope_node *nodes[], size_t tree_offsets[]) {
  // This describes how many of the nodes[] and tree_offsets[] arrays are filled in.
  uint8_t max_height = r->height;
  uint8_t new_height = random_height();
  rope_node *new_node = alloc_node(r, new_height);
  new_node->num_bytes = num_bytes;
  memcpy(new_node->str, str, num_bytes);
  
  // Ensure the rope has enough capacity to store the next pointers to the new object.
  if (new_height > max_height) {
    r->height = new_height;
    if (r->height > r->height_capacity) {
      do {
        r->height_capacity *= 2;
      } while (r->height_capacity < r->height);
      r->heads = (rope_next_node *)r->realloc(r->heads, sizeof(rope_next_node) * r->height_capacity);
    }
  }

  // Fill in the new node's nexts array.
  int i;
  for (i = 0; i < new_height; i++) {
    if (i < max_height) {
      rope_next_node *prev_node = (nodes[i] ? &nodes[i]->nexts[i] : &r->heads[i]);
      new_node->nexts[i].node = prev_node->node;
      new_node->nexts[i].skip_size = num_chars + prev_node->skip_size - tree_offsets[i];

      prev_node->node = new_node;
      prev_node->skip_size = tree_offsets[i];
    } else {
      // Edit the head node instead of editing the parent listed in nodes.
      new_node->nexts[i].node = NULL;
      new_node->nexts[i].skip_size = r->num_chars - pos + num_chars;
      
      r->heads[i].node = new_node;
      r->heads[i].skip_size = pos;
    }
    
    nodes[i] = new_node;
    tree_offsets[i] = num_chars;
  }
  
  for (; i < max_height; i++) {
    if (nodes[i]) {
      nodes[i]->nexts[i].skip_size += num_chars;
    } else {
      r->heads[i].skip_size += num_chars;
    }
    tree_offsets[i] += num_chars;
  }
  
  r->num_chars += num_chars;
  r->num_bytes += num_bytes;
}
void PixelFeatureLayer<Dtype>::Forward_cpu(
  const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  const PixelFeatureParameter& parameter =
    this->layer_param_.pixel_feature_param();

  Dtype* top_data = top[0]->mutable_cpu_data();

  switch (this->layer_param_.pixel_feature_param().type()) {
  case PixelFeatureParameter_Feature_POSITION: {
    if (!ran_once) {
      const Dtype scale = parameter.pos_scale();
      const Dtype offset_h = parameter.offset_h();
      const Dtype offset_w = parameter.offset_w();

      for (unsigned int n = 0; n < num_; ++n) {
        for (unsigned int y = 0; y < height_; ++y) {
          Dtype y_offset = scale * y + offset_h;
          for (unsigned int x = 0; x < width_; ++x) {
            top_data[top[0]->offset(n, 0, y, x)] = y_offset;
            top_data[top[0]->offset(n, 1, y, x)] = scale * x + offset_w;
          }
        }
      }
    }

    break;
  }
  case PixelFeatureParameter_Feature_POSITION_AND_RGB: {
    const Dtype scale = parameter.pos_scale();
    const Dtype color_scale = parameter.color_scale();
    const Dtype offset_h = parameter.offset_h();
    const Dtype offset_w = parameter.offset_w();

    for (unsigned int n = 0; n < num_; ++n) {
      for (unsigned int y = 0; y < height_; ++y) {
        Dtype y_offset = scale * y + offset_h;
        for (unsigned int x = 0; x < width_; ++x) {
          top_data[top[0]->offset(n, 0, y, x)] = y_offset;
          top_data[top[0]->offset(n, 1, y, x)] = scale * x + offset_w;
          for (unsigned int c = 0; c < bottom[0]->channels(); ++c) {
            top_data[top[0]->offset(n, c+2, y, x)] =
              color_scale * bottom[0]->data_at(n, c, y, x);
          }
        }
      }
    }
    break;
  }
  case PixelFeatureParameter_Feature_RGB_AND_POSITION: {
    const Dtype scale = parameter.pos_scale();
    const Dtype color_scale = parameter.color_scale();
    const Dtype offset_h = parameter.offset_h();
    const Dtype offset_w = parameter.offset_w();

    for (unsigned int n = 0; n < num_; ++n) {
      for (unsigned int y = 0; y < height_; ++y) {
        Dtype y_offset = scale * y + offset_h;
        for (unsigned int x = 0; x < width_; ++x) {
          for (unsigned int c = 0; c < bottom[0]->channels(); ++c) {
            top_data[top[0]->offset(n, c, y, x)] =
              color_scale * bottom[0]->data_at(n, c, y, x);
          }
          top_data[top[0]->offset(n, bottom[0]->channels(), y, x)] = y_offset;
          top_data[top[0]->offset(n, bottom[0]->channels() + 1, y, x)] =
            scale * x + offset_w;
        }
      }
    }
    break;
  }
  case PixelFeatureParameter_Feature_RGB: {
    const Dtype color_scale = parameter.color_scale();
    for (unsigned int n = 0; n < num_; ++n) {
      for (unsigned int y = 0; y < height_; ++y) {
        for (unsigned int x = 0; x < width_; ++x) {
          for (unsigned int c = 0; c < bottom[0]->channels(); ++c) {
            top_data[top[0]->offset(n, c, y, x)] =
              color_scale * bottom[0]->data_at(n, c, y, x);
          }
        }
      }
    }
    break;
  }
  case PixelFeatureParameter_Feature_RANDOM_POSITION:
  case PixelFeatureParameter_Feature_NUM_RANDOM_POSITION: {
    if (!ran_once) {
      const int input_height = bottom[0]->height();
      const int input_width = bottom[0]->width();
      const Dtype scale = parameter.pos_scale();

      boost::uniform_real<Dtype> random_height(0, input_height);
      boost::variate_generator<caffe::rng_t*, boost::uniform_real<Dtype> >
        variate_height(caffe_rng(), random_height);

      boost::uniform_real<Dtype> random_width(0, input_width);
      boost::variate_generator<caffe::rng_t*, boost::uniform_real<Dtype> >
        variate_width(caffe_rng(), random_width);

      for (unsigned int n = 0; n < num_; ++n) {
        for (unsigned int y = 0; y < height_; ++y) {
          for (unsigned int x = 0; x < width_; ++x) {
            top_data[top[0]->offset(n, 0, y, x)] = scale * variate_height();
            top_data[top[0]->offset(n, 1, y, x)] = scale * variate_width();
          }
        }
      }
    }
    break;
  }
  case PixelFeatureParameter_Feature_WARPED_POSITION: {
    if (!ran_once) {
      const Dtype angle = -parameter.rotation_angle() / 180.0 * M_PI;
      const Dtype scale = parameter.pos_scale();
      const Dtype angle_sigma = parameter.rotation_sigma();

      const Dtype cosAngle = std::cos(angle);
      const Dtype sinAngle = std::sin(angle);

      const Dtype mid_y = height_ / 2;
      const Dtype mid_x = width_ / 2;

      Dtype scaled_y, scaled_x;
      for (unsigned int n = 0; n < num_; ++n) {
        for (unsigned int y = 0; y < height_; ++y) {
          scaled_y = y * scale - mid_y;
          for (unsigned int x = 0; x < width_; ++x) {
            scaled_x = x * scale - mid_x;

            top_data[top[0]->offset(n, 0, y, x)] =
              sinAngle * scaled_x + cosAngle * scaled_y + mid_y;
            top_data[top[0]->offset(n, 1, y, x)] =
              cosAngle * scaled_x - sinAngle * scaled_y + mid_x;
            top_data[top[0]->offset(n, 2, y, x)] =
              angle / angle_sigma;
          }
        }
      }
    }
    break;
  }
  case PixelFeatureParameter_Feature_RANDOM_ROTATE: {
    if (!ran_once) {
      boost::uniform_real<Dtype> random_angle(-10, 10);
      boost::variate_generator<caffe::rng_t*, boost::uniform_real<Dtype> >
        variate_angle(caffe_rng(), random_angle);
      const Dtype angle = variate_angle() / 180.0 * M_PI;
      const Dtype scale = 1;

      const Dtype cosAngle = std::cos(angle);
      const Dtype sinAngle = std::sin(angle);

      const Dtype mid_y = height_ / 2;
      const Dtype mid_x = width_ / 2;

      Dtype scaled_y, scaled_x;
      for (unsigned int n = 0; n < num_; ++n) {
        for (unsigned int y = 0; y < height_; ++y) {
          scaled_y = y * scale - mid_y;
          for (unsigned int x = 0; x < width_; ++x) {
            scaled_x = x * scale - mid_x;

            top_data[top[0]->offset(n, 0, y, x)] =
              sinAngle * scaled_x + cosAngle * (scaled_y - mid_y) + mid_y;
            top_data[top[0]->offset(n, 1, y, x)] =
              cosAngle * scaled_x - sinAngle * scaled_y + mid_x;
          }
        }
      }
    }
    break;
  }
  default:
    LOG(FATAL) << "Undefined feature type of pixel feature layer";
  }

  ran_once = true;
}