<commit_before>/** @file

  Vector and Set templates with qsort.

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#ifndef _vec_H_
#define _vec_H_

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "defalloc.h"
#include "ink_assert.h"

// Simple Vector class, also supports open hashed sets

#define VEC_INTEGRAL_SHIFT_DEFAULT      2               /* power of 2 (1 << VEC_INTEGRAL_SHIFT)*/
#define VEC_INTEGRAL_SIZE               (1 << (S))
#define VEC_INITIAL_SHIFT               ((S)+1)
#define VEC_INITIAL_SIZE                (1 << VEC_INITIAL_SHIFT)

#define SET_LINEAR_SIZE         4               /* must be <= than VEC_INTEGRAL_SIZE */
#define SET_INITIAL_INDEX       2

template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT>  // S must be a power of 2
class Vec {
 public:
  size_t        n;
  size_t        i;      // size index for sets, reserve for vectors
  C             *v;
  C             e[VEC_INTEGRAL_SIZE];
  
  Vec();
  Vec<C,A,S>(const Vec<C,A,S> &vv);
  Vec<C,A,S>(const C c);
  ~Vec();

  C &operator[](int i) const { return v[i]; }

  C get(size_t i);
  void add(C a);  
  void push_back(C a) { add(a); } // std::vector name
  bool add_exclusive(C a);
  C& add();
  C pop();
  void reset();
  void clear();
  void free_and_clear();
  void delete_and_clear();
  void set_clear();
  C *set_add(C a);
  void set_remove(C a); // expensive, use BlockHash for cheaper remove
  C *set_add_internal(C a);
  bool set_union(Vec<C,A,S> &v);
  int set_intersection(Vec<C,A,S> &v);
  int some_intersection(Vec<C,A,S> &v);
  int some_disjunction(Vec<C,A,S> &v);
  int some_difference(Vec<C,A,S> &v);
  void set_intersection(Vec<C,A,S> &v, Vec<C,A,S> &result);
  void set_disjunction(Vec<C,A,S> &v, Vec<C,A,S> &result);
  void set_difference(Vec<C,A,S> &v, Vec<C,A,S> &result);
  size_t set_count() const;
  size_t count() const;
  C *in(C a);
  C *set_in(C a);
  C first_in_set();
  C *set_in_internal(C a);
  void set_expand();
  ssize_t index(C a) const;
  void set_to_vec();
  void vec_to_set();
  void move(Vec<C,A,S> &v);
  void copy(const Vec<C,A,S> &v);
  void fill(size_t n);
  void append(const Vec<C> &v);
  template <typename CountType> void append(const C * src, CountType count);
  void prepend(const Vec<C> &v);
  void remove_index(int index);
  void remove(C a) { int i = index(a); if (i>=0) remove_index(i); }
  C& insert(size_t index);
  void insert(size_t index, Vec<C> &vv);
  void insert(size_t index, C a);
  void push(C a) { insert(0, a); }
  void reverse();
  void reserve(size_t n);
  C* end() const { return v + n; }
  C &first() const { return v[0]; }
  C &last() const { return v[n-1]; }
  Vec<C,A,S>& operator=(Vec<C,A,S> &v) { this->copy(v); return *this; }
  unsigned length () const { return n; }
  // vector::size() intentionally not implemented because it should mean "bytes" not count of elements
  int write(int fd);
  int read(int fd);
  void qsort(bool (*lt)(C,C));
  
private:
  void move_internal(Vec<C,A,S> &v);
  void copy_internal(const Vec<C,A,S> &v);
  void add_internal(C a);
  C& add_internal();
  void addx();
};

// c -- class, p -- pointer to elements of v, v -- vector
#define forv_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = (_v).v[0]; \
                    ((intptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))
#define for_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, _p = (_v).v[0]; \
                    ((intptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))
#define forvp_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = &(_v).v[0]; \
                    ((intptr_t)(qq__##_p) < (_v).length()) && ((_p = &(_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))

template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> class Accum { public:
  Vec<C,A,S> asset;
  Vec<C,A,S> asvec;<commit_msg>Fix build errors for Ubuntu.<commit_after>/** @file

  Vector and Set templates with qsort.

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#ifndef _vec_H_
#define _vec_H_

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "defalloc.h"
#include "ink_assert.h"

// Simple Vector class, also supports open hashed sets

#define VEC_INTEGRAL_SHIFT_DEFAULT      2               /* power of 2 (1 << VEC_INTEGRAL_SHIFT)*/
#define VEC_INTEGRAL_SIZE               (1 << (S))
#define VEC_INITIAL_SHIFT               ((S)+1)
#define VEC_INITIAL_SIZE                (1 << VEC_INITIAL_SHIFT)

#define SET_LINEAR_SIZE         4               /* must be <= than VEC_INTEGRAL_SIZE */
#define SET_INITIAL_INDEX       2

template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT>  // S must be a power of 2
class Vec {
 public:
  size_t        n;
  size_t        i;      // size index for sets, reserve for vectors
  C             *v;
  C             e[VEC_INTEGRAL_SIZE];
  
  Vec();
  Vec<C,A,S>(const Vec<C,A,S> &vv);
  Vec<C,A,S>(const C c);
  ~Vec();

  C &operator[](int i) const { return v[i]; }

  C get(size_t i);
  void add(C a);  
  void push_back(C a) { add(a); } // std::vector name
  bool add_exclusive(C a);
  C& add();
  C pop();
  void reset();
  void clear();
  void free_and_clear();
  void delete_and_clear();
  void set_clear();
  C *set_add(C a);
  void set_remove(C a); // expensive, use BlockHash for cheaper remove
  C *set_add_internal(C a);
  bool set_union(Vec<C,A,S> &v);
  int set_intersection(Vec<C,A,S> &v);
  int some_intersection(Vec<C,A,S> &v);
  int some_disjunction(Vec<C,A,S> &v);
  int some_difference(Vec<C,A,S> &v);
  void set_intersection(Vec<C,A,S> &v, Vec<C,A,S> &result);
  void set_disjunction(Vec<C,A,S> &v, Vec<C,A,S> &result);
  void set_difference(Vec<C,A,S> &v, Vec<C,A,S> &result);
  size_t set_count() const;
  size_t count() const;
  C *in(C a);
  C *set_in(C a);
  C first_in_set();
  C *set_in_internal(C a);
  void set_expand();
  ssize_t index(C a) const;
  void set_to_vec();
  void vec_to_set();
  void move(Vec<C,A,S> &v);
  void copy(const Vec<C,A,S> &v);
  void fill(size_t n);
  void append(const Vec<C> &v);
  template <typename CountType> void append(const C * src, CountType count);
  void prepend(const Vec<C> &v);
  void remove_index(int index);
  void remove(C a) { int i = index(a); if (i>=0) remove_index(i); }
  C& insert(size_t index);
  void insert(size_t index, Vec<C> &vv);
  void insert(size_t index, C a);
  void push(C a) { insert(0, a); }
  void reverse();
  void reserve(size_t n);
  C* end() const { return v + n; }
  C &first() const { return v[0]; }
  C &last() const { return v[n-1]; }
  Vec<C,A,S>& operator=(Vec<C,A,S> &v) { this->copy(v); return *this; }
  unsigned length () const { return n; }
  // vector::size() intentionally not implemented because it should mean "bytes" not count of elements
  int write(int fd);
  int read(int fd);
  void qsort(bool (*lt)(C,C));
  
private:
  void move_internal(Vec<C,A,S> &v);
  void copy_internal(const Vec<C,A,S> &v);
  void add_internal(C a);
  C& add_internal();
  void addx();
};

// c -- class, p -- pointer to elements of v, v -- vector
#define forv_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = (_v).v[0]; \
                    ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))
#define for_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, _p = (_v).v[0]; \
                    ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))
#define forvp_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = &(_v).v[0]; \
                    ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = &(_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1))

template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> class Accum { public:
  Vec<C,A,S> asset;
  Vec<C,A,S> asvec;
{'fraction_non_alphanumeric': 0.16135375698613125, 'fraction_numerical': 0.006002897950734837, 'mean_word_length': 3.494418604651163, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3770579', 'n_tokens_mistral': 3738, 'n_tokens_neox': 3705, 'n_words': 1077}