Changeset 13654


Ignore:
Timestamp:
Dec 13, 2012, 1:47:40 AM (10 years ago)
Author:
jordan
Message:

copyediting: indentation cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/ptrarray.c

    r13625 r13654  
    2525tr_ptrArrayDestruct (tr_ptrArray * p, PtrArrayForeachFunc func)
    2626{
    27     assert (p);
    28     assert (p->items || !p->n_items);
    29 
    30     if (func)
    31         tr_ptrArrayForeach (p, func);
    32 
    33     tr_free (p->items);
    34 
    35     memset (p, ~0, sizeof (tr_ptrArray));
     27  assert (p != NULL);
     28  assert (p->items || !p->n_items);
     29
     30  if (func)
     31    tr_ptrArrayForeach (p, func);
     32
     33  tr_free (p->items);
    3634}
    3735
    3836void
    39 tr_ptrArrayForeach (tr_ptrArray *       t,
    40                     PtrArrayForeachFunc func)
    41 {
    42     int i;
    43 
    44     assert (t);
    45     assert (t->items || !t->n_items);
    46     assert (func);
    47 
    48     for (i = 0; i < t->n_items; ++i)
    49         func (t->items[i]);
     37tr_ptrArrayForeach (tr_ptrArray * t, PtrArrayForeachFunc func)
     38{
     39  int i;
     40
     41  assert (t);
     42  assert (t->items || !t->n_items);
     43  assert (func);
     44
     45  for (i=0; i<t->n_items; ++i)
     46    func (t->items[i]);
    5047}
    5148
    5249void**
    53 tr_ptrArrayPeek (tr_ptrArray * t,
    54                  int *         size)
    55 {
    56     *size = t->n_items;
    57     return t->items;
     50tr_ptrArrayPeek (tr_ptrArray * t, int * size)
     51{
     52  *size = t->n_items;
     53  return t->items;
    5854}
    5955
    6056int
    61 tr_ptrArrayInsert (tr_ptrArray * t,
    62                    void        * ptr,
    63                    int           pos)
    64 {
    65     if (t->n_items >= t->n_alloc)
    66     {
    67         t->n_alloc = MAX (FLOOR, t->n_alloc * 2);
    68         t->items = tr_renew (void*, t->items, t->n_alloc);
    69     }
    70 
    71     if (pos < 0 || pos > t->n_items)
    72         pos = t->n_items;
    73     else
    74         memmove (t->items + pos + 1,
    75                  t->items + pos,
    76                  sizeof (void*) * (t->n_items - pos));
    77 
    78     t->items[pos] = ptr;
    79     t->n_items++;
    80     return pos;
     57tr_ptrArrayInsert (tr_ptrArray * t, void * ptr, int pos)
     58{
     59  if (t->n_items >= t->n_alloc)
     60    {
     61      t->n_alloc = MAX (FLOOR, t->n_alloc * 2);
     62      t->items = tr_renew (void*, t->items, t->n_alloc);
     63    }
     64
     65  if (pos < 0 || pos > t->n_items)
     66    pos = t->n_items;
     67  else
     68    memmove (t->items+pos+1, t->items+pos, sizeof(void*)*(t->n_items-pos));
     69
     70  t->items[pos] = ptr;
     71  t->n_items++;
     72  return pos;
    8173}
    8274
     
    8476tr_ptrArrayPop (tr_ptrArray* t)
    8577{
    86     void * ret = NULL;
    87 
    88     if (t->n_items)
    89         ret = t->items[--t->n_items];
    90 
    91     return ret;
     78  void * ret = NULL;
     79
     80  if (t->n_items)
     81    ret = t->items[--t->n_items];
     82
     83  return ret;
    9284}
    9385
    9486void
    95 tr_ptrArrayErase (tr_ptrArray * t,
    96                   int           begin,
    97                   int           end)
    98 {
    99     assert (begin >= 0);
    100     if (end < 0) end = t->n_items;
    101     assert (begin < end);
    102     assert (end <= t->n_items);
    103 
    104     memmove (t->items + begin,
    105              t->items + end,
    106              sizeof (void*) * (t->n_items - end));
    107 
    108     t->n_items -= (end - begin);
     87tr_ptrArrayErase (tr_ptrArray * t, int begin, int end)
     88{
     89  if (end < 0)
     90    end = t->n_items;
     91
     92  assert (begin >= 0);
     93  assert (begin < end);
     94  assert (end <= t->n_items);
     95
     96  memmove (t->items+begin, t->items+end, sizeof(void*)*(t->n_items-end));
     97
     98  t->n_items -= (end - begin);
    10999}
    110100
     
    119109                       bool               * exact_match)
    120110{
    121     int pos = -1;
    122     bool match = false;
    123 
    124     if (t->n_items == 0)
    125     {
    126         pos = 0;
    127     }
    128     else
    129     {
    130         int first = 0;
    131         int last = t->n_items - 1;
    132 
    133         for (;;)
     111  int pos = -1;
     112  bool match = false;
     113
     114  if (t->n_items == 0)
     115    {
     116      pos = 0;
     117    }
     118  else
     119    {
     120      int first = 0;
     121      int last = t->n_items - 1;
     122
     123      for (;;)
    134124        {
    135             const int half = (last - first) / 2;
    136             const int c = compare (t->items[first + half], ptr);
    137 
    138             if (c < 0) {
    139                 const int new_first = first + half + 1;
    140                 if (new_first > last) {
    141                     pos = new_first;
    142                     break;
     125          const int half = (last - first) / 2;
     126          const int c = compare (t->items[first + half], ptr);
     127
     128          if (c < 0)
     129            {
     130              const int new_first = first + half + 1;
     131              if (new_first > last)
     132                {
     133                  pos = new_first;
     134                  break;
    143135                }
    144                 first = new_first;
     136              first = new_first;
    145137            }
    146             else if (c > 0) {
    147                 const int new_last = first + half - 1;
    148                 if (new_last < first) {
    149                     pos = first;
    150                     break;
     138          else if (c > 0)
     139            {
     140              const int new_last = first + half - 1;
     141              if (new_last < first)
     142                {
     143                  pos = first;
     144                  break;
    151145                }
    152                 last = new_last;
     146              last = new_last;
    153147            }
    154             else {
    155                 match = true;
    156                 pos = first + half;
    157                 break;
     148          else
     149            {
     150              match = true;
     151              pos = first + half;
     152              break;
    158153            }
    159154        }
    160155    }
    161156
    162     if (exact_match)
    163         *exact_match = match;
    164     return pos;
     157  if (exact_match != NULL)
     158    *exact_match = match;
     159
     160  return pos;
    165161}
    166162
     
    170166{
    171167#ifndef NDEBUG
    172     int i;
    173 
    174     for (i = 0; i < t->n_items - 2; ++i)
    175         assert (compare (t->items[i], t->items[i + 1]) < 0);
     168  int i;
     169
     170  for (i=0; i<t->n_items-2; ++i)
     171    assert (compare (t->items[i], t->items[i+1]) < 0);
    176172#endif
    177173}
     
    179175int
    180176tr_ptrArrayInsertSorted (tr_ptrArray * t,
    181                          void *        ptr,
     177                         void        * ptr,
    182178                         int           compare (const void*, const void*))
    183179{
    184     int pos;
    185     int ret;
    186 
    187     assertSortedAndUnique (t, compare);
    188 
    189     pos = tr_ptrArrayLowerBound (t, ptr, compare, NULL);
    190     ret = tr_ptrArrayInsert (t, ptr, pos);
    191 
    192     assertSortedAndUnique (t, compare);
    193     return ret;
     180  int pos;
     181  int ret;
     182
     183  assertSortedAndUnique (t, compare);
     184
     185  pos = tr_ptrArrayLowerBound (t, ptr, compare, NULL);
     186  ret = tr_ptrArrayInsert (t, ptr, pos);
     187
     188  assertSortedAndUnique (t, compare);
     189  return ret;
    194190}
    195191
    196192void*
    197193tr_ptrArrayFindSorted (tr_ptrArray * t,
    198                        const void * ptr,
     194                       const void  * ptr,
    199195                       int           compare (const void*, const void*))
    200196{
    201     bool match = false;
    202     const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
    203     return match ? t->items[pos] : NULL;
     197  bool match = false;
     198  const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
     199  return match ? t->items[pos] : NULL;
    204200}
    205201
     
    209205                         int           compare (const void*, const void*))
    210206{
    211     bool match;
    212     void * ret = NULL;
    213     const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
    214     assertSortedAndUnique (t, compare);
    215 
    216     if (match)
    217     {
    218         ret = t->items[pos];
    219         assert (compare (ret, ptr) == 0);
    220         tr_ptrArrayErase (t, pos, pos + 1);
    221     }
    222     assertSortedAndUnique (t, compare);
    223     assert ((ret == NULL) || (compare (ret, ptr) == 0));
    224     return ret;
    225 }
     207  bool match;
     208  void * ret = NULL;
     209  const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
     210
     211  assertSortedAndUnique (t, compare);
     212
     213  if (match)
     214    {
     215      ret = t->items[pos];
     216      assert (compare (ret, ptr) == 0);
     217      tr_ptrArrayErase (t, pos, pos + 1);
     218    }
     219
     220  assertSortedAndUnique (t, compare);
     221  assert ((ret == NULL) || (compare (ret, ptr) == 0));
     222  return ret;
     223}
Note: See TracChangeset for help on using the changeset viewer.